C#日歷款式的下拉式盤算器實例講授。本站提示廣大學習愛好者:(C#日歷款式的下拉式盤算器實例講授)文章只能為提供參考,不一定能成為您想要的結果。以下是C#日歷款式的下拉式盤算器實例講授正文
本文引見了若何在Visual Studio中創立用戶控件來顯示下拉式盤算器,彈出後果相似於日歷控件。
引見
假如我們正在做一個相似於庫存掌握和計費體系的項目,有些部門能夠必需手動盤算數值。是以,用戶就不能不應用盤算器獲得成果,再填入到輸出字段中,或許在任務窗口上零丁翻開一個盤算器窗口。總之,各類未便和費事。
這篇文章重要描寫的是若何添加下拉式盤算器到DataGridView單位格中,以下圖:


應用代碼
第一步,我們必需先創立一個函數盤算器,而且可以或許應用控件。是以,無妨先創立一個Visual Studio用戶自界說控件。怎樣做呢?翻開VS,創立一個新的Windows窗體運用法式(乃至你也能夠在你以後的項目中這麼做,但最好能離開,然後聯合)。
然後,在Solution Explorer中,右鍵單擊項目,選擇add->User Control。定名(這裡應用“CalculatorControl”),並添加。這時候會給你一個像任務空間一樣的Windows窗體。在它下面,用控件對象箱中的TextBox和Button創立一個盤算器的結構。結構越小越好(想一想日歷控件),由於這就是個盤算器罷了。

為了疾速弄定盤算器功效,可以點擊這裡下載NCal(確保下載二進制文件),並添加到項目標援用文件中。
完成每一個數字按鈕的點擊事宜,將對應的數字輸出/(追加)到文本框中,然後用異樣的方法完成其他按鈕,如+,X,/…並把對應的符號輸出/(追加)到文本框中…
例如在文本框中輸出:2 * 3 + 4
然後應用上面的代碼來驗證表達式,並獲得成果:
//
using System.Windows.Forms;
using NCalc;
//
string resText;
bool eqPressed;
double result;
public void btnEqual_Click(object sender, EventArgs e)
{
Expression ex = new Expression(textBox1.Text);
if (ex.HasErrors())
{
//Invalid Expression
}
else
{
result = Convert.ToDouble(ex.Evaluate());
resText = result.ToString();
}
textBox1.Text = resText;
text = resText;
eqPressed = true;
}
//
如今盤算器功效曾經完成。直接構建處理計劃,那末你能夠會發明用戶控件顯示在對象箱頂部。你可以添加Windows窗體,拖放用戶控件到窗體中運轉,看看可否正常任務。
然後,在你想要添加下拉式盤算器的項目中,創立另外一個只要一個小按鈕的用戶控件。這個按鈕將被用於翻開盤算器。
添加CalculatorControl內置援用文件到項目中。
創立一個新的繼續ToolStripDropDown的類:
using System.Windows.Forms;
class CalDrop : ToolStripDropDown
{
Control content;
ToolStripControlHost drop;
public CalDrop(CalculatorControl content)
{
this.content = content;
this.drop= new System.Windows.Forms.ToolStripControlHost(content);
//Add the host to the list
this.Items.Add(this.drop);
}
}
在按鈕的單擊事宜中添加以下代碼:
private void button1_Click(object sender, EventArgs e)
{
CalculatorControl calculator = new CalculatorControl();
CalDrop cal = new CalDrop(calculator);
Point controlLoc = fm.PointToScreen(button1.Location);
Point relativeLoc = new Point(controlLoc.X + button1.Width + 100,
controlLoc.Y + button1.Height * 2);
Rectangle calRect = button1.DisplayRectangle;
cal.Show(locPoint);
}
添加控件到DataGridViewCell
在你構建處理計劃時,新的按鈕控件會湧現在對象箱中。添加以下代碼到項目標窗體類中。
private CalculatorPick calculator;
public form1()
{
calculator = new CalculatorPick();
calculator.Visible = false;
dataGridView2.Controls.Add(calculator);
}
private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == clmCommision.Index)
{
Rectangle calRect = dataGridView2.GetCellDisplayRectangle
(e.ColumnIndex, e.RowIndex,false);
Point p = calculator.FindForm().PointToClient
(calculator.Parent.PointToScreen(calculator.Location));
p.X -= calculator.Width/3;
p.Y += calculator.Height;
calculator.LocPoint = p;
calculator.Width = calRect.Width/3;
calculator.Height = calRect.Height;
calculator.Visible = true;
calculator.Calculator.btnEqual.Click += new EventHandler(calculatorBtnEqlClicked);
}
else
if(calculator!=null)
calculator.Visible = false;
}
void calculatorBtnEqlClicked(object sender, EventArgs e)
{
dataGridView2.CurrentCell.Value = calculator.Calculator.Result.ToString();
}
本技能描寫的是添加控件到DataGridView中,可讓界面顯得更加互動,愛好的同伙就點個贊吧!