Xceed Grid for WinForms v4.3 Documentation
WinCalculator control

Welcome to Xceed Grid for WinForms v4.3 > Basic Concepts > Editor Controls > WinCalculator control

The WinCalculator class represents an extensible and themable calculator control for Windows Forms which supports both Windows XP and classic Windows themes and can adapt to the current Windows theme. 

Calculator buttons

The appearance and behavior of each button in the WinCalculator control can be modified through the ButtonStyles collection. Each button has a specific CalculatorFunctions value assigned to it which is used to access the button within the ButtonStyles collection. The appearance of a  button can be modified through the ButtonStyle's Appearance property while the behavior and visibility of the button can be modified directly on the ButtonStyle. For example, to modify the type of button used as the "Multiply" button, a new ButtonType object must be assigned to the ButtonType property of the ButtonStyle' s Appearance property.

See example

VB.NET
Copy Code
Dim calculator As New WinCalculator()
calculator.ButtonStyles( CalculatorFunctions.Multiply ).Appearance.ButtonType =New ButtonType( ButtonBackgroundImageType.Help, ButtonImageType.Delete )
calculator.ButtonStyles( CalculatorFunctions.Multiply ).Text = String.Empty
Me.Controls.Add( calculator )
C#
Copy Code
WinCalculator calculator = new WinCalculator();
calculator.ButtonStyles[ CalculatorFunctions.Multiply ].Appearance.ButtonType = new  ButtonType( ButtonBackgroundImageType.Help, ButtonImageType.Delete );
calculator.ButtonStyles[ CalculatorFunctions.Multiply ].Text = string.Empty;
this.Controls.Add( calculator );

The behavior of each button is modifiable directly on the button's ButtonStyle. For example, to render a button invisible, the ButtonStyle's Visible property can be set to false. Not that setting the Visible property is set to false, it will no longer be visible however the space reserved to it in the WinCalculator will remain unoccupied.

See example

VB.NET
Copy Code
Dim calculator As New WinCalculator()
calculator.ButtonStyles( CalculatorFunctions.MemoryAdd ).Visible = False
calculator.ButtonStyles( CalculatorFunctions.MemoryClear ).Visible = False
calculator.ButtonStyles( CalculatorFunctions.MemoryEqual ).Visible = False
calculator.ButtonStyles( CalculatorFunctions.MemoryRecall ).Visible = False
Me.Controls.Add( calculator )
C#
Copy Code
WinCalculator calculator = new WinCalculator();
calculator.ButtonStyles[ CalculatorFunctions.MemoryAdd ].Visible = false;
calculator.ButtonStyles[ CalculatorFunctions.MemoryClear ].Visible = false;
calculator.ButtonStyles[ CalculatorFunctions.MemoryEqual ].Visible = false;
calculator.ButtonStyles[ CalculatorFunctions.MemoryRecall ].Visible = false;
this.Controls.Add( calculator );

By default, the height and width of the buttons will be resized if/when the WinCalculator is resized however; explicitly setting the ButtonHeight and ButtonWidth properties will prevent the height and width of the buttons from being modified when the calculator is resized. This also implies that the size of the WinCalculator control will not adapt to the size defined by the ButtonHeight and ButtonWidth properties and clipping of the buttons may occur. 

The fitted width and height of the WinCalculator control (the size necessary to correctly display all the buttons and the formula box) can be retrieve using the GetFittedWidth and GetFittedHeight methods and assigned to the Width and Height properties of the WinCalculator control.

See example

VB.NET
Copy Code
Dim calculator As New WinCalculator()
calculator.ButtonHeight = 35
calculator.ButtonWidth = 35
calculator.Width = calculator.GetFittedWidth()
calculator.Height = calculator.GetFittedHeight()
Me.Controls.Add( calculator )
C#
Copy Code
WinCalculator calculator = new WinCalculator();
calculator.ButtonHeight = 35;
calculator.ButtonWidth = 35;
calculator.Width = calculator.GetFittedWidth();
calculator.Height = calculator.GetFittedHeight();
this.Controls.Add( calculator );

Formula box

The formula box contained in the WinCalculator control is the area where the mathematical equation is displayed, and is accessible through the FormulaBox property which only allows for the visual characteristics of the formula box to be modified.  

The formula box can be located either above or below the buttons in the WinCalculator control by setting its Position property, or it can be hidden by setting its Visible property to false. The formula box itself cannot be replaced or removed. 

The height and width of the formula box are always considered when using the GetFittedWidth and GetFittedHeight methods.

Mathematical functions

Mathematical functions can be executed through mouse or keyboard interactions with the WinCalculator control, or by calling the ExecuteFunction method. Normally, the ExecuteFunction method is used to execute operators (addition, division, multiplication, subtraction, equal, etc) however it can also be used to execute number and memory functions. The function executed by the ExecuteFunction method will be displayed in the FormulaBox and subsequent functions will be appended until the Equal or Clear function is called. If the Equal function is called, the pending operation will be resolved and the result displayed. 

Every time a button is pressed through user interaction or through the ExecuteFunction method, the FunctionExecuted event will be raised. If an error occurs during the execution of a function, the Error event will be raised specifying what error occurred. If the error occurred because of a division by zero or because of an overflow, the DivisionByZeroText and OverflowErrorText respectively will be displayed in the formula box. 

Values can be programmatically inserted at the end of an equation using the InsertValue method. If the InsertValue method is called and the last item in the equation is a number, the new value added using the InsertValue method will replace the last number in the equation in its entirety. If the last value in the equation is an operator and the InsertValue method is called, the number will be added after the operator. Using the ExecuteFunction to add a number to the equation will not replace the last number (if the last value in the equation is a number) but will append the new number to the existing one. 

For example, if the last value in the equation is "2" and the InsertValue method is called with "5", the value displayed in the formula box will be "5". If the same is done with the ExecuteFunction method, "5" will be appended to "2" resulting in "25" being displayed in the formula box. 

Values can be set and retrieved using the SetValue and GetValue methods respectively. The GetValue method can be used to retrieve and display the resolved value of the equation in the formula box, while the SetValue method will clear the current equation and replace the content of the formula box with its specified value. 

The WinCalculator control supports 28 significant digits: -9.999999999999999999999999999 E+2147483647   through -1.0 e-2147483648 for negative values and 1.0 E-2147483648 through 9.999999999999999999999999999 E+2147483647 for positive values.