How would I implement this?
-
I have 20 textboxes. Lets say, they are txtbox1 to txtbox20 Now, I would like to check them one by one if their text is null. If txtbox1.Text is null, i would stop checking the other textboxes right away. I can easily perform this using an if-else but, its not gonna be that readable. So I want to use a switch statement. The problem is, how? Please help. :( "To teach is to learn twice"
-
I have 20 textboxes. Lets say, they are txtbox1 to txtbox20 Now, I would like to check them one by one if their text is null. If txtbox1.Text is null, i would stop checking the other textboxes right away. I can easily perform this using an if-else but, its not gonna be that readable. So I want to use a switch statement. The problem is, how? Please help. :( "To teach is to learn twice"
daljv wrote: So I want to use a switch statement. The problem is, how? Why not just do soomething like this:
ControlCollection col = (ControlCollection)this.Controls;
foreach(Control t in col)
{
if(t is TextBox)
{
if(t.Text != String.Empty)
{
// Do something here.
break;
}
}
}-Nick Parker
-
daljv wrote: So I want to use a switch statement. The problem is, how? Why not just do soomething like this:
ControlCollection col = (ControlCollection)this.Controls;
foreach(Control t in col)
{
if(t is TextBox)
{
if(t.Text != String.Empty)
{
// Do something here.
break;
}
}
}-Nick Parker
Nick Parker wrote: ControlCollection col = (ControlCollection)this.Controls;foreach(Control t in col){ if(t is TextBox) { if(t.Text != String.Empty) { // Do something here. break; } }} Hmmm... I can't help myself.... 1~ he wanted to stop as soon as one text box had no text (actually he mentioned only the first text box, but I'll assume it was an example). Thus
//... if(t.Text == String.Empty) break; //...
Also, why not keep your own collection of controls containing only the text boxes so that you would not have to parse through all of the controls in the form? Actually, since they are all text boxes, make it a collection of text boxes... So, when you add them to the form's Controls in your initializer, also add them to myBoxes; then:foreach(TextBox t in myBoxes) { if(t.Text == String.Empty) break; //Do whatever you need here... }
HTH, F.O.R. -
Nick Parker wrote: ControlCollection col = (ControlCollection)this.Controls;foreach(Control t in col){ if(t is TextBox) { if(t.Text != String.Empty) { // Do something here. break; } }} Hmmm... I can't help myself.... 1~ he wanted to stop as soon as one text box had no text (actually he mentioned only the first text box, but I'll assume it was an example). Thus
//... if(t.Text == String.Empty) break; //...
Also, why not keep your own collection of controls containing only the text boxes so that you would not have to parse through all of the controls in the form? Actually, since they are all text boxes, make it a collection of text boxes... So, when you add them to the form's Controls in your initializer, also add them to myBoxes; then:foreach(TextBox t in myBoxes) { if(t.Text == String.Empty) break; //Do whatever you need here... }
HTH, F.O.R.Frank Olorin Rizzi wrote: if(t.Text == String.Empty) break; Woops, I guess I flip-flopped that. :-O Ah well, I think he got the main idea which was important. Frank Olorin Rizzi wrote: Also, why not keep your own collection of controls containing only the text boxes so that you would not have to parse through all of the controls in the form? Because then he would have to maintain that collection where as the Form already maintains a collection of all controls applied to it. This is more extensible and easier to maintain. Granted there are other ways to do this, they simple require more work and don't provide much more in the efficiency department. :) -Nick Parker
-
Frank Olorin Rizzi wrote: if(t.Text == String.Empty) break; Woops, I guess I flip-flopped that. :-O Ah well, I think he got the main idea which was important. Frank Olorin Rizzi wrote: Also, why not keep your own collection of controls containing only the text boxes so that you would not have to parse through all of the controls in the form? Because then he would have to maintain that collection where as the Form already maintains a collection of all controls applied to it. This is more extensible and easier to maintain. Granted there are other ways to do this, they simple require more work and don't provide much more in the efficiency department. :) -Nick Parker
Nick Parker wrote: Because then he would have to maintain that collection where as the Form already maintains a collection of all controls applied to it. This is more extensible and easier to maintain. Granted there are other ways to do this, they simple require more work and don't provide much more in the efficiency department. This argument lacks, if there are more text boxes in th esame form... Roland Bär
-
Frank Olorin Rizzi wrote: if(t.Text == String.Empty) break; Woops, I guess I flip-flopped that. :-O Ah well, I think he got the main idea which was important. Frank Olorin Rizzi wrote: Also, why not keep your own collection of controls containing only the text boxes so that you would not have to parse through all of the controls in the form? Because then he would have to maintain that collection where as the Form already maintains a collection of all controls applied to it. This is more extensible and easier to maintain. Granted there are other ways to do this, they simple require more work and don't provide much more in the efficiency department. :) -Nick Parker
Nick Parker wrote: Because then he would have to maintain that collection where as the Form already maintains a collection of all controls applied to it hmm.. yes, I see what you mean. ...speaking of which, let me ask this: If I picked the TextBox object that gets added to the Form's Controls collection (say it is named tBox1), and simply add it to my own collection (say an Hashtable) like so: myTable.Add(myTable.Count, tBox1); Wouldn't I be adding just a reference? Thus the impact on the memory would be relatively small? Or am I missing something here?
-
Nick Parker wrote: Because then he would have to maintain that collection where as the Form already maintains a collection of all controls applied to it. This is more extensible and easier to maintain. Granted there are other ways to do this, they simple require more work and don't provide much more in the efficiency department. This argument lacks, if there are more text boxes in th esame form... Roland Bär
Roland Bär wrote: This argument lacks, if there are more text boxes in th esame form... Show me an example please. -Nick Parker
-
daljv wrote: So I want to use a switch statement. The problem is, how? Why not just do soomething like this:
ControlCollection col = (ControlCollection)this.Controls;
foreach(Control t in col)
{
if(t is TextBox)
{
if(t.Text != String.Empty)
{
// Do something here.
break;
}
}
}-Nick Parker
this won't work - t is TextBox, t would still be a Control which does not have a .Text field so use "as". also why are you casting this.Controls? its a ControlCollection already...
foreach(Control t in this.Controls) { TextBox tb = t as TextBox; if(tb != null && tb.Text == String.Empty) { // Do something here break; } }
"When the only tool you have is a hammer, a sore thumb you will have."
-
this won't work - t is TextBox, t would still be a Control which does not have a .Text field so use "as". also why are you casting this.Controls? its a ControlCollection already...
foreach(Control t in this.Controls) { TextBox tb = t as TextBox; if(tb != null && tb.Text == String.Empty) { // Do something here break; } }
"When the only tool you have is a hammer, a sore thumb you will have."
Philip Fitzsimons wrote: this won't work Yes, it will, try it. Philip Fitzsimons wrote: also why are you casting this.Controls? its a ControlCollection already... Test your code, it will fail if you don't cast it, mine did, I previously wasn't casting it. :) -Nick Parker
-
Roland Bär wrote: This argument lacks, if there are more text boxes in th esame form... Show me an example please. -Nick Parker
Sorry, not precice enough ... :-O if you have the 20 textboxes, that should be tested, and then also other textboxes in the same form, that should not be checked, it is better to have the 20 textboxes in an own collection. Like this you can iterate over your collection without checking if it is a textbox to be checked or not. Hope it is clear what I mean. :~ greets Roland
-
Philip Fitzsimons wrote: this won't work Yes, it will, try it. Philip Fitzsimons wrote: also why are you casting this.Controls? its a ControlCollection already... Test your code, it will fail if you don't cast it, mine did, I previously wasn't casting it. :) -Nick Parker
I can't see who it would work - it won't work on my computer... "t" is a Control. "t.Text" is not valid on a Control. how does it compile?
"When the only tool you have is a hammer, a sore thumb you will have."
-
Sorry, not precice enough ... :-O if you have the 20 textboxes, that should be tested, and then also other textboxes in the same form, that should not be checked, it is better to have the 20 textboxes in an own collection. Like this you can iterate over your collection without checking if it is a textbox to be checked or not. Hope it is clear what I mean. :~ greets Roland
Roland Bär wrote: if you have the 20 textboxes, that should be tested, and then also other textboxes in the same form, that should not be checked, it is better to have the 20 textboxes in an own collection. Like this you can iterate over your collection without checking if it is a textbox to be checked or not. There are other ways around your problem, however my answer still identifies how to accomplish what he was asking for. -Nick Parker
-
I can't see who it would work - it won't work on my computer... "t" is a Control. "t.Text" is not valid on a Control. how does it compile?
"When the only tool you have is a hammer, a sore thumb you will have."
Philip Fitzsimons wrote: how does it compile? Drop my code onto a form, it works. :-D -Nick Parker
-
Nick Parker wrote: Because then he would have to maintain that collection where as the Form already maintains a collection of all controls applied to it hmm.. yes, I see what you mean. ...speaking of which, let me ask this: If I picked the TextBox object that gets added to the Form's Controls collection (say it is named tBox1), and simply add it to my own collection (say an Hashtable) like so: myTable.Add(myTable.Count, tBox1); Wouldn't I be adding just a reference? Thus the impact on the memory would be relatively small? Or am I missing something here?
There are many ways in which you can tackle this, depending on the exact circumstances you should consider using a specific method. My method is rather abstract. :) -Nick Parker
-
Philip Fitzsimons wrote: how does it compile? Drop my code onto a form, it works. :-D -Nick Parker
no it does not: error CS0117: 'System.Web.UI.Control' does not contain a definition for 'Text'
"When the only tool you have is a hammer, a sore thumb you will have."
-
no it does not: error CS0117: 'System.Web.UI.Control' does not contain a definition for 'Text'
"When the only tool you have is a hammer, a sore thumb you will have."
Philip Fitzsimons wrote: no it does not: Ah, I see you are doing this on an ASP.NET webform. This works on a Windows Form. ;) -Nick Parker
-
Philip Fitzsimons wrote: no it does not: Ah, I see you are doing this on an ASP.NET webform. This works on a Windows Form. ;) -Nick Parker
:-D
"When the only tool you have is a hammer, a sore thumb you will have."