本文共 2484 字,大约阅读时间需要 8 分钟。
c# listbox使用
Following operations are performing on the ListBox:
在ListBox上执行以下操作:
Add
加
Remove
去掉
Clear
明确
Get selected items
获取所选项目
etc...
等等...
Follow controls are using in the application:
在应用程序中使用以下控件:
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 (按钮):清除完整列表。
Example (form design):
示例(表单设计):
C# Source Code:
C#源代码:
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
翻译自:
c# listbox使用
转载地址:http://ecozd.baihongyu.com/