• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Visual basic for loops

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

jmp123

Member
Joined
Jun 6, 2005
Location
London, England
Basically I want to do this in a for loop

For i = 1 to x
Textbox(i).enabled = false
Next i

ofcourse visual basic does not like this bit

Textbox(i) so how could i get round this?

btw its visual basic.net
 
Last edited:
When you do that Visual Basic is looking for a control array. You probably mean for it to change Textbox1, Textbox2, Textbox3,... where it is looking for the array Textbox(1), Textbox(2), Textbox(3)...

The easiest way is to change your textboxes into the control array by setting the .index value.

You may be able to concatinate the values. Like Textbox & i & .text but I don't think that will work.
 
hmm

dont really understand anything there. You would think this would be simpler, I have my code working but it has millions of if statements where as one while code would work. If someone could write this could for me would really help me
 
Found how to work it its :

Dim MyTextboxes As TextBox()

MyTextboxes = New TextBox() {Candidate1, Candidate2, Candidate3, Candidate4, Candidate5, Candidate6}

For i = 0 To (x - 1)
MyTextboxes(i).Visible = True


Next
 
Why not just iterate through the collection of controls and pull out only the textboxes? You will have a form with controls on it and you want to iterate through the controls. Try this:

Code:
for each ctrl as control in me.controls

if ctrl.gettype().tostring() = "System.Windows.Forms.Textbox" then

  tb = ctype(ctrl, Textbox)
  tb.enabled = true

end if

next

Something like this will allow you to place more textboxes into the form without having to change that code to enable/disable them. This makes it more dynamic.

If this was not what you are looking for, I am still very tired so please pardon me. :)
 
I think that it's even easier, something like

If typeof(ctrl) is textbox

that should work.

Of course, there might be textboxes that shouldn't be toggled. In that case, you could do

If typeof(ctrl) is textbox andalso ctrl.Name.Contains("whatever") then

The andalso causes the If to end and not evaluate the name if the ctrl is not of type textbox since you don't care what the name is if the ctrl is not a textbox.
 
Back