Uncategorized

How to add items dynamically in ListBox windows forms using C#

In this article we will learn How to add items dynamically in ListBox windows forms using C#.

In this blog we will see step by step How to add items dynamically in ListBox windows forms using C#.

Step 1. We will add one ListBox, one textbox, one label and one button on windows form.

how-to-add-items-dynamically-in-listbox-windows-forms-using-c

Step 2. In this step we will change the text of label and button. Need to open properties of label and button for change text just like given below screenshot.

how-to-add-items-dynamically-in-listbox-windows-forms-using-c

how-to-add-items-dynamically-in-listbox-windows-forms-using-c

Step 3. In this step we will double click on Add button for generate click event. After double click Form1.cs page will open. You can see in snap shot.

how-to-add-items-dynamically-in-listbox-windows-forms-using-c

how-to-add-items-dynamically-in-listbox-windows-forms-using-c

Step 4. Write this code in Form1.cs page for add items dynamically in ListBox windows forms using C#.

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

namespace ListboxControl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//listbox1 is the name of listbox
//textBox1 is the name of textbox
//item: An object representing the item to add to the collection.
// Add : Add an items in listbox
listBox1.Items.Add(textBox1.Text);
//code for clear textbox after add item in listbox
textBox1.Clear();
textBox1.Focus();
}
}
}

Output

how-to-add-items-dynamically-in-listbox-windows-forms-using-c

how-to-add-items-dynamically-in-listbox-windows-forms-using-c

Leave a Reply

Your email address will not be published.