C#日歷樣式的下拉式計(jì)算器實(shí)例講解
本文介紹了如何在Visual Studio中創(chuàng)建用戶控件來(lái)顯示下拉式計(jì)算器,彈出效果類(lèi)似于日歷控件。
介紹
如果我們正在做一個(gè)類(lèi)似于庫(kù)存控制和計(jì)費(fèi)系統(tǒng)的項(xiàng)目,有些部分可能必須手動(dòng)計(jì)算數(shù)值。因此,用戶就不得不使用計(jì)算器得到結(jié)果,再填入到輸入字段中,或者在工作窗口上單獨(dú)打開(kāi)一個(gè)計(jì)算器窗口??傊?,各種不便和麻煩。
這篇文章主要描述的是如何添加下拉式計(jì)算器到DataGridView單元格中,如下圖:


使用代碼
第一步,我們必須先創(chuàng)建一個(gè)函數(shù)計(jì)算器,并且能夠使用控件。因此,不妨先創(chuàng)建一個(gè)Visual Studio用戶自定義控件。怎么做呢?打開(kāi)VS,創(chuàng)建一個(gè)新的Windows窗體應(yīng)用程序(甚至你也可以在你當(dāng)前的項(xiàng)目中這么做,但最好能分開(kāi),然后結(jié)合)。
然后,在Solution Explorer中,右鍵單擊項(xiàng)目,選擇add->User Control。命名(這里使用“CalculatorControl”),并添加。這時(shí)會(huì)給你一個(gè)像工作空間一樣的Windows窗體。在它上面,用控件工具箱中的TextBox和Button創(chuàng)建一個(gè)計(jì)算器的布局。布局越小越好(想想日歷控件),因?yàn)檫@就是個(gè)計(jì)算器而已。

為了快速搞定計(jì)算器功能,可以點(diǎn)擊這里下載NCal(確保下載二進(jìn)制文件),并添加到項(xiàng)目的引用文件中。
實(shí)現(xiàn)每個(gè)數(shù)字按鈕的點(diǎn)擊事件,將對(duì)應(yīng)的數(shù)字輸入/(追加)到文本框中,然后用同樣的方式實(shí)現(xiàn)其他按鈕,如+,X,/…并把對(duì)應(yīng)的符號(hào)輸入/(追加)到文本框中…
例如在文本框中輸入:2 * 3 + 4
然后使用下面的代碼來(lái)驗(yàn)證表達(dá)式,并得到結(jié)果:
//
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;
}
//
現(xiàn)在計(jì)算器功能已經(jīng)完成。直接構(gòu)建解決方案,那么你可能會(huì)發(fā)現(xiàn)用戶控件顯示在工具箱頂部。你可以添加Windows窗體,拖放用戶控件到窗體中運(yùn)行,看看能否正常工作。
然后,在你想要添加下拉式計(jì)算器的項(xiàng)目中,創(chuàng)建另一個(gè)只有一個(gè)小按鈕的用戶控件。這個(gè)按鈕將被用于打開(kāi)計(jì)算器。
添加CalculatorControl內(nèi)置引用文件到項(xiàng)目中。
創(chuàng)建一個(gè)新的繼承ToolStripDropDown的類(lèi):
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
在你構(gòu)建解決方案時(shí),新的按鈕控件會(huì)出現(xiàn)在工具箱中。添加以下代碼到項(xiàng)目的窗體類(lèi)中。
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中,可以讓界面顯得更為互動(dòng),喜歡的朋友就點(diǎn)個(gè)贊吧!
相關(guān)文章
C#使用FileStream循環(huán)讀取大文件數(shù)據(jù)的方法示例
這篇文章主要介紹了C#使用FileStream循環(huán)讀取大文件數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了FileStream文件流的形式循環(huán)讀取大文件的相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
利用C#修改Windows操作系統(tǒng)時(shí)間
這篇文章主要介紹了利用C#修改Windows操作系統(tǒng)時(shí)間,幫助大家更好的利用c#操作系統(tǒng),感興趣的朋友可以了解下2020-10-10
C#實(shí)現(xiàn)Winform版計(jì)算器
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)Winform版計(jì)算器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
C#使用protobuf-net進(jìn)行序列化的詳細(xì)操作
本文帶領(lǐng)大家學(xué)習(xí)C#中protobuf-net工具的另一種使用體驗(yàn),這個(gè)工具的使用體驗(yàn)屬于Code-First模式,先定義類(lèi)型,并使用注解進(jìn)行標(biāo)記,不需要先編寫(xiě).proto文件,感興趣的朋友跟隨小編一起看看吧2021-11-11
Unity實(shí)現(xiàn)簡(jiǎn)單換裝系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)簡(jiǎn)單換裝系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
Unity3D實(shí)現(xiàn)控制攝像機(jī)移動(dòng)
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)控制攝像機(jī)移動(dòng) ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02

