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:
- //
- 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;
- }
- //
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:
- 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);
- }
- }
Add the following code to the button's Click event:
- 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 + .
- Controlloc.y + button1. Height * 2);
- Rectangle Calrect = button1. DisplayRectangle;
- Cal. Show (Locpoint);
- }
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.
- 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 ();
- }
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