Tutorial: Create a math quiz WinForms app

In this series of 4 tutorials, y'all'll build a math quiz. The quiz contains 4 random math issues that a quiz taker tries to reply within a specified time.

The Visual Studio integrated development environment (IDE) provides the tools that you need to create the app. To learn more about this IDE, see Welcome to the Visual Studio IDE.

In this beginning tutorial, you learn how to:

  • Create a Visual Studio project that uses Windows Forms.
  • Add labels, a button, and other controls to a form.
  • Set properties for the controls.
  • Save and run your project.

Prerequisites

Y'all need Visual Studio to complete this tutorial. Visit the Visual Studio downloads page for a costless version.

Create your Windows Forms project

When you create a math quiz, the first step is to create a Windows Forms App project.

  1. Open Visual Studio.

  2. On the card bar, select File > New > Project.

  3. On the left side of the New Project dialog box, select either Visual C# or Visual Bones, and then select Windows Desktop.

  4. In the project templates list, select Windows Forms App (.Internet Framework). Name the new form MathQuiz, and then select OK.

    Note

    If you don't run into the Windows Forms App (.NET Framework) template, utilise Visual Studio Installer to install the .NET desktop evolution workload.

    Screenshot that shows the dot NET desktop development workload in Visual Studio Installer.

    For more information, see Install Visual Studio.

  1. Open Visual Studio.

  2. On the start window, select Create a new project.

    Screenshot that shows the Create a new project option in the Visual Studio start window.

  3. In the Create a new projection window, search for Windows Forms. Then select Desktop from the Projection type list.

  4. Select the Windows Forms App (.Net Framework) template for either C# or Visual Bones, and then select Next.

    Screenshot that shows the Create a new project dialog box. The search box, the Project type list, and two templates are called out.

    Annotation

    If yous don't see the Windows Forms App (.NET Framework) template, you lot tin can install it from the Create a new project window. In the Not finding what you're looking for? bulletin, select Install more tools and features.

    Screenshot that shows the Install more tools and features link from the Not finding what you're looking for message in the Create new project dialog box.

    Side by side, in Visual Studio Installer, select .Cyberspace desktop development.

    Screenshot that shows the dot NET desktop development workload in Visual Studio Installer.

    Select Change in Visual Studio Installer. You might be prompted to save your work. Next, select Continue to install the workload.

  5. In the Configure your new projection window, proper noun your project MathQuiz, and then select Create.

  1. Open up Visual Studio.

  2. On the start window, select Create a new project.

    Screenshot that shows the Create a new project option in the Visual Studio start window.

  3. In the Create a new project window, search for Windows Forms. And so select Desktop from the Project type list.

  4. Select the Windows Forms App (.Internet Framework) template for either C# or Visual Bones, and so select Next.

    Screenshot that shows the Create a new project dialog box. The search box, the Project type list, and two templates are called out.

    Note

    If you don't see the Windows Forms App (.NET Framework) template, you tin install it from the Create a new project window. In the Non finding what yous're looking for? message, select Install more tools and features.

    Screenshot that shows the Install more tools and features link from the Not finding what you're looking for message in the Create new project dialog box.

    Side by side, in Visual Studio Installer, select .Cyberspace desktop development.

    Screenshot that shows the dot NET desktop development workload in Visual Studio Installer.

    Select Modify in Visual Studio Installer. You might be prompted to relieve your piece of work. Next, select Continue to install the workload.

  5. In the Configure your new project window, name your project MathQuiz, and then select Create.

Visual Studio creates a solution for your app. A solution is a container for all the projects and files that your app needs.

Set form properties

After you lot select your template and name your file, Visual Studio opens a form for you. This section shows you how to change some form properties.

  1. In your project, select Windows Forms Designer. The designer tab is labeled Form1.cs [Pattern] for C# or Form1.vb [Design] for Visual Basic.

  2. Select the course, Form1.

  3. The Properties window now displays backdrop for the class. This window is ordinarily in the lower right corner of Visual Studio. If you don't see Properties, select View > Properties Window.

  4. Find the Text holding in the Properties window. Depending on how the listing is sorted, you might need to scroll downward. Enter the value Math Quiz for the Text value, and and then select Enter.

    Your grade at present has the text "Math Quiz" in its title bar.

    Note

    You can brandish properties by category or alphabetically. Employ the buttons on the Backdrop window to switch back and forth.

  5. Alter the size of the form to 500 pixels wide by 400 pixels alpine.

    Yous can resize the form by dragging its edges or drag handle until the correct size appears as the Size value in the Properties window. The drag handle is a minor white square in the lower-right corner of the form. You can as well resize the form by changing the values of the Size belongings.

  6. Change the value of the FormBorderStyle holding to Fixed3D, and set up the MaximizeBox holding to False.

    These values forbid quiz takers from resizing the form.

Create the time remaining box

The math quiz contains a box in the upper-right corner. That box shows the number of seconds that remain in the quiz. This section shows you how to use a characterization for that box. You'll add lawmaking for the countdown timer in a later tutorial in this series.

  1. On the left side of the Visual Studio IDE, select the Toolbox tab. If yous don't see the toolbox, select View > Toolbox from the carte bar or Ctrl+Alt+X.

  2. Select the Label control in the Toolbox, and so drag it onto the grade.

  3. In the Properties box, gear up the following backdrop for the label:

    • Set the (Name) to timeLabel.
    • Change AutoSize to False so that you can resize the box.
    • Change the BorderStyle to FixedSingle to draw a line around the box.
    • Set the Size to 200, 30.
    • Select the Text property, and then select the Backspace key to clear the Text value.
    • Select the plus sign (+) adjacent to the Font property, and and so fix the Size to fifteen.75.
  4. Motility the label to the upper-right corner of the form. When bluish spacer lines appear, use them to position the command on the form.

  5. Add another Label control from the Toolbox, and then set its font size to fifteen.75.

  6. Set up this characterization's Text holding to Fourth dimension Left.

  7. Move the label then that it lines up to the left of the timeLabel label.

    Screenshot that shows the Time Left label and the remaining time label. The controls are lined up next to each other in the upper-right corner of the form.

Add controls for the addition problem

The first office of the quiz is an addition problem. This section shows you how to use labels to display that problem.

  1. Add a Label control from the Toolbox to the form.

  2. In the Properties box, set the following properties for the label:

    • Set the Text to ? (question mark).
    • Set up AutoSize to Faux.
    • Set the Size to 60, fifty.
    • Set the font size to 18.
    • Set TextAlign to MiddleCenter.
    • Set the Location to fifty, 75 to position the control on the form.
    • Set the (Name) to plusLeftLabel.
  3. In the grade, select the plusLeftLabel label that you created. Copy the label past selecting either Edit > Copy or Ctrl+C.

  4. Paste the label into the grade three times past selecting either Edit > Paste or Ctrl+V three times.

  5. Adjust the iii new labels then that they are in a row to the right of the plusLeftLabel label.

  6. Prepare the second label's Text property to + (plus sign).

  7. Ready the 3rd label'south (Name) property to plusRightLabel.

  8. Set the fourth characterization'southward Text belongings to = (equals sign).

  9. Add a NumericUpDown control from the Toolbox to the class. You'll learn more nigh this kind of control later.

  10. In the Properties box, set the post-obit properties for the NumericUpDown control:

    • Set the font size to 18.
    • Set the width to 100.
    • Prepare the (Name) to sum.
  11. Line upwardly the NumericUpDown control with the Label controls for the add-on problem.

    Screenshot that shows the first row of the math quiz. Labels are visible, and a control with arrow keys displays a zero.

Add together controls for the subtraction, multiplication, and division problems

Side by side, add together labels to the form for the remaining math issues.

  1. Copy the iv Label controls and the NumericUpDown control that you created for the improver problem. Paste them into the form.

  2. Move the new controls to line up below the addition controls.

  3. In the Properties box, set the post-obit backdrop for the new controls:

    • Set the (Proper name) of the get-go question-marker characterization to minusLeftLabel.
    • Set the Text of the second label to - (minus sign).
    • Set the (Proper noun) of the second question-mark label to minusRightLabel.
    • Set the (Proper noun) of the NumericUpDown control to difference.
  4. Copy the addition controls, and paste them two more times into the course.

  5. For the third row:

    • Set the (Proper noun) of the first question-marker label to timesLeftLabel.
    • Set the Text of the 2nd label to × (multiplication sign). You tin re-create the multiplication sign from this tutorial and paste information technology onto the course.
    • Set the (Proper name) of the second question-mark characterization to timesRightLabel.
    • Set the (Proper noun) of the NumericUpDown command to product.
  6. For the fourth row:

    • Set the (Name) of the commencement question-mark characterization to dividedLeftLabel.
    • Prepare the Text of the second label to ÷ (division sign). You lot tin can re-create the partitioning sign from this tutorial and paste information technology onto the form.
    • Set the (Proper name) of the 2nd question-mark label to dividedRightLabel.
    • Set the (Name) of the NumericUpDown control to caliber.

Screenshot that shows a math quiz with four rows of problems. Labels and controls with arrow keys are visible.

Add together a starting time push and set the tab-index order

This department shows you how to add a offset push button. You'll also specify the tabbing order of the controls. This ordering determines how the quiz taker moves from one command to the next past using the Tab key.

  1. Add a Button control from the Toolbox to the form.

  2. In the Properties box, set the post-obit backdrop of the Push:

    • Set the (Name) to startButton.
    • Set the Text to Starting time the quiz.
    • Fix the font size to xiv.
    • Set AutoSize to True, which causes the button to automatically resize to fit the text.
    • Prepare the TabIndex to 0. This value makes the start button the get-go control to receive the focus.
  3. Center the button almost the bottom of the form.

    Screenshot that shows a math quiz with four rows of problems and a start button.

  4. In the Properties box, gear up the TabIndex holding of each NumericUpDown control:

    • Set the TabIndex of the sum NumericUpDown control to one.
    • Gear up the TabIndex of the divergence NumericUpDown control to two.
    • Set the TabIndex of the production NumericUpDown control to 3.
    • Fix the TabIndex of the quotient NumericUpDown control to 4.

Run your app

The math issues don't work yet on your quiz. Simply you can withal run your app to check whether the TabIndex values function every bit you expect.

  1. Use one of the following methods to salve your app:

    • Select Ctrl+Shift+South.
    • On the bill of fare bar, select File > Save All.
    • On the toolbar, select the Save All button.
  2. Employ ane of the following methods to run your app:

    • Select F5.
    • On the menu bar, select Debug > First Debugging.
    • On the toolbar, select the Starting time button.
  3. Select the Tab central a few times to see how the focus moves from one control to the next.

Next steps

Accelerate to the next tutorial to add together random math problems and an effect handler to your math quiz.