温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#中怎么实现一个日历样式的下拉式计算器

发布时间:2021-07-07 17:53:25 来源:亿速云 阅读:158 作者:Leah 栏目:编程语言

C#中怎么实现一个日历样式的下拉式计算器,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

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

介绍

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

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

C#中怎么实现一个日历样式的下拉式计算器

C#中怎么实现一个日历样式的下拉式计算器

使用代码

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

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

C#中怎么实现一个日历样式的下拉式计算器

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

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

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

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

// 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;          } //

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

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

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

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

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

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();            }

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI