溫馨提示×

c# listbox如何添加數(shù)據(jù)項(xiàng)

c#
小樊
81
2024-10-17 00:16:26
欄目: 編程語言

在C#中,可以使用ListBox控件的Items屬性來添加數(shù)據(jù)項(xiàng)。以下是一個(gè)簡單的示例,演示了如何在WinForms應(yīng)用程序中使用ListBox控件添加數(shù)據(jù)項(xiàng):

  1. 打開Visual Studio,創(chuàng)建一個(gè)新的Windows Forms應(yīng)用程序項(xiàng)目。
  2. 在設(shè)計(jì)器中找到ListBox控件,并將其添加到窗體上。
  3. 在窗體上的代碼編輯器(例如Form1.cs)中,為ListBox控件添加Items屬性。例如:
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        // 添加數(shù)據(jù)項(xiàng)到ListBox控件
        listBox1.Items.Add("數(shù)據(jù)項(xiàng)1");
        listBox1.Items.Add("數(shù)據(jù)項(xiàng)2");
        listBox1.Items.Add("數(shù)據(jù)項(xiàng)3");
    }
}

在這個(gè)示例中,我們首先使用InitializeComponent()方法初始化窗體和控件。然后,我們使用Items.Add()方法向ListBox控件添加三個(gè)數(shù)據(jù)項(xiàng)。

現(xiàn)在,運(yùn)行應(yīng)用程序,你會(huì)看到ListBox控件中顯示了添加的數(shù)據(jù)項(xiàng)。

0