Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2007

Tip: Looking for answers? Try searching our database.

For...Each Loop and Controls Help

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
levimefford@gmail.com - 02 Oct 2007 03:07 GMT
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.
Charlie - 02 Oct 2007 04:16 GMT
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...

> 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
[quoted text clipped - 27 lines]
> insight on how the program decides what order to iterate through the
> controls.
Cor Ligthert[MVP] - 02 Oct 2007 04:43 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.