消息框MessageBox+遍历控件

时间:2022-11-23 20:58:02

消息对话框:主要用来显示信息,也可以警告、用户确认取消等。

MessageBox.Show("展示内容","标题",MessageBoxButtons.按钮种类,MessageBoxIcon.图标);

消息框MessageBox+遍历控件

        private void button1_Click(object sender, EventArgs e)
{
string str = "您选择的颜色如下:";
foreach (Control c in this.Controls)//foreach遍历控件
{
if (c.GetType().Name == "CheckBox")
{
CheckBox cb=(CheckBox)c; //将控件类(大类)转为CheckBox类(小类)
if (cb.Checked)
str += "\n" + cb.Text;
}
}
MessageBox.Show(str);
}