简体   繁体   English

如果在 forms 中未选择所有选项,如何停止代码执行

[英]How to stop execution on code if all options are not selected in forms

I am creating a simple form that prompts the user with 2 radio box selections.我正在创建一个简单的表单,提示用户选择 2 个单选框。 1 of the selections is the color and the other is the form size.选项之一是颜色,另一个是表格大小。 I need to create a code that forces the user to choose both a color and size option or no changes will happen to the form.我需要创建一个代码,强制用户同时选择颜色和大小选项,否则表单不会发生任何变化。 How would I create this?我将如何创建这个? Example to help out.帮助的例子。

Sally checks blue but doesn't pick a form size and clicks the "Save form settings" button. Sally 选中蓝色,但没有选择表单大小并单击“保存表单设置”按钮。 It should not save anything to the form at all unless she chooses a form size.除非她选择表单大小,否则它根本不应该将任何内容保存到表单中。 And vise versa, if she chooses only a size but no color no changes should be executed unless all options are picked.反之亦然,如果她只选择尺寸但不选择颜色,则除非选择所有选项,否则不应执行任何更改。

private void button1_Click(object sender, EventArgs e)
        {
            // changes color of form
            if (radioButton1.Checked)
            {
                this.BackColor = Color.Blue;
            }
            else if (radioButton2.Checked)
            {
                this.BackColor = Color.Red;
            }
            else
            {
                this.BackColor = Color.Yellow;
            }
            // changes size of form
            if (radioButton3.Checked)
            {
                this.Size = new System.Drawing.Size(500, 500);
            }
            else if (radioButton4.Checked)
            {
                this.Size = new System.Drawing.Size(750, 750);
            }
            // executing changes for small and large blue form
            if (radioButton1.Checked && radioButton3.Checked)
            {
                textBox1.Text = "A Small, Blue form";
            }
            else if (radioButton1.Checked && radioButton4.Checked)
            {
                textBox1.Text = "A Large, Blue form";
            }
            // executing changes for small and large red form
            if (radioButton2.Checked && radioButton3.Checked)
            {
                textBox1.Text = "A Small, Red form";
            }
            else if (radioButton2.Checked && radioButton4.Checked)
            {
                textBox1.Text = "A Large, Red form";
            }
            // executing changes for small and large yellow form (i accidently named the yellow radio button "yellow" and it wouldn't let me change it so thats why it looks funny)
            if (Yellow.Checked && radioButton3.Checked)
            {
                textBox1.Text = "A Small, Yellow form";
            }
            else if (Yellow.Checked && radioButton4.Checked)
            {
                textBox1.Text = "A Large, Yellow form";
            }
            // code so both selections have to be made
            if (radioButton1.Checked == false)
            {

            }
            else if (radioButton2.Checked == false)
            {

            }
            else
            {

            }
            if (radioButton3.Checked == false)
            {

            }
            else
            {

            }
        }

If you have any tips on how to clean this up and make it better also please let me know.如果您对如何清理它并使其变得更好有任何提示,请告诉我。

One way to do this would be to capture the color and size separately into variables based on the values selected in the radio buttons, and then if no value is selected for a radio button group, use a default value that we can verify later.一种方法是根据单选按钮中选择的值将颜色和大小分别捕获到变量中,然后如果没有为单选按钮组选择任何值,则使用我们稍后可以验证的默认值。

For example, if we were going to set the color, we might do something like:例如,如果我们要设置颜色,我们可能会执行以下操作:

var color = Color.Empty;
if (rdoRed.Checked) color = Color.Red;
else if(rdoBlue.Checked) color = Color.Blue;
else if (rdoYellow.Checked) color = Color.Yellow;

By doing this, we know that if color == Color.Empty , then the user didn't select a color.通过这样做,我们知道如果color == Color.Empty ,那么用户没有 select 一个颜色。 We can apply the same logic with the Size property, and then we can use an if condition to verify that neither of the values are empty before changing the form size.我们可以对Size属性应用相同的逻辑,然后我们可以使用if条件来验证在更改表单大小之前两个值都不是空的。

The ternary operator ( ?: ) can help shorten our code: 三元运算符( ?: ) 可以帮助缩短我们的代码:

private void btnApplyChanges_Click(object sender, EventArgs e)
{
    // Set this to the value they chose, or 'Empty' if no choice was made
    var color = rdoBlue.Checked ? Color.Blue 
        : rdoYellow.Checked ? Color.Yellow 
        : rdoRed.Checked ? Color.Red 
        : Color.Empty;

    // Set this to the value they chose, or 'Empty' if no choice was made
    var size = rdoSmall.Checked ? new Size(500, 500) 
        : rdoLarge.Checked ? new Size(750, 750) 
        : Size.Empty;

    // If neither value is empty, then change our form properties
    if (color != Color.Empty && size != Size.Empty)
    {
        // If we get this far, we know size was selected. So we choose the word "Small"
        // if the height is 500, otherwise "Large" (there are better ways to do this)
        var sizeText = size.Height == 500 ? "small" : "large";
        txtStatus.Text = $"A {sizeText}, {color.Name} form";
        BackColor = color;
        Size = size;
    }
}

I'm not sure how you will handle all of these controls without using GroupBox .我不确定在不使用GroupBox的情况下如何处理所有这些控件。 If you use GroupBox to group all releated RadioButton controls, you can loop over them using the groupbox.如果您使用GroupBox对所有相关的RadioButton控件进行分组,则可以使用 groupbox 对它们进行循环。

Example:例子:

foreach(var control in groupbox.Controls)
{
    if(control is RadioButton)
    {
        var radio = control as RadioButton; 

        var name = radio.Name;

        if(radio.Checked) { ... }
    }

}

You can do the same thing using the main Form as the container, but this would loop over all the form controls, which wouldn't be a good idea.您可以使用主Form作为容器来执行相同的操作,但这会遍历所有窗体控件,这不是一个好主意。

Secondly, you can define private methods that would handle some parts of the code, which would make things easier to handle like a method to set the size, another to set a color, another to get the message for a chosen size, and similarly you do the same for the color.其次,您可以定义private方法来处理代码的某些部分,这将使事情更容易处理,例如设置大小的方法,设置颜色的方法,获取所选尺寸的消息的方法,类似地您对颜色做同样的事情。 In the end, you'll only pass a value to these methods, and then you process the results as you want.最后,您只需将值传递给这些方法,然后根据需要处理结果。

here is an example:这是一个例子:

private System.Drawing.Size GetSize(string radioButtonName)
{
    switch(radioButtonName)
    {
        case "radioButton3":
            return new System.Drawing.Size(500, 500);
        case "radioButton4":
            return new System.Drawing.Size(750, 750);
        default: // set a default size
            return Size.Empty;
    }   
}

private Color GetColor(string radioButtonName)
{
    switch(radioButtonName)
    {
        case "radioButton1":
            return Color.Blue;
        case "radioButton2":
            return Color.Red;
        case "Yellow":
            return Color.Yellow;            
        default: // set a default color
            return Color.Empty;
    }   
}

private string GetSizeTextMessage(int width)
{
    switch(width)
    {
        case 500:
            return "Small";
        case 750:
            return "Large";
        default: 
            return null;
    }   
}

Now, with this, you can add or adjust any settings you want to them.现在,有了这个,您可以添加或调整您想要的任何设置。 Then, in your main click event, you can do something like this:然后,在您的主点击事件中,您可以执行以下操作:

string sizeRadioName; 
string colorRadioName;

// changes color of form
if (radioButton1.Checked)
{
    colorRadioName = radioButton1.Name;
}
else if (radioButton2.Checked)
{
    colorRadioName = radioButton2.Name;
}

// changes size of form
if (radioButton3.Checked)
{
    sizeRadioName = radioButton3.Name;
}
else if (radioButton4.Checked)
{
    sizeRadioName = radioButton4.Name
}

if(size != Size.Empty && color != Color.Empty) { 

    this.Size  = GetSize(sizeRadioName);
    this.BackColor = GetColor(colorRadioName);
    textBox1.Text = $"A {GetSizeTextMessage(this.Size.Width)} , {this.BackColor.Name} form";        
}   

this is for your current implementation, if you used a GroupBox it would changed to something like this:这是针对您当前的实现,如果您使用GroupBox ,它将更改为如下所示:

Size size; 
Color color; 

foreach(var control in groupBoxExample.Controls)
{
    if(control.GetType() != typeof(RadioButton)) { continue; }

    var radio = control as RadioButton; 

    var name = radio.Checked ? radio.Name : null;

    if(name == null) { continue; }

    size  = GetSize(name);

    color = GetColor(name);
}

if(size != Size.Empty && color != Color.Empty) { 

    this.Size  = size;
    this.BackColor = color;
    textBox1.Text = $"A {GetSizeTextMessage(this.Size.Width)} , {this.BackColor.Name} form";        
}       

As you can see in this, we just put all the radios inside a group box, and I looped over them.正如您在此看到的,我们只是将所有收音机放在一个组框内,然后我循环播放它们。

I hope this would help you out.我希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM