Topic description
Create a C# Windows application, enter n, and find 1+2+3+..+n.
Steps
1> First add 2 Label controls, 1 TextBox, and 1 Button control to Windows Forms. The main property settings of each control are listed in the following table.
controls | Attributes | property settings | controls | Attributes | property settings |
label1 | Text | n: | textBox1 | Name | txtNum |
label2 | Text | "" | button | Name | btnOk |
Name | lbShow | Text | Sure |
2> Double-click the "btnOk" button in the form design area, the system automatically adds the "Click" event and the corresponding event method for the button, and then edit the code in the source code view .
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 Test3_5 { public partial class Test3_5 : Form { public Test3_5() { InitializeComponent(); } private void btnOk_Click(object sender, EventArgs e) { int n = Convert.ToInt32(txtNum.Text); int i = 1; int sum=0; while(i<=n){ sum+=i; i++; } lblShow.Text = "1+2+3+....+n = " + sum; } } }