c# listbox使用_设计一个Windows应用程序以演示C#中ListBox的使用

c# listbox使用

Following operations are performing on the ListBox:

在ListBox上执行以下操作:

  1. Add

  2. Remove

    去掉

  3. Clear

    明确

  4. Get selected items

    获取所选项目

  5. 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):

示例(表单设计):

List Box Example in C#

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:

在上面的代码中,我们使用了按钮单击事件来执行任务。 我们使用以下方法:

  1. Listbox.Itmes.Add(text)

    Listbox.Itmes.Add(文本)

  2. Listbox.Itmes.RemoveAt(index)

    Listbox.Itmes.RemoveAt(index)

  3. Listbox.Itmes.Clear()

    Listbox.Itmes.Clear()

We used some properties like:

我们使用了一些属性,例如:

  1. lstItem.Items.Count

    lstItem.Items.Count

  2. lstItem.SelectedIndex

    lstItem.SelectedIndex

  3. lstItem.SelectedItem

    lstItem.SelectedItem

翻译自: https://www.includehelp.com/dot-net/listbox-example-in-c-sharp.aspx

c# listbox使用

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值