简体   繁体   中英

Use a “null” switch statement to determine if a range of TextBoxes are null, instead of using if-else statements

At the moment, I am trying to write a method that has inside it a switch statement which uses as the "trigger" the reserved word null. This is because I want all the TextBoxes, in this switch statement to be tested for the state of being null, and if they are, a MessageDialog (which I've coded for each box) will appear saying that 'You cannot leave "insert name of TextBox here", blank. You must enter text'

In short, is it possible to use a switch statement to test if a bunch of TextBoxes are null, rather than using a whole sequence of (unwieldy) if-else statements? If so, how? (please)

I should add that I've been at this problem for the last hour and a half, with no success.

Any help is gratefully accepted.

If you specifically want to use a Switch :

Make them into a List or an Array , if it is not that already.

List<string> TextBoxCollection = new List<string>();
TextBoxCollection.Add(yourTextBoxStringValue); //Do this for each of the boxes.

Then just iterate through the TextBoxCollection.

foreach (string textContent in TextBoxCollection)
{
    switch (textContent)
    {
        case null:
        {
            //Do the message error here.
            break;
        }
        case "otherRequirement":
        {
            //Do other stuff here.
            break;
        }
        case "anotherRequirement":
        case "oneMoreRequirement":
        {
            //Do different things here, maybe?
            break;
        }
    }
}

This will iterate through the collection, and then test each string from it.

I would not recommend doing this, unless all the text boxes' values have to be validated for multiple conditions, all the same , in which case this will look more tidy than an If Else for each string value.


If you want a quick one line solution to check this for each one, you can just use:

if (string.IsNullOrWhiteSpace(yourTextBox.Text)) { //Throw error here. }

You can do the following:

Create a method, which will loop over Controls recursively:

    public static void ForAllControls(this Control parent, Action<Control> action)
    {
        foreach (Control c in parent.Controls)
        {
            action(c);
            ForAllControls(c, action);
        }
    }

Create a method, which will validate the TextBox:

    public void validateTextBoxes(Control c)
    {
        if (c.GetType() == typeof(TextBox)) 
            if(TextBox.Text == "") MessageBox.Show("insert name of TextBox here")
    }

Call the method, like this:

    this.ForAllControls(c => {
            if (c.GetType() == typeof(TextBox)) validateTextBoxes(c);
        });

Or use ErrorProvider.SetError();

This may not be using switch but it will save you some lines.

public static void IsTxtNull(Textbox xTxt, string msg)
{
    if(string.IsNullOrEmpty(xTxt.Text)
        MessageBox.Show(msg);
}

Then call it like this

IsTxtNull(YourTxtBox1,"Message for YourTxtBox1");
IsTxtNull(YourTxtBox2,"Message for YourTxtBox2");
IsTxtNull(YourTxtBox3,"Message for YourTxtBox3");
etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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