Windows窗体应用程序 button与radiobutton的小练习(C#)
选择左边的按钮,改变将要弹出信息框下面的按钮选项,选择右边的按钮改变信息框文字前的图标。
下面的图片是示范
代码如下:
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 Button控件和Radio控件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private MessageBoxIcon iconType;
private MessageBoxButtons buttonType;
//通过sender在单选按钮的基础上改变按钮的类型
private void buttonType_CheckedChange(object sender, EventArgs e)
{
if (sender == radioButton1)
buttonType = MessageBoxButtons.OK;
else if (sender == radioButton2)
buttonType = MessageBoxButtons.OKCancel;
else if (sender == radioButton3)
buttonType = MessageBoxButtons.AbortRetryIgnore;
else if (sender == radioButton4)
buttonType = MessageBoxButtons.YesNoCancel;
else if (sender == radioButton5)
buttonType = MessageBoxButtons.YesNo;
else
buttonType = MessageBoxButtons.RetryCancel;
}
//通过sender在单选按钮的基础上改变图标的类型
private void iconType_CheckChange(object sender, EventArgs e)
{
if (sender == radioButton7)
iconType = MessageBoxIcon.Asterisk;
else if (sender == radioButton8)
iconType = MessageBoxIcon.Error;
else if (sender == radioButton9)
iconType = MessageBoxIcon.Exclamation;
else if (sender == radioButton10)
iconType = MessageBoxIcon.Hand;
else if (sender == radioButton11)
iconType = MessageBoxIcon.Information;
else if (sender == radioButton12)
iconType = MessageBoxIcon.Question;
else if (sender == radioButton13)
iconType = MessageBoxIcon.Stop;
else
iconType = MessageBoxIcon.Warning;
}
//显示信息框以及信息框里被选中的按钮
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("This if your custom messagebox", "custom messagebox",buttonType,iconType,0,0);
switch (result)
{
case DialogResult.OK:
label1.Text = "ok is pressed";
break;
case DialogResult.Cancel:
label1.Text = " cancel is pressed";
break;
case DialogResult.Abort:
label1.Text = "abort is pressed";
break;
case DialogResult.Retry:
label1.Text = "retry is pressed";
break;
case DialogResult.Ignore:
label1.Text = "ignore is pressed";
break;
case DialogResult.Yes:
label1.Text = "yes is pressed";
break;
case DialogResult.No:
label1.Text = "no is pressed";
break;
}
}
}
}
这里要说一下DialogResult ,DialogResult 是Form类的一个属性, 一般对话框上会有:确定,取消两个按钮。
点击确定,会设置 DialogResult =DialogResult.OK ;
点击取消,会设置 DialogResult =DialogResult.Cancel ;
然后在主进程根据 DialogResult 来判断用户是点了确定还是取消,
并根据这个返回值,决定下面的操作。
这个DialogResult属性来自 BadEgger 的**** 博客 ,全文地址请点击:https://blog.****.net/qq_36196748/article/details/78727412?utm_source=copy