Simple calculator in windows form




















Press OK. Perform some changes in properties as shown in figure. Step 4 Now, we start working with the display, usually when the calculator starts it should display the number 0. In our case it does not. So we modify the Text Property and write 0 in that. The second thing to note is that the number is aligned to the left, while calculators align the number to the right. Search for the TextAlign property and change it to Right.

This is what the window will look like. Step 5 From the tool box window, drag and drop a Button onto the form. Change its Name property to n1. This will help us identify which number was pressed. The only correct way is to use the designer.

Let's move to the calculation itself. The code won't be complicated at all. We'll simply just use conditions for the operationComboBox items and calculate the result inside the event handler of the button accordingly. Then we'll set the result as the text of resultLabel. We shouldn't forget to handle division by zero. First, we store the values from the controls in variables to make the code more readable.

We access the selected comboBox item using the SelectedItem property, which is of the object type. This means we have to convert it to string in our case. Similarly, we could also use just the item index using SelectedIndex. Since NumericUpDown returns the value in its Value property which is of the decimal type, we must convert it to the double type using the Convert class.

For the case of zero divisor, we display a MessageBox using the static class of the same name and calling the Show method. Finally, we display the result in resultLabel. Unlike the console, where we could simply print the numbers, we must first convert the numbers to string here.

Like this, the form will be created in the center of the screen. We can also disable window maximization using the MaximizeBox property. The code is available in the article's attachment as always. In the next lesson, Birthday Reminder - Designing Forms , we'll create a more complex app using multiple forms - a birthday reminder. Did you have a problem with anything? Download the sample application below and compare it with your project, you will find the error easily.

Downloaded x Only this week! More info. Login Register. Programming courses How I start C. Master Form Applications in C. NET Windows Forms! Just click the button below to start the whole online course! Previous article Introduction to Windows Forms applications. All articles in this section Form Applications in C. NET Windows Forms. Skip article not recommended Birthday Reminder - Designing Forms.

Article has been written for you by David Capka. User rating:. He shares his knowledge with the community and is always looking to improve. He believes that anyone can do what they set their mind to. Tagged as C. Stats A Simple Calculator in Windows Forms. Yvan Rodrigues Rate me:. Please Sign up or sign in to vote. Download source - Using the Code C. Copy Code. Yvan Rodrigues. Systems Engineer Clearpath Robotics. Yvan Rodrigues has 30 years of experience in information systems and software development for the industry.

Yvan draws on experience as owner of Red Cell Innovation Inc. Yvan supports open-source software. Tuan Nguyen 8-Feb Member Dec Yvan Rodrigues Dec Yvan Rodrigues , C. Red Cell Innovation Inc. Go to top. Layout: fixed fluid. First Prev Next.



0コメント

  • 1000 / 1000