如何选择组框中的单选按钮(C#)?

时间:2018-03-15 08:03:39

标签: c#

我有一组收音机按钮。我可以为每个控制器创建一个方法,为每个控制器添加新的System.EventHandler但是在Deitel的书中他只创建了一个没有任何事件处理程序的方法,并使用sender选择一个单选按钮,但我不知道如何调用这个方法!我复制了visual studio中的所有代码,但它没有用。你能救我吗?

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 WindowsFormsApplication3
{
    public partial class Form1 : Form
    {

        private MessageBoxIcon iconType;
        private MessageBoxButtons buttonType; 

        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {


        }

        private void buttonType_CheckedChanged(object sender, EventArgs e) {

            if (sender == OKRadioBtn) buttonType = MessageBoxButtons.OK;
            else if (sender == OKCancelRadioBtn)  buttonType = MessageBoxButtons.OKCancel;  
            else if (sender == AbortRetryIgnoreRadioBtn) buttonType = MessageBoxButtons.AbortRetryIgnore;
            else if (sender == YesNoRadioBtn) buttonType = MessageBoxButtons.YesNoCancel;
            else if (sender == YesNoCancelRadioBtn) buttonType = MessageBoxButtons.YesNoCancel;
            else if (sender == RetryCancelRadioBtn) buttonType = MessageBoxButtons.RetryCancel;

        }
        private void iconType_ChekedChanged(object sender, EventArgs e) {

            if (sender == AsteriskRadioBtn) iconType = MessageBoxIcon.Asterisk;
            else if (sender == ErrorRadioBtn) iconType = MessageBoxIcon.Error;
            else if (sender == ExclamationRadioBtn) iconType = MessageBoxIcon.Exclamation;
            else if (sender == HandRadioBtn) iconType = MessageBoxIcon.Hand;
            else if (sender == InformationRadioBtn) iconType = MessageBoxIcon.Information;
            else if (sender == QuestionRadioBtn) iconType = MessageBoxIcon.Question;
            else if (sender == StopRadioBtn) iconType = MessageBoxIcon.Stop;
            else if (sender == WarningRadioBtn) iconType = MessageBoxIcon.Warning;
        }

        private void DisplayBTN_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("This is your Custom MessageBox.", "Custom Messagebox", buttonType, iconType);

            switch (result) { 

                case DialogResult.OK :
                    DisplayLable.Text = "Ok was pressed";
                    break;
                case DialogResult.Cancel:
                    DisplayLable.Text = "Cancel was pressed";
                    break;
                case DialogResult.Abort:
                    DisplayLable.Text = "Abort was pressed";
                    break;
                case DialogResult.Retry:
                    DisplayLable.Text = "Retry was pressed";
                    break;
                case DialogResult.Ignore:
                    DisplayLable.Text = "Ignore was pressed";
                    break;
                case DialogResult.Yes:
                    DisplayLable.Text = "Yes was pressed";
                    break;
                case DialogResult.No:
                    DisplayLable.Text = "No was pressed";
                    break;
            }
        }


    }
}

0 个答案:

没有答案