C # Implementing a calendar-style drop-down calculator

Source: Internet
Author: User

C # Implementing a calendar-style drop-down calculator

Original address: http://developer.51cto.com/art/201508/487486.htm

If we are working on a project similar to the Inventory Control and billing system, some parts may have to calculate the values manually. Therefore, the user will have to use the calculator to get the results, fill in the input fields, or open a separate calculator window on the work window. In short, all kinds of inconvenience and trouble.

This article describes how to create a user control in Visual Studio to display a drop-down evaluator that looks like a calendar control.

Introduced

If we are working on a project similar to the Inventory Control and billing system, some parts may have to calculate the values manually. Therefore, the user will have to use the calculator to get the results, fill in the input fields, or open a separate calculator window on the work window. In short, all kinds of inconvenience and trouble.

This article mainly describes how to add a drop-down calculator to the DataGridView cell, such as:

Using code

First, we must first create a function evaluator and be able to use the controls. Therefore, you might want to create a Visual Studio user custom control first. How do you do it? Open VS, create a new Windows Forms application (you can even do this in your current project, but it's better to separate and then combine).

Then, in the Solution Explorer, right-click the project and select Add->user Control. Name (use "Calculatorcontrol" here), and Add. This will give you a Windows Form that looks like a workspace. On top of it, use the Control Toolbox TextBox and Button Create a calculator layout. The smaller the layout, the better (think of the calendar control), because this is a calculator.

To quickly fix the calculator function, click here to download ncal (make sure to download the binaries) and add them to the project reference file.

Implement the Click event for each number button, enter/append the corresponding number to the text box, and implement the other buttons in the same way, such as +,x,/... and the corresponding symbol input/(append) to the text box ...

For example, in the text box, enter: 2 * 3 + 4

Then use the following code to validate the expression and get the result:

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

The calculator function is now complete. Build the solution directly, then you may find that the user control appears at the top of the toolbox. You can add Windows Forms, drag and drop user controls into the form to run, and see if they work correctly.

Then, in the project where you want to add a drop-down calculator, create another user control that has only one small button. This button will be used to open the calculator.

Add Calculatorcontrol built-in reference files to the project.

To create a new inherited ToolStripDropDown class:

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

Add the following code to the button's Click event:

  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 + .
  7. Controlloc.y + button1. Height * 2);
  8. Rectangle Calrect = button1. DisplayRectangle;
  9. Cal. Show (Locpoint);
  10. }

Adding Controls to DataGridViewCell

When you build the solution, the New button control appears in the Toolbox. Add the following code to the project's form class.

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

Points of interest

This tip describes adding controls to DataGridView to make the interface appear more interactive.

License

Any relevant source code and files in this article are under the Code Project Open License (cpol) license.

C # Implementing a calendar-style drop-down calculator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.