Resolved Dual Validation on a textbox entry

ConsKa

Well-known member
Joined
Dec 11, 2020
Messages
140
Programming Experience
Beginner
I am wondering whether there is a better way of doing this.

I have an inputbox where a user enters a Unique ID that UID is put into 1 of 12 saveboxes.

The user can recall the data from the 12 saveboxes.

The test I want to run is:

Is the UID in the inputbox equal to the any of the saveboxes - if yes - move on to rest of code.

If the inputbox is not equal to the savebox - AND - there are no empty saveboxes. Then send a message.

This is the code that I have that is working - credit to jmcilhinney who gave me the baseline code for another task.

C#:
var x = 0;
            for (var i = 1; i < 13; i++)
            {
                var controlNumber = i;

                if (panel1.Controls[$"HoldSD{controlNumber}"].Text != sdNobox.Text)
                {
                    x = x + 1;
                }
            }
            for (var i = 1; i < 13; i++)
            {
                var controlNumber = i;

                if (panel1.Controls[$"HoldSD{controlNumber}"].Text != "")
                {
                    x = x + 1;
                }
                if (x >= 24)
                {
                    MessageBox.Show("Resolve");
                    return;
                }
            }


I tried really hard to get this into a single check, using &&, || boolean variables....I tried a lot of things, but this is the only one I could get to work.

I am happy with it, it does the job, but I think, perhaps there is an opportunity to learn something new?

edit, i know, should probably just be x++; it's late and I been working on this for hours....
 
C#:
var saveBox = Enumerable.Range(1, 12)
                        .Select(n => panel1.Controls[$"HoldSD{n}"])
                        .FirstOrDefault(c => c.Text == string.Empty || c.Text == sdNobox.Text);

if (saveBox == null)
{
    // There is no match and there is no empty save box.
    // Display message here.
  
    return;
}

if (saveBox.Text == string.Empty)
{
    saveBox.Text = sdNobox.Text;
}
That creates an enumerable list of controls with the appropriate names and finds the first that is either empty or has a matching value, if there is one. If there's no match then the method ends there. If there is an empty match then it is populated with the current value. Subsequent code will then be executed whether the match was previously empty or not.
 
Last edited:
Is the UID in the inputbox equal to the any of the saveboxes - if yes - move on to rest of code.

If the inputbox is not equal to the savebox - AND - there are no empty saveboxes. Then send a message.
Seems like you already had the logic figured when you were describing what you needed. See how this code below lines up with your description:
C#:
var saveBoxes = Enumerable.Range(1, 12)
                                              .Select(n => panel1.Controls[$"HoldSD{n}"]);

if (saveBoxes.Any(c => c.Text == sdNoBox.Text))
{
    // move on to the rest of the code
}
else if (saveBoxes.All(c => c.Text != sdNoBox.Text && c.Text != string.Empty))
{
    // send a message
}

The code can be further simplified to:
C#:
var saveBoxes = Enumerable.Range(1, 12)
                                              .Select(n => panel1.Controls[$"HoldSD{n}"]);

if (saveBoxes.Any(c => c.Text == sdNoBox.Text))
{
    // move on to the rest of the code
}
else if (saveBoxes.All(c => c.Text != string.Empty))
{
    // send a message
}

Note that on line 8, there is no need to do:
C#:
saveBoxes.All(c => c.Text != sdNoBox.Text && c.Text != string.Empty)
because line 4 had already checked if at least one save box contains a match to your other input box. If there are no matches, then the code will branch to line 8. So all that line 8 really needs to do is to check if all the save boxes are not empty.
 
Back
Top Bottom