首页 > 编程 > C# > 正文

C#日历样式的下拉式计算器实例讲解

2019-10-29 21:37:45
字体:
来源:转载
供稿:网友

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

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

介绍

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

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

C#日历样式的下拉式计算器实例讲解

C#日历样式的下拉式计算器实例讲解

使用代码

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

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

C#日历样式的下拉式计算器实例讲解

为了快速搞定计算器功能,可以点击这里下载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. {  
  5. calculator = new CalculatorPick();  
  6.  
  7. calculator.Visible = false;  
  8. dataGridView2.Controls.Add(calculator);  
  9. }  
  10.  
  11. private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)  
  12. {  
  13. if (e.ColumnIndex == clmCommision.Index)  
  14. {  
  15. Rectangle calRect = dataGridView2.GetCellDisplayRectangle  
  16. (e.ColumnIndex, e.RowIndex,false);  
  17.  
  18. Point p = calculator.FindForm().PointToClient  
  19. (calculator.Parent.PointToScreen(calculator.Location));  
  20. p.X -= calculator.Width/3;  
  21. p.Y += calculator.Height;  
  22. calculator.LocPoint = p;  
  23.  
  24. calculator.Width = calRect.Width/3;  
  25. calculator.Height = calRect.Height;  
  26.  
  27. calculator.Visible = true;  
  28. calculator.Calculator.btnEqual.Click += new EventHandler(calculatorBtnEqlClicked);  
  29. }  
  30. else 
  31. if(calculator!=null)  
  32. calculator.Visible = false;  
  33. }  
  34.  
  35. void calculatorBtnEqlClicked(object sender, EventArgs e)  
  36. {  
  37. dataGridView2.CurrentCell.Value = calculator.Calculator.Result.ToString();  

本技巧描述的是添加控件到DataGridView中,可以让界面显得更为互动,喜欢的朋友就点个赞吧!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表