C# Object-Oriented Programming Course Experiment 2: Experiment Name: Windows Form Program

Experiment content: Windows Forms program

insert image description here

1. Experimental purpose and requirements

  • (1) Master the basic applications of common properties, methods, and events of controls Label, Button, and TextBox;
  • (2) Master the basic application of common properties, methods and events of controls RadioButton and CheckBox through simple programs;
  • (3) Master the basic applications of frame-frame controls GroupBox, Panel, and TabControl;
  • (4) Master the basic applications of common properties, methods, and events of the controls ListBox, CombBox, and ListView;
  • (5) Master the structure of Windows applications;
  • (6) Master the design of WinForm
  • (7) Further study and master the methods of finding and modifying compilation errors;

2. Experimental environment

Microsoft Visual Studio 2008

3. Experimental content and steps

1. Design a simple calculator

3.1. Experimental content

  • Design a simple calculator so that it can perform addition, subtraction, multiplication, and division calculations. The running status of the program is shown in the figure. (Question 3.1 on page 113 of Chapter 4 of the textbook)

As follows

insert image description here

3.2. Experimental steps

1. The properties of the experimental interface are set as follows:

  • (1) Text properties of three Lables: the first number, the second number, and the operation result.
  • (2) Text property and Name property of four RadButtons: + and radAdd, - and radSub, × and radMul, ÷ and radDiv.
  • (3) Name and Text properties of a Button: btnOk and operation.

2. The design code of the experimental program is as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 实验二_1_
{
    
    
    public partial class Form1 : Form
    {
    
    
        public Form1()
        {
    
    
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
    
    
            double a, b;
            a = Convert.ToDouble(txtDigit1.Text);
            b = Convert.ToDouble(txtDigit2.Text);

            if (radAdd.Checked)
                txtShow.Text =Convert.ToString(a + b);
            if (radSub.Checked)
                txtShow.Text = Convert.ToString(a - b);
            if (radMul.Checked)
                txtShow.Text = Convert.ToString(a * b);
            if (radDiv.Checked)
            {
    
    
                if (b==0)
                    MessageBox.Show("除数不能为零!");
                else 
                    txtShow.Text = Convert.ToString(a / b);
            }
        }
    }
}
  • 3. The results of the experiment are as follows
  1. 3.1. Simple calculator division
    insert image description here
    insert image description here

  2. 3.2. Multiplication with a simple calculator
    insert image description here

  3. 3.3. Subtraction of a simple calculator

insert image description here

  1. 3.4. Addition of a simple calculator

insert image description here

4. The experiment realizes the basic operations of addition, subtraction, multiplication and division, and can judge the divisor cannot be zero.

2. Design the application program for purchasing calculator configuration

3.1. Experimental content

  • Design an application for purchasing calculator configurations, as shown in the figure below. When the user selects the basic configuration and clicks the "OK" button, the selected information will be displayed in the right list box. (Question 3.2 on page 113 of Chapter 4 of the textbook)

As follows

insert image description here

3.2. Experimental steps

  • 1. The interface design of the experiment is as follows:
container Name attribute Item property DropDownHeight Property Text property
comboBox cboCollection Lenovo Founder Dell Compatible Simple
groupBox1 CPU
groupBox2 Memory
groupBox3 other devices
basic controls Name attribute Text property
RadioButton1 radPentiumD Pentium D
RadioButton2 radPentiumM Pentium M
RadioButton3 radXeon Xeon
RadioButton4 rad256MB 256MB
RadioButton5 Rad512MB 512MB
CheckBox1 chkPrinter printer
CheckBox2 chkMode Mode
CheckBox3 chkNetConfiguration network adapter
ListBox lstShow
Button btnOk Sure
  • 2. The programming code of the experiment is as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 实验二_2_
{
    
    
    public partial class Form1 : Form
    {
    
    
        public Form1()
        {
    
    
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
    
    
            lstShow.Items.Clear();
            string a = cboCollection.SelectedItem.ToString();
            lstShow.Items.Add(a);

            if (radPentiumD.Checked)
                lstShow.Items.Add(radPentiumD.Text);
            if (radPentiumM.Checked)
                lstShow.Items.Add(radPentiumM.Text);
            if (radXeon.Checked)
                lstShow.Items.Add(radXeon.Text);

            if (rad256MB.Checked)
                lstShow.Items.Add(rad256MB.Text);
            if (rad512MB.Checked)
                lstShow.Items.Add(rad512MB.Text);

            if (chkPrinter.Checked)
                lstShow.Items.Add(chkPrinter.Text);
            if (chkMode.Checked)
                lstShow.Items.Add(chkMode.Text);
            if (chkNetConfiguration.Checked)
                lstShow.Items.Add(chkNetConfiguration.Text);
        }
    }
}
  • 3. The running effect of the experiment is as follows:
  1. 3.1
    insert image description here

  2. 3.2
    insert image description here

  3. 3.3
    insert image description here

  • 4. The lstShow.Items.Clear(); statement realizes the effect of clearing the original ListBox Item collection for each operation.

4. Experimental summary

insert image description here

  • 1. Master the basic properties of Button, TextBox, RadioButton, CheckBox GroupBox and other basic controls and containers through Windows of C#.
  • 2. Master the basic usage of the Add method of ListBox Item to add items.
  • 3. Master the application of Convert class to convert data.
  • 4. During the experiment, there appeared
if (b=0)
   MessageBox.Show("除数不能为零!");

The basic error, the compilation is not successful, only to know that it should be

b==0(b=0是赋值语句,而b==0是判断b是否等于零)

insert image description here

Guess you like

Origin blog.csdn.net/m0_47419053/article/details/127131665