c# listbox使用
Following operations are performing on the ListBox: 在ListBox上執行以下操作:-
Add
加
-
Remove
去掉
-
Clear
明确
-
Get selected items
擷取所選項目
-
etc...
等等...
-
txtInput (TextBox) : To take user input.
txtInput (TextBox):接受使用者輸入。
-
lblCount (Label) : To show count of list-box items.
lblCount (标簽):顯示清單框項目的計數。
-
lstItem (ListBox) : List-box to contain list of items.
lstItem (ListBox):包含項目清單的清單框。
-
btnAdd (Button) : To add entered item into list.
btnAdd (按鈕):将輸入的項目添加到清單中。
-
btnRemove (Button) : To remove selected item from list.
btnRemove (按鈕):從清單中删除標明的項目。
-
btnShow (Button) : To show selected item in message-box.
btnShow (按鈕):在消息框中顯示標明的項目。
-
btnClear (Button) : To clear complete list.
btnClear (按鈕):清除完整清單。
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 MyWinApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
lstItem.Items.Add(txtInput.Text);
txtInput.Text = "";
lblCount.Text = "Count: " + lstItem.Items.Count;
}
private void btnRmv_Click(object sender, EventArgs e)
{
lstItem.Items.RemoveAt(lstItem.SelectedIndex);
lblCount.Text = "Count: " + lstItem.Items.Count;
}
private void btnShow_Click(object sender, EventArgs e)
{
MessageBox.Show(lstItem.SelectedItem.ToString());
}
private void btnClr_Click(object sender, EventArgs e)
{
lstItem.Items.Clear();
lblCount.Text = "Count: " + lstItem.Items.Count;
}
private void Form1_Load(object sender, EventArgs e)
{
lblCount.Text = "Count: " + lstItem.Items.Count;
}
}
}
In the above code, we used button click events for performing tasks. We used following methods:
在上面的代碼中,我們使用了按鈕單擊事件來執行任務。 我們使用以下方法:
-
Listbox.Itmes.Add(text)
Listbox.Itmes.Add(文本)
-
Listbox.Itmes.RemoveAt(index)
Listbox.Itmes.RemoveAt(index)
-
Listbox.Itmes.Clear()
Listbox.Itmes.Clear()
We used some properties like:
我們使用了一些屬性,例如:
-
lstItem.Items.Count
lstItem.Items.Count
-
lstItem.SelectedIndex
lstItem.SelectedIndex
-
lstItem.SelectedItem
lstItem.SelectedItem
翻譯自: https://www.includehelp.com/dot-net/listbox-example-in-c-sharp.aspx
c# listbox使用