📜  C#| ListBox类别

📅  最后修改于: 2021-05-29 15:19:37             🧑  作者: Mango

在Windows窗体中,ListBox控件用于显示列表中的多个元素,用户可以从中选择一个或多个元素,并且这些元素通常显示在多列中。 ListBox类用于表示Windows列表框,还提供不同类型的属性,方法和事件。它在System.Windows.Forms命名空间下定义。 ListBox类包含三种不同类型的集合类,即

  • ListBox.ObjectCollection:此类包含ListBox控件中包含的所有元素。
  • ListBox.SelectedObjectCollection:此类保存所选项目的集合,该项目是ListBox控件中包含的项目的子集。
  • ListBox.SelectedIndexCollection:此类包含选定索引的集合,该集合是ListBox.ObjectCollection的索引的子集,并且这些索引指定了选定的元素。

在C#中,您可以使用两种不同的方法在Windows窗体中创建一个ListBox:

1.设计时:这是创建ListBox的最简单方法,如以下步骤所示:

  • 第1步:创建一个Windows窗体,如下图所示:
    Visual Studio->文件->新建->项目-> WindowsFormApp
  • 步骤2:接下来,将ListBox控件从工具箱拖放到窗体。

  • 步骤3:拖放之后,您将转到ListBox控件的属性,以根据需要修改ListBox。

    输出:

2.运行时:比上述方法有些棘手。在此方法中,可以借助ListBox类提供的语法以编程方式创建ListBox控件。以下步骤显示了如何动态设置创建列表框:

  • 步骤1:使用ListBox类提供的ListBox()构造函数创建ListBox控件。
    // Creating a ListBox control
    ListBox mylist = new ListBox(); 
    
  • 步骤2:创建ListBox控件后,设置ListBox类提供的ListBox控件的属性。
    ListBox mylist = new ListBox(); 
            mylist.Location = new Point(287, 109); 
            mylist.Size = new Size(120, 95); 
            mylist.ForeColor = Color.Purple; 
            mylist.Items.Add(123); 
            mylist.Items.Add(456); 
            mylist.Items.Add(789);
    
  • 步骤3:最后,使用以下语句将此ListBox控件添加到表单中:
    // Adding ListBox control 
    // to the form 
    this.Controls.Add(mylist);
    

    例子:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
      
    namespace WindowsFormsApp25 {
      
    public partial class Form1 : Form {
      
        public Form1()
        {
            InitializeComponent();
        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
      
            // Creating and setting the
            // properties of ListBox
            ListBox mylist = new ListBox();
            mylist.Location = new Point(287, 109);
            mylist.Size = new Size(120, 95);
            mylist.ForeColor = Color.Purple;
            mylist.Items.Add(123);
            mylist.Items.Add(456);
            mylist.Items.Add(789);
      
            // Adding ListBox control
            // to the form
            this.Controls.Add(mylist);
        }
    }
    }
    

    输出:

建设者

Constructor Description
ListBox() This Constructors is used to initialize a new instance of the ListBox class.

特性

Property Description
AutoSize This property is used to get or set a value that indicates whether the control resizes based on its contents.
BackColor This property is used to get or set the background color for the control.
BorderStyle This property indicates the border style for the control.
Font This property is used to get or set the font of the text displayed by the control.
ForeColor This property is used to get or set the foreground color of the control.
Height This property is used to get or set the height of the control.
Location This property is used to get or set the coordinates of the upper-left corner of the ListBox control relative to the upper-left corner of its form.
Name This property is used to get or set the name of the control.
TabStop This property is used to get or set a value that shows whether the user can press the TAB key to provide the focus to the ListBox.
Size This property is used to get or set the height and width of the control.
Text This property is used to get or set the text to be displayed in the RichTextBox control.
Visible This property is used to get or set a value indicating whether the control and all its child controls are displayed.
Width This property is used to get or set the width of the control.
ColumnWidth This property is used to get or set the width of columns in a multicolumn ListBox.
HorizontalExtent This property is used to get or set the width by which the horizontal scroll bar of a ListBox can scroll.
ItemHeight This property is used to get or set the height of an item in the ListBox.
Items This property is used to get the items of the ListBox.
PreferredHeight This property is used to get the combined height of all items in the ListBox.
SelectedIndex This property is used to get or set the zero-based index of the currently selected item in a ListBox.
SelectedItem This property is used to get or set the currently selected item in the ListBox.
SelectedIndices This property is used to get a collection that contains the zero-based indexes of all currently selected items in the ListBox.
Sorted This property is used to get or set a value indicating whether the items in the ListBox are sorted alphabetically.
TopIndex This property is used to get or set the index of the first visible item in the ListBox.