looping through textboxes
-
Ok, I am trying to loop through 6 textboxes to check,if they are blank replace with zero. i can do this with each one but i want to put it in a loop for efficiancy. here is what i want to do dim i as integer for i=1 to 6 if textbox(i).text ="" then textbox(i).text = 0 end if I get this error 'textbox' is a type and cannot be used as an expression, and it highlights this line - if textbox(i).text="" then Thanks for the help, Santana
-
Ok, I am trying to loop through 6 textboxes to check,if they are blank replace with zero. i can do this with each one but i want to put it in a loop for efficiancy. here is what i want to do dim i as integer for i=1 to 6 if textbox(i).text ="" then textbox(i).text = 0 end if I get this error 'textbox' is a type and cannot be used as an expression, and it highlights this line - if textbox(i).text="" then Thanks for the help, Santana
I do not think there is a way to put textboxes into an array, in a way to loop through them as you are doing. You can however, iterate through the controls on the page:
Private Sub TraverseControls(ByVal container As Control) Dim control As control For Each control In container.Controls If control.GetType.Name = "TextBox" Then CType(control, TextBox).Text = String.Empty End If If control.HasControls() Then TraverseControls(control) End If Next End Sub
You can call this function with Page, but it will set all your textboxes to an empty string. In order to make good use of it, you can put your 6 textboxes in a Panel, and then call the function with that Panel. hope this helps, sivilian -
I do not think there is a way to put textboxes into an array, in a way to loop through them as you are doing. You can however, iterate through the controls on the page:
Private Sub TraverseControls(ByVal container As Control) Dim control As control For Each control In container.Controls If control.GetType.Name = "TextBox" Then CType(control, TextBox).Text = String.Empty End If If control.HasControls() Then TraverseControls(control) End If Next End Sub
You can call this function with Page, but it will set all your textboxes to an empty string. In order to make good use of it, you can put your 6 textboxes in a Panel, and then call the function with that Panel. hope this helps, siviliansivilian wrote: I do not think there is a way to put textboxes into an array Yes, there is. The controls collection is essentially an array of
Control
objects which is the base class of all the controls, includingTextBox
. So, it is not that much different to having an array of TextBoxes. I'm a C# developer, so I can't show you exactly in VB.NET, so I'll give you a C# example that assumes there are 6 TextBoxes:TextBox[] myTextBoxes = new TextBox[6];
int i = 0;
foreach(Control ctrl in container.Controls)
{
TextBox tb = ctrl as TextBox;
if (tb == null)
continue;
myTextBoxes[i] = tb;
i++;
}At the end of the
myTextBoxes
contains the 6 text boxes, which can be looped over as normal.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!