In this example, you will learn how to print all numbers between two numbers (entered by the user) in Listbox using C# Windows Form Application.
This problem is solved using for loop.
Form Design:
Source Code (button1_Click):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | private void button1_Click(object sender, EventArgs e) { listBox1.Items.Clear(); int n1, n2; n1 = Convert.ToInt32(txtNum1.Text); n2 = Convert.ToInt32(txtNum2.Text); for(int i=n1;i<=n2;i++) { listBox1.Items.Add(i); } } |
Output: