WinForm Application Practical Development Guide - Tips for Assignment of Check Box Controls

In the development of WinForm programs, I have some tips for assigning and obtaining values ​​for check boxes. Let me share and discuss them.

PS: I recommend an interface component that can be used in C# development - DevExpress WinForms , which can perfectly build smooth, beautiful and easy-to-use applications, whether it is an Office-style interface or analyze and process large quantities of business data. Can do it with ease!

Click to get the official version of DevExpress v23.1 (Q technical exchange: 523159565)

The application scenario is like this, if you need to use check boxes to present content, as shown in the following figure:

WinForm Application Practical Development Guide - Tips for Assignment of Check Box Controls

The content of the cut part above is to place multiple CheckBoxes in the GroupBox; in fact, this part can also use the CheckedListBox control of the Winform control to present the content, as shown below.

WinForm Application Practical Development Guide - Tips for Assignment of Check Box Controls

No matter what kind of control is used, we will have the trouble of assigning it. I have encapsulated a function here, which can easily assign values ​​to the control. The general code is as follows.

CheckBoxListUtil.SetCheck(this.groupRemove, info.切除程度);

So how to get the content code of the control, the code is as follows:

info.切除程度 = CheckBoxListUtil.GetCheckedItems(this.groupRemove);

Assignment and retrieval are called through the encapsulation function, which is very simple and can be reused. The encapsulation method function is as follows.

public class CheckBoxListUtil
{
/// <summary>
/// 如果值列表中有的,根据内容勾选GroupBox里面的成员.
/// </summary>
/// <param name="group">包含CheckBox控件组的GroupBox控件</param>
/// <param name="valueList">逗号分隔的值列表</param>
public static void SetCheck(GroupBox group, string valueList)
{
string[] strtemp = valueList.Split(',');
foreach (string str in strtemp)
{
foreach (Control control in group.Controls)
{
CheckBox chk = control as CheckBox;
if (chk != null && chk.Text == str)
{
chk.Checked = true;
}
}
}
}

/// <summary>
/// 获取GroupBox控件成员勾选的值
/// </summary>
/// <param name="group">包含CheckBox控件组的GroupBox控件</param>
/// <returns>返回逗号分隔的值列表</returns>
public static string GetCheckedItems(GroupBox group)
{
string resultList = "";
foreach (Control control in group.Controls)
{
CheckBox chk = control as CheckBox;
if (chk != null && chk.Checked)
{
resultList += string.Format("{0},", chk.Text);
}
}
return resultList.Trim(',');
}

/// <summary>
/// 如果值列表中有的,根据内容勾选CheckedListBox的成员.
/// </summary>
/// <param name="cblItems">CheckedListBox控件</param>
/// <param name="valueList">逗号分隔的值列表</param>
public static void SetCheck(CheckedListBox cblItems, string valueList)
{
string[] strtemp = valueList.Split(',');
foreach (string str in strtemp)
{
for (int i = 0; i < cblItems.Items.Count; i++)
{
if (cblItems.GetItemText(cblItems.Items[i]) == str)
{
cblItems.SetItemChecked(i, true);
}
}
}
}

/// <summary>
/// 获取CheckedListBox控件成员勾选的值
/// </summary>
/// <param name="cblItems">CheckedListBox控件</param>
/// <returns>返回逗号分隔的值列表</returns>
public static string GetCheckedItems(CheckedListBox cblItems)
{
string resultList = "";
for (int i = 0; i < cblItems.CheckedItems.Count; i++)
{
if (cblItems.GetItemChecked(i))
{
resultList += string.Format("{0},", cblItems.GetItemText(cblItems.Items[i]));
}
}
return resultList.Trim(',');
}

}

The above code is divided into two parts, one is to operate the GroupBox control group, and the second is to operate the CheckedListBox control.

In this way, it is more convenient when making check boxes. For example, I adopt the first GroupBox control group method, and the interface for checking according to the content is as follows.

WinForm Application Practical Development Guide - Tips for Assignment of Check Box Controls

Apply the above auxiliary class function, if you use the GroupBox scheme, you can just drag a few CheckBox controls into it, and it is not necessary to give him a meaningful name, because no matter it is Zhang San or Li Si, as long as There is no problem with its parent being GroupBox.

This article is reproduced from: Blog Garden - Wu Huacong

Guess you like

Origin blog.csdn.net/AABBbaby/article/details/132555675