Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm needing to check if any of a bunch of textboxes are empty, (they can be 5 or 50 or 500 depending on the situation; tho this is irrelevant).

I wrote a simple method to check if a textbox is empty:
C#
private bool IsEmpty(TextBox checkthis)
{
    if (checkthis.Text.Length == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}


This, however, requires me to do something like this:
C#
if(IsEmpty(txtBxFirstName) || IsEmpty(txtBxLastName) || IsEmpty(txtBxDOB))
{
    Something();
}


It's no problem manually typing a few, but the way this is shaping up to be, I need something more versatile.

What I want to do is to have anything from 1 object to hundreds of objects as parameters in some equivalent method.

Anybody got any ideas??
Posted
Comments
Sergey Alexandrovich Kryukov 29-May-14 0:18am    
The method IsEmpty is horrific: it demonstrates that you don't understand Boolean and conditions; it could be just

bool IsEmpty(TextBox textBox) { return textBox.Text.Length == 0; }
Or perhaps textBox.Text.Trim().Length == 0.

All the "ifs" just make no sense. And the whole question... (sigh...) Programming is all about abstraction...

—SA
Frank R. Haugen 30-May-14 9:34am    
Well, I had the need for something doing it, and I did it quick and dirty ;-)

But you are right, I don't know the intricacies of of every facet of this kind of thing, which is why I ask.

But to be fair, you are one of the most, (if not THE most), respected member of this community, so compared to you I'm clueless.

I have had an enormously bad formal education in software development, and most my experience has been PHP and VB6 -related. I am getting there, but please be patient with me and those like me, who ask because we don't understand, but are attempting to improve ourselves.

You really come off snide and condescending in this comment
Sergey Alexandrovich Kryukov 30-May-14 10:32am    
Well, that I can understand, of course. As soon as someone is ready to do the job and all those small steps patiently, I can be very patient, not problems with that...
—SA
Sergey Alexandrovich Kryukov 30-May-14 10:54am    
I provided some more ideas in Solution 3, please see.
—SA

In addition to Solution 1, which needs to be improved:

Simpler, you can have a set of text boxes or other controls collected in a regular array:
C#
TextBox[] textBoxes = new TextBox[] { txtBxFirstName, txtBxLastName, txtBxDOB, };
//and then, for example
foreach(TextBox texBox in textBoxes) { /* whatever... */ } 

You need to create a textBoxes object only if you are going to use it elsewhere, in about loop or LINQ, otherwise you would create the array right in the loop. The set is variable during the development, but is constant during run-time, so using a collection would be redundant.

But this would only look fine if you have not too many controls in a list/collection. If there are too many, it will became unmanageable, and the root cause would be: why would you create all those controls in a designer at all? So many mistakenly think that the designer is a kind of automation of development, but in fact it can be just a lot of manual work, bad for maintenance. If you have a big stack of buttons, putting them in a designer, taking care of alignment and other things would be just silly. Writing XAML by hand would be faster (copy and past ;-)), but not more reasonable. Main thing is: you don't really need individual names for so many buttons, you rather need to address then all via an array object like textBoxes. You rather need to create then in a loop in first place. Something like:
C#
Grid parent = someGrid; // someGrid already existed in XAML
// create, say, row definitions
TextBox[] textBoxes = new TextBox[count]; // somewhere you know what is the count
for (int index; index < count; ++index) {
    textBoxes[index] = new TextBox();
    // take care of all its properties...
    // insert it:
    parent.Children.Add(textBoxes[index]);
    // and, this is a main question often asked on WPF:
    // how to set a dependency property of a child control?
    // here is how:
    parent.SetRow(textBoxes[index], index);
    // and so on...
}


—SA
 
Share this answer
 
v2
In complement to solutions 1 and 3, the following tips might be useful.

You can have a functions that accept an arbitrary number of parameters:
C#
void DoSomething(params TextBox[] textBoxes)
{
    foreach (var item in textBoxes) { /* Do something */ }
}

This can be used like that:
C#
DoSomething(txtBxFirstName, txtBxLastName, txtBxDOB);


See MSDN documentation: http://msdn.microsoft.com/fr-fr/library/w5zay9db.aspx[^]

This can be useful if you have only a few function to call of many controls. If you have many different things to do, then using lambdas as in solution 1 or regular loop might be appropriate. It also depends on the complexity of what is done with the controls.

As mentionned in solutions 3, in some cases, it might make sense to create items directly from the array and avoid the designer. I would think that if you have 100 controls as mentionned in the question then it might be a good solution.

On the other hand, in some dialogs you might have like 10 to 20 controls (say a form to enter name and address in multiples fields). In such case, you might still want to layout controls in the designer but using array (either explicitly or implicitly with params keyword) might make sense.
 
Share this answer
 
v3
Comments
Frank R. Haugen 30-May-14 20:13pm    
I'm just inundated by great answers today!

Thanks!
C#
//create List or array of text boxes 
List<TextBox> textboxes = new List<TextBox>(){txtBxFirstName, txtBxLastName, txtBxDOB}; // you can add any number of textbox controls 
//then you can simply do as below 
if(textboxes.Any(t=>string.IsNullOrEmpty(t.Text))
{
    Something();
}

or you can create method like below
C#
public void DoSomething(List<TextBox> textboxes)
{
    if(textboxes.Any(t=>string.IsNullOrEmpty(t.Text))
    {
        Something();
    }
}
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 29-May-14 0:21am    
Right, a 5.
—SA
DamithSL 29-May-14 0:25am    
Thank you SA
Sergey Alexandrovich Kryukov 30-May-14 10:54am    
On second thought, this answer would need some improvement... :-)
Please see my answer.
—SA
Frank R. Haugen 30-May-14 9:36am    
Thanks, this couldn't be a more elegant solution! 5/5!
RDBurmon 30-May-14 11:27am    
perfect +5
Through the form's Controls collection Property you can get or find all controls then check which are TextBoxes.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900