[Yugong Series] Detailed Explanation of ComboBox Controls on Winform Controls in September 2023


foreword

Winform controls are user interface elements in Windows Forms. They can be used to create various visual and interactive components of Windows applications, such as buttons, labels, text boxes, drop-down list boxes, check boxes, radio boxes, progress bars, etc. . Developers can use Winform controls to build user interfaces and respond to user actions to create powerful desktop applications.

1. Detailed Explanation of ComboBox Control

ComboBox is a common control in Winform (Windows Forms), which allows the user to select one of the options from a list of predefined options.

Use a ComboBox control to allow users to select an option without requiring manual input or selection of other types of controls. It can be used as an input control or as a menu control.

The ComboBox control can be used in Winform through the following steps:

Step 1: Drag the ComboBox control into the form.

Step 2: Set the following properties in the properties window of the ComboBox:

  • Items: Specify the list of options to be displayed in the ComboBox.

  • SelectedIndex: Specifies the index selected by the ComboBox. The default is -1, meaning no option is selected.

  • DropDownStyle: Specifies the style displayed by the ComboBox.

  • Text: Specifies the currently selected text value in the ComboBox.

Step 3: Use ComboBox related events and methods in the code.

For example, you can use the SelectedIndexChanged event to retrieve the options selected by a ComboBox, and use the Add method to add options to the ComboBox.

The following is an example code using the ComboBox control:

private void Form1_Load(object sender, EventArgs e)
{
    
    
    // 添加选项列表
    comboBox1.Items.Add("选项1");
    comboBox1.Items.Add("选项2");
    comboBox1.Items.Add("选项3");

    // 初始选择第一个选项
    comboBox1.SelectedIndex = 0;
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    
    
    // 获取当前选择的文本值
    string selectedText = comboBox1.Text;

    // 显示消息框
    MessageBox.Show("您选择了:" + selectedText);
}

private void button1_Click(object sender, EventArgs e)
{
    
    
    // 添加新的选项
    comboBox1.Items.Add("新选项");

    // 选择新添加的选项
    comboBox1.SelectedIndex = comboBox1.Items.Count - 1;
}

insert image description here

1. Attribute introduction

1.1 DropDownWidth

The DropDownWidth property of the ComboBox control in Winform specifies the width of the drop-down list box. By default, the drop-down list box has the same width as the ComboBox control. However, in some cases it may be necessary to change the width of the drop-down list box to accommodate longer option text or more options.

To use the DropDownWidth property, you can follow these steps:

Step 1: Drag and drop the ComboBox control onto the form.

Step 2: In the properties window of the ComboBox, set the DropDownStyle property to DropDownList or DropDown.

Step 3: Set the DropDownWidth property to the desired width.

For example, the following code sets the DropDownWidth of the ComboBox control to 200 pixels:

comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.DropDownWidth = 200;

In the above code, the DropDownStyle property is first set to DropDownList so that the user can only select options from the drop-down list. Then, set the DropDownWidth property to 200 pixels so that the width of the drop-down list is 200 pixels.

Note: If the DropDownStyle property is set to DropDown, the ComboBox control will automatically complete text as the user types, and the DropDownWidth property will have no effect.

The use scenarios of the DropDownWidth property include that when the option text in the ComboBox control is wider than the width of the ComboBox control, you can use the DropDownWidth property to adjust the width of the drop-down list for better viewing and selection of options.
insert image description here

1.2 MaxDropDownItems和IntegralHeight

The ComboBox control is one of the commonly used controls in Winform, which is used to display data items available for selection in the drop-down list. The MaxDropDownItems property is used to set the maximum number of data items that can be displayed in the drop-down list.

By setting the MaxDropDownItems property, you can limit the number of data items displayed in the drop-down list to prevent the interface from being cluttered due to the drop-down list being too large. When there are more data items in the drop-down list, you can use the scroll bar to scroll through them.

Sample code:

//设置ComboBox控件的MaxDropDownItems属性为5
comboBox1.MaxDropDownItems = 5;
comboBox1.IntegralHeight=false;

When setting this value, the property of IntegralHeight must be set to false, and DropDownHeight must be the default value of 106. If the number of data items in the drop-down list exceeds 5, a scroll bar will appear to view all data items.

insert image description here

1.3 DropDownStyle

The ComboBox control is one of the commonly used controls in Winform. It can provide a drop-down list among multiple options for users to choose. The DropDownStyle property is a property of the ComboBox control, which is required in many scenarios when using the ComboBox control.

The DropDownStyle property is an enumeration type of the ComboBox control, and there are three optional values:

  1. DropDown: This is the default value. At this time, the drop-down list of the ComboBox control is displayed in an expanded form, and the user can manually click the drop-down list to select the option to be displayed.

  2. DropDownList: At this time, the ComboBox control drop-down list is displayed in an expanded form, but the user cannot enter or edit the option content in the drop-down list, and can only choose one option from it.

  3. Simple: At this time, the drop-down list of the ComboBox control is only expanded when the user clicks the button of the ComboBox control, and there is only one row, and cannot be scrolled or resized.

scenes to be used:

  1. When the number of options of the ComboBox control is small, the DropDownStyle can be used as the DropDownList, so that the user can only choose from the drop-down list to avoid user error input.

  2. When the number of options of the ComboBox control is large, the DropDownStyle can be used as DropDown, so that the user can scroll the drop-down list for better search and selection.

  3. When the ComboBox control only needs to provide a drop-down option, you can use the DropDownStyle as Simple to make the interface simple and beautiful.

Sample code:

// 设置ComboBox控件的DropDownStyle属性
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox2.DropDownStyle = ComboBoxStyle.DropDown;
comboBox3.DropDownStyle = ComboBoxStyle.Simple;

1.4 DrawMode

The ComboBox control is one of the very commonly used controls in Winform. It provides a drop-down menu list from which the user can select one or more items. The DrawMode property of the ComboBox control is used to set the drawing mode of the ComboBox control. The drawing modes in the ComboBox control are divided into the following two types:

  1. Normal: In Normal mode, each item in the ComboBox control is drawn by the ComboBox control itself. By default, the value of the DrawMode property of the ComboBox control is Normal.

  2. OwnerDrawFixed: In OwnerDrawFixed mode, each item in the ComboBox control must be drawn manually by the programmer. This mode can be used to achieve specific drawing effects, such as customizing the color, font, icon, etc. of the drop-down list items.

Using the DrawMode property to set the drawing mode of the ComboBox control is very simple. At design time or runtime, you only need to set the DrawMode property of the ComboBox control to Normal or OwnerDrawFixed. For example, you can add the following code in the Form_Load event to set the drawing mode of the ComboBox control to OwnerDrawFixed:

private void Form_Load(object sender, EventArgs e)
{
    
    
    comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
}

In OwnerDrawFixed mode, the DrawItem event of the ComboBox control will be triggered. In this event, each item in the ComboBox control can be drawn manually. For example, you can manually draw items in a ComboBox control with the following code:

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    
    
    e.DrawBackground();
    e.DrawFocusRectangle();
    Brush brush = Brushes.Black;
    if (e.Index % 2 == 0) brush = Brushes.Red;
    e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), e.Font, brush, e.Bounds);
}

In the above code, we manually draw each item in the ComboBox control. Here, we set the odd rows in the ComboBox control to be red and the even rows to be black. In actual development, you can modify the drawing code according to your needs to achieve a custom drop-down list item effect.

1.5 SelectedItem和SelectedIndex

ComboBox is one of the commonly used controls in Windows Forms, which is used to display optional items in a drop-down list. The ComboBox control contains two important properties, namely SelectedItem and SelectedIndex.

SelectedItem is the value of the currently selected item in the ComboBox control. For example, if there are three options "Monday", "Tuesday" and "Wednesday" in the ComboBox control, and the user selects "Tuesday", the SelectedItem property will return the string "Tuesday".

The SelectedIndex property is the index number in the list of the currently selected item in the ComboBox control. For example, if there are three options "Monday", "Tuesday" and "Wednesday" in the ComboBox control, and the user selects "Tuesday", the SelectedIndex property will return 1, indicating the second item in the list.

The following is the sample code of the ComboBox control:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    
    
    // 获取当前选择项的值
    string selectedItem = comboBox1.SelectedItem.ToString();

    // 获取当前选择项的索引号
    int selectedIndex = comboBox1.SelectedIndex;

    // 在Label控件中显示当前选择项
    label1.Text = "您选择了:" + selectedItem + ",索引号为:" + selectedIndex;
}

In this example, the values ​​of the SelectedItem and SelectedIndex properties are obtained in the SelectedIndexChanged event of the ComboBox, and the information of the currently selected item is displayed in the Label control.

2. Common scenarios

The ComboBox control is a very commonly used control in Winform, it can be used in a variety of scenarios:

  1. Data selection: ComboBox can be used to allow users to select one item from a set of predefined data items, such as countries, provinces, cities, etc. Users can select an option via a drop-down list.

  2. Input prompt: ComboBox can also be used for input prompt. When the user enters text, the drop-down list will automatically filter out the options that match the input. The user can select an option or continue to enter.

  3. Auto-completion: ComboBox can also support auto-completion input like an input box. When the user enters text, the drop-down list will automatically pop up and list all possible options. The user can select an option through the keyboard or mouse.

  4. Hierarchical selection: ComboBox can also be used to implement hierarchical selection. For example, in a complex data structure, users can select a certain level of data through the drop-down list, and then continue to select the next level of data, and so on.

3. Specific cases

The following is a specific case of the ComboBox control in Winform:

Suppose we want to develop a student performance management system, which requires a drop-down box control to select subjects. We can use the ComboBox control to achieve.

  1. You can drag the ComboBox control in the window, or dynamically create the ComboBox object in the code.

  2. In the window's Load event, use the following code to populate the ComboBox control's options:

private void Form1_Load(object sender, EventArgs e)
{
    
    
    // 添加学科选项到ComboBox控件
    comboBox1.Items.Add("数学");
    comboBox1.Items.Add("语文");
    comboBox1.Items.Add("英语");
    comboBox1.Items.Add("物理");
    comboBox1.Items.Add("化学");

    // 默认选择第一个选项
    comboBox1.SelectedIndex = 0;
}
  1. Add a button in the window, when the user selects a subject, click the button to display the grades of the subject.
private void button1_Click(object sender, EventArgs e)
{
    
    
    // 获取用户选择的学科
    string subject = comboBox1.Text;

    // 根据选项获取对应学科的成绩,并显示在窗口中
    switch (subject)
    {
    
    
        case "数学":
            textBox1.Text = "数学成绩:90";
            break;
        case "语文":
            textBox1.Text = "语文成绩:95";
            break;
        case "英语":
            textBox1.Text = "英语成绩:85";
            break;
        case "物理":
            textBox1.Text = "物理成绩:80";
            break;
        case "化学":
            textBox1.Text = "化学成绩:88";
            break;
    }
}

In this way, when the user selects a subject, the results of the subject can be displayed by clicking the button.

insert image description here

Guess you like

Origin blog.csdn.net/aa2528877987/article/details/132657676