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 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/qq_36196748/article/details/78727412?utm_source=copy


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值