Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

 

This is a quickkie :)

 

How do I loop through each textbox on my form ..

I remember reading about this when I was looking for something completely different and now I can't find it now I want it .. :confused:

 

I remember it to be something like this:

 

dim text1 as textbox

for each text1 in form1

text1.text = "moo"

next

 

that doesn't quite work..

Posted

Hi,

 

Thanks for that. It worked like I wanted ... but now I have another problem :confused:

 

How does the for each loop work for the textboxes... there must be a 'int' which tells it where to start and stop ..

 

The problem I am trying to fix is, rather then lots of code to put text into each textbox from sql. I wanted it so I can do a small loop and it would do it all for me ... this is the only hiccup I am having at the moment...

 

e.g.

 

       Dim ctl As Control
       Dim x
       For Each ctl In Controls
           If ctl.GetType Is GetType(TextBox) Then
               ctl.text = dataview.item(0).item(x).tostring
               x += 1
           End If
       Next

 

Hope that makes some sense..

 

:p

Posted

The Controls object is also an object

 

(c# code)

for (int i = 0; i < myMainControl.Controls.Count; i++)
{
  Control control = myMainControl.Controls[i];
  if (control.GetType == typeof(TextBox) )
  {
      ....
  }
}

 

so foreach just goes through each index, hope that code helps

Posted

Hi,

 

Thanks, I think I worked it out as you prolly posted this :\

 

The code worked, I just needed to adjust which controls are added to the collection first ..

Works fine :)

Posted

Hi,

 

Well as I said befoe the code works perfectly, but ... I need to use the ReadOnly control of the Textbox ..

I tried this:

 

Dim txtb As TextBox
       For Each txtb In Me.XpePanel4.Controls  

 

But it didn't work.. (I did include next btw ;) )

 

Can I add the ReadOnly bit to the object or just search through textbox's ..

 

Any Ideas?

Posted

i dont know what ReadOnly has to do with the method we showed you

ReadOnly tells the application that this Textbox wont write the letters, which were typed by user

Posted

Ok, maybe i should have explained what i'm trying to do.. (i'll do that now :P)

 

I am searching through a range of textboxes using for/next loops (and the nifty control code that was given to me :))

rather then ..

 

txtname.text = "joe"

txtaddress.text = "17 Chicken Street"

txtcity = "Paxo"

etc etc..

 

It all works by pulling in the information from my SQL database, but this information can be changed by a certain user, but only certain bits, by different users...

 

e.g.

 

Admin can change all fields

Reception can change txtname.text

 

but reception at the moment with the code can type into the txtcity.text field, which is where I want the readonly to jump in.

So it turns all fields that the user has access to: ReadOnly = False and the rest is ReadOnly = True ...

All fields would be set to ReadOnly = True to start with before the above code.

 

Does that help? .. even a little.. ?

Posted

Hi,

 

Anyone have any ideas for this??

 

I am still stuck at this point..

 

 

       Dim ctl As TextBox
       For Each ctl In Me.XpePanel4.Controls ' Tasks
         if ctl.tag = 1 then
           ctl.readonly = true
         else
           ctl.readonly = false
         end if
       next

 

I have that, now the error I get is:

System.InvalidCastException: Specified cast is not valid.

 

any ideas??

  • Administrators
Posted

Dim ctl As Control
       For Each ctl In Me.XpePanel4.Controls ' Tasks
       if typeof(ctl) is TextBox then
       Dim t as textbox= directcast(ctl,TextBox)
         if t.tag = 1 then
           t.readonly = true
         else
           t.readonly = false
         end if
        end if
       next

 

You will get this error if some of the controls on the form are not TextBoxes - the code above will loop through the controls but not expect them to be textboxes, if they are then we deal with them correctly.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...