天天看點

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): 示例(表單設計):
c# listbox使用_設計一個Windows應用程式以示範C#中ListBox的使用
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使用