自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#實現(xiàn)日歷樣式的下拉式計算器

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

本文介紹了如何在Visual Studio中創(chuàng)建用戶控件來顯示下拉式計算器,彈出效果類似于日歷控件。

介紹

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

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

Before Opening the Drop

Drop View

使用代碼

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

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

Calculator Design

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

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

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

然后使用下面的代碼來驗證表達式,并得到結(jié)果:

 

  1. // 
  2. using System.Windows.Forms; 
  3. using NCalc; 
  4. // 
  5.     string resText; 
  6.     bool eqPressed; 
  7.     double result; 
  8.  
  9. public void btnEqual_Click(object sender, EventArgs e) 
  10.         { 
  11.             Expression ex = new Expression(textBox1.Text); 
  12.             if (ex.HasErrors()) 
  13.             { 
  14.                 //Invalid Expression 
  15.             } 
  16.             else 
  17.             { 
  18.                 result = Convert.ToDouble(ex.Evaluate()); 
  19.                 resText = result.ToString(); 
  20.             } 
  21.             textBox1.Text = resText; 
  22.             text = resText; 
  23.             eqPressed = true
  24.  
  25.         } 
  26. // 

現(xiàn)在計算器功能已經(jīng)完成。直接構(gòu)建解決方案,那么你可能會發(fā)現(xiàn)用戶控件顯示在工具箱頂部。你可以添加Windows窗體,拖放用戶控件到窗體中運行,看看能否正常工作。

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

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

創(chuàng)建一個新的繼承ToolStripDropDown的類:

 

  1. using System.Windows.Forms; 
  2.  
  3. class CalDrop : ToolStripDropDown 
  4.     { 
  5.       Control content; 
  6.       ToolStripControlHost drop; 
  7.  
  8. public CalDrop(CalculatorControl content) 
  9.         { 
  10.  
  11.             this.content = content; 
  12.  
  13.             this.drop= new System.Windows.Forms.ToolStripControlHost(content); 
  14.  
  15.             //Add the host to the list 
  16.             this.Items.Add(this.drop); 
  17.         } 

在按鈕的單擊事件中添加以下代碼:

 

  1. private void button1_Click(object sender, EventArgs e) 
  2.         { 
  3.             CalculatorControl calculator = new CalculatorControl(); 
  4.             CalDrop cal = new CalDrop(calculator); 
  5.  
  6.             Point controlLoc = fm.PointToScreen(button1.Location); 
  7.             Point relativeLoc = new Point(controlLoc.X + button1.Width + 100
  8.     controlLoc.Y + button1.Height * 2); 
  9.             Rectangle calRect = button1.DisplayRectangle; 
  10.             cal.Show(locPoint); 
  11.         } 

添加控件到DataGridViewCell

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

 

  1. private CalculatorPick calculator; 
  2.  
  3. public form1() 
  4.             calculator = new CalculatorPick(); 
  5.  
  6.             calculator.Visible = false
  7.             dataGridView2.Controls.Add(calculator); 
  8.  
  9. private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e) 
  10.                 if (e.ColumnIndex == clmCommision.Index) 
  11.                 { 
  12.                     Rectangle calRect = dataGridView2.GetCellDisplayRectangle 
  13.       (e.ColumnIndex, e.RowIndex,false);                   
  14.  
  15.                     Point p = calculator.FindForm().PointToClient 
  16.     (calculator.Parent.PointToScreen(calculator.Location)); 
  17.                     p.X -= calculator.Width/3
  18.                     p.Y += calculator.Height; 
  19.                     calculator.LocPoint = p;  
  20.  
  21.                     calculator.Width = calRect.Width/3
  22.                     calculator.Height = calRect.Height; 
  23.  
  24.                     calculator.Visible = true
  25.                     calculator.Calculator.btnEqual.Click += new EventHandler(calculatorBtnEqlClicked); 
  26.                 } 
  27.                 else 
  28.                     if(calculator!=null
  29.                     calculator.Visible = false
  30.  
  31. void calculatorBtnEqlClicked(object sender, EventArgs e) 
  32. {            
  33.             dataGridView2.CurrentCell.Value = calculator.Calculator.Result.ToString();            

興趣點

本技巧描述的是添加控件到DataGridView中,可以讓界面顯得更為互動。

許可證

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

責(zé)任編輯:王雪燕 來源: 碼農(nóng)網(wǎng)
相關(guān)推薦

2024-01-31 08:33:06

C++編程計算器

2009-09-11 12:52:09

C# WinForm控編輯器

2022-07-11 16:19:22

css屬性鴻蒙

2009-08-11 15:46:15

C#日歷控件

2016-12-12 13:41:37

iOS簡易加法開發(fā)

2009-04-27 09:11:51

C#房貸計算器

2011-09-16 14:13:15

Windows7計算器

2009-08-24 10:06:31

C#接口成員

2009-09-11 09:17:00

C# Button

2009-08-27 14:29:28

顯式實現(xiàn)接口

2009-08-03 13:43:02

C#日歷控件

2020-08-12 08:22:37

Python開發(fā)個稅

2009-08-31 17:53:20

C#實現(xiàn)索引器

2009-09-11 12:41:41

C#類型轉(zhuǎn)換

2009-09-11 10:25:35

C# button樣式

2024-05-07 14:40:49

Python兒童計算器

2009-09-04 09:27:48

C#調(diào)用瀏覽器

2017-09-05 16:43:47

Electron桌面計算器

2010-01-15 19:12:36

Linux計算器

2022-03-02 15:35:57

UI界面容器組件鴻蒙
點贊
收藏

51CTO技術(shù)棧公眾號