Monday, January 18, 2010

How can I loop through form controls in VB.NET 2008 ?

I have several labels on a form named sequentially lblTemp0, lblTemp1, lblTemp2, etc.. I want to loop through these and modify their caption. In VB 6 I used the following code.





For X = 0 to 25


Me(';lblTemp'; %26amp; X).Caption = gTemp(X)


Next X





The me(';lblTemp'; %26amp; X).Text code does not work in VB.NET 2008.





Thanks.


TimHow can I loop through form controls in VB.NET 2008 ?
'Cycle through all controls in the form - set to ME because


'it is run from the form class


For Each objcontrol As Control In Me.Controls


'Check that the control is a label control and that the name contains the word lbltemp


'if it does then set it to equal the word sometext


If objcontrol.GetType.ToString = ';System.Windows.Forms.Label'; And objcontrol.Name.Contains(';lblTemp';) _


Then objcontrol.Text = ';sometext';


Next





You can play with your own if statements that i've made there. For instance you can return the last character from the name convert to integer do some math and put the text in to match the result. Whatever you want really





Hope this is clear

No comments:

Post a Comment