Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用单选按钮改进基本登录系统_C# - Fatal编程技术网

C# 使用单选按钮改进基本登录系统

C# 使用单选按钮改进基本登录系统,c#,C#,这是我的密码: public bool radioButtons() { if (!userRadioButton.Checked && !adminRadioButton.Checked) { MessageBox.Show("You must select an account type"); return false; } else {

这是我的密码:

public bool radioButtons()
    {
        if (!userRadioButton.Checked && !adminRadioButton.Checked)
        {
            MessageBox.Show("You must select an account type");
            return false;
        }
        else
        {
            return true;
        }
    }

现在,当用户名为空时,它会显示messagebox两次

我认为您想要实现的是这样的

public bool radioButtons()
{
    if (!userRadioButton.Checked && !adminRadioButton.Checked)
    {
        MessageBox.Show("You must select an account type");
        return false;
    }
    else
    {
        return true;
    }
}
在按钮点击事件中

public void button1_Click(object sender, EventArgs e)
{
    bool a = radioButtons();
    if (a)   
    {
        // a is true do what you want.
    }
}

你需要改变两件事:

  • 单选按钮
    方法不需要接受布尔值
  • 您应该首先声明
    a
  • 您的代码应该如下所示:

    public bool radioButtons()
    {
        if (!userRadioButton.Checked && !adminRadioButton.Checked)
        {
            MessageBox.Show("You must select an account type");
            return false;
        }
        else
        {
            return true;
        }
    }
    
    public void button1_Click(object sender, EventArgs e)
    {
    
        if (radioButtons())
        {
           //Your code here
        }
    }
    

    对不起,纠正一下。单选按钮(a)中的a和if(a==true)是红色下划线的d
    a
    全局变量?红色下划线的错误说明了什么?什么是
    a
    ?声明在哪里?我刚刚声明a为bool,单选按钮(a)仍然带下划线,表示使用未分配的局部变量a。基本上,我想让程序运行radioButtons函数来检查一个是否被选中,如果被选中,然后运行if语句中的代码来排序它!但是现在如果单选按钮未选中,则会显示两次消息框?按钮是否单击了两次?不,仅单击一次。当我单击register按钮时,消息框会显示,当您单击ok时,它会再次显示如果此代码帮助是您的,则您会接受此作为答案。谢谢