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

开发 后端
如果我们正在做一个类似于库存控制和计费系统的项目,有些部分可能必须手动计算数值。因此,用户就不得不使用计算器得到结果,再填入到输入字段中,或者在工作窗口上单独打开一个计算器窗口。总之,各种不便和麻烦。

本文介绍了如何在Visual Studio中创建用户控件来显示下拉式计算器,弹出效果类似于日历控件。

介绍

如果我们正在做一个类似于库存控制和计费系统的项目,有些部分可能必须手动计算数值。因此,用户就不得不使用计算器得到结果,再填入到输入字段中,或者在工作窗口上单独打开一个计算器窗口。总之,各种不便和麻烦。

这篇文章主要描述的是如何添加下拉式计算器到DataGridView单元格中,如下图:

Before Opening the Drop

Drop View

使用代码

***步,我们必须先创建一个函数计算器,并且能够使用控件。因此,不妨先创建一个Visual Studio用户自定义控件。怎么做呢?打开VS,创建一个新的Windows窗体应用程序(甚至你也可以在你当前的项目中这么做,但***能分开,然后结合)。

然后,在Solution Explorer中,右键单击项目,选择add->User Control。命名(这里使用“CalculatorControl”),并添加。这时会给你一个像工作空间一样的Windows窗体。在它上面,用控件工具箱中的TextBoxButton创建一个计算器的布局。布局越小越好(想想日历控件),因为这就是个计算器而已。

Calculator Design

为了快速搞定计算器功能,可以点击这里下载NCal(确保下载二进制文件),并添加到项目的引用文件中。

实现每个数字按钮的点击事件,将对应的数字输入/(追加)到文本框中,然后用同样的方式实现其他按钮,如+,X,/…并把对应的符号输入/(追加)到文本框中…

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

然后使用下面的代码来验证表达式,并得到结果:

 

  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. // 

现在计算器功能已经完成。直接构建解决方案,那么你可能会发现用户控件显示在工具箱顶部。你可以添加Windows窗体,拖放用户控件到窗体中运行,看看能否正常工作。

然后,在你想要添加下拉式计算器的项目中,创建另一个只有一个小按钮的用户控件。这个按钮将被用于打开计算器。

添加CalculatorControl内置引用文件到项目中。

创建一个新的继承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

在你构建解决方案时,新的按钮控件会出现在工具箱中。添加以下代码到项目的窗体类中。

 

  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中,可以让界面显得更为互动。

许可证

这篇文章中任何相关的源代码和文件,都是在The Code Project Open License (CPOL)许可下的。

责任编辑:王雪燕 来源: 码农网
相关推荐

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#日历控件

2009-04-27 09:11:51

C#房贷计算器

2016-12-12 13:41:37

iOS简易加法开发

2011-09-16 14:13:15

Windows7计算器

2009-08-24 10:06:31

C#接口成员

2009-08-27 14:29:28

显式实现接口

2009-08-03 13:43:02

C#日历控件

2009-09-11 09:17:00

C# Button

2020-08-12 08:22:37

Python开发个税

2009-08-31 17:53:20

C#实现索引器

2009-09-11 12:41:41

C#类型转换

2009-09-11 10:25:35

C# button样式

2009-09-04 09:27:48

C#调用浏览器

2017-09-05 16:43:47

Electron桌面计算器

2010-01-15 19:12:36

Linux计算器

2022-03-02 15:35:57

UI界面容器组件鸿蒙

2009-09-01 18:29:10

C#继承C#多态
点赞
收藏

51CTO技术栈公众号