For...Each Loop and Controls Help

L

levimefford

Alright, I need some help with a (somewhat large) problem. I am
working with a large number of controls on a form. I have made it a
bit easier on myself to deal with them through the naming conventions
and locked/unlocked settings. However, there is still one problem that
I have. The order.

Dim ccontrol As Control
Dim txt As TextBox
Dim i As Integer = 29

For Each ccontrol In Me.Sinclair.Controls
If (TypeOf ccontrol Is TextBox) Then
txt = DirectCast(ccontrol, TextBox)
If txt.ReadOnly = True Then
If InStr(ccontrol.Name.ToString, "uiSinclair")
Then
ccontrol.Text =
xlSinclair.Range(CellRange(i)).Value
i = i - 1
End If
End If
End If
Next ccontrol

Works just fine if the text boxes somehow, miraculously, are in the
correct "control order". But I'm not sure what exactly changes this
"control order" or whatever you would like to call it or how I would
go about manually changing this order so that I can load the data that
I want to into these text boxes. If anyone could give me a little
insight on how the program decides what order to iterate through the
controls.
 
G

Guest

I don't think there is a way to determine the order of the For Each of the
form's controls collection.

I know of two way to resolve it.

First the Q&D way

Set the tag property of each textbox equal to the index of the column you
want to associate it with

As you loop the controls collection...
ccontrol.Text = xlSinclair.Range(CellRange(clng(ccontrol.text.tag))).Value
This way, whichever textbox is next in the collection, it contains its own
link to the column index via the tag.

The other way would be to add your controls to a keyed or indexed collection.
A hashtable allows access to its objects via the key...
The key would be the associated column index

Dim ht As New Hashtable()
'add controls, using the column indices as keys

'loop the hashtable
Dim key As Int32
For Each key In ht.Keys
ccontrol = ht(key)
control.Text = xlSinclair.Range(CellRange(key)).Value
Next

You could also create a similar collection using an ArrayList, where you add
the controls to the ArrayList in the order of the columns, then loop through
them with a For i = 0 to TextBoxArrayList.count - 1...
 
C

Cor Ligthert[MVP]

Hi,

If you use the designer then the textboxes are added to the form (or any
other control) in the way you drag them on the screen. When you remove them,
they are removed, if you set them temporaly on another control or whatever,
they are removed from the form and added to that other control.

Have a look at the messages from Charlie, in fact I write this alone to show
an addition on that. Create a simple array of textboxes where you name them
(you are using the same textboxes however set them in an array that holds
only the references).

dim mySinclairTextboxes() as Textbox = {txt1,txt2,etc}

That is in my idea the most simple way in your solution.

Cor
 
Joined
Jul 13, 2011
Messages
1
Reaction score
0
I figured this one out. You can specify the control order by using the designer.vb (form_name.designer.vb). You know that thing that MS doesn't want you to modify via code. :rolleyes: Find the section where the Me.Controls.Add(Me.control_name) is located and change the order there to match your preference. Save and retry. Viola!

For C# this is located in the form_name.cs file. Again, goto the section that contains the code to add the controls to the form. this.Controls.Add(this.control_name) and change the order to correspond to your preference. Happy coding.

Make It A Great Day!

jkgraham
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top