首页 技术 正文
技术 2022年11月12日
0 收藏 369 点赞 2,810 浏览 3100 个字

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

原文地址:http://developer.51cto.com/art/201508/487486.htm

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

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

介绍

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

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

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

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

使用代码

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

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

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. public void btnEqual_Click(object sender, EventArgs e)
  9. {
  10. Expression ex = new Expression(textBox1.Text);
  11. if (ex.HasErrors())
  12. {
  13. //Invalid Expression
  14. }
  15. else
  16. {
  17. result = Convert.ToDouble(ex.Evaluate());
  18. resText = result.ToString();
  19. }
  20. textBox1.Text = resText;
  21. text = resText;
  22. eqPressed = true;
  23. }
  24. //

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

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

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

创建一个新的继承ToolStripDropDown的类:

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

在按钮的单击事件中添加以下代码:

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

添加控件到DataGridViewCell

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

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

兴趣点

本技巧描述的是添加控件到DataGridView中,可以让界面显得更为互动。

许可证

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291