日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

html下拉式日历,C#实现日历样式的下拉式计算器

發(fā)布時間:2023/12/20 C# 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 html下拉式日历,C#实现日历样式的下拉式计算器 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

介紹

如果我們正在做一個類似于庫存控制和計費系統(tǒng)的項目,有些部分可能必須手動計算數(shù)值。因此,用戶就不得不使用計算器得到結(jié)果,再填入到輸入字段中,或者在工作窗口上單獨打開一個計算器窗口。總之,各種不便和麻煩。

這篇文章主要描述的是如何添加下拉式計算器到DataGridView單元格中,如下圖:

使用代碼

第一步,我們必須先創(chuàng)建一個函數(shù)計算器,并且能夠使用控件。因此,不妨先創(chuàng)建一個Visual Studio用戶自定義控件。怎么做呢?打開VS,創(chuàng)建一個新的Windows窗體應(yīng)用程序(甚至你也可以在你當前的項目中這么做,但最好能分開,然后結(jié)合)。

然后,在Solution Explorer中,右鍵單擊項目,選擇add->User Control。命名(這里使用“CalculatorControl”),并添加。這時會給你一個像工作空間一樣的Windows窗體。在它上面,用控件工具箱中的TextBox和Button創(chuàng)建一個計算器的布局。布局越小越好(想想日歷控件),因為這就是個計算器而已。

為了快速搞定計算器功能,可以點擊這里下載NCal(確保下載二進制文件),并添加到項目的引用文件中。

實現(xiàn)每個數(shù)字按鈕的點擊事件,將對應(yīng)的數(shù)字輸入/(追加)到文本框中,然后用同樣的方式實現(xiàn)其他按鈕,如+,X,/…并把對應(yīng)的符號輸入/(追加)到文本框中…

例如在文本框中輸入:2 * 3 + 4

然后使用下面的代碼來驗證表達式,并得到結(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īng)完成。直接構(gòu)建解決方案,那么你可能會發(fā)現(xiàn)用戶控件顯示在工具箱頂部。你可以添加Windows窗體,拖放用戶控件到窗體中運行,看看能否正常工作。

然后,在你想要添加下拉式計算器的項目中,創(chuàng)建另一個只有一個小按鈕的用戶控件。這個按鈕將被用于打開計算器。

添加CalculatorControl內(nèi)置引用文件到項目中。

創(chuàng)建一個新的繼承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

在你構(gòu)建解決方案時,新的按鈕控件會出現(xiàn)在工具箱中。添加以下代碼到項目的窗體類中。

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中,可以讓界面顯得更為互動。

許可證

這篇文章中任何相關(guān)的源代碼和文件,都是在The Code Project Open License (CPOL)許可下的。

總結(jié)

以上是生活随笔為你收集整理的html下拉式日历,C#实现日历样式的下拉式计算器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。