|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Using a variable to specify the name of a controlI need to specify a control's name using a variable and I can't seem to get it right. I have several labels on a form and depending on a user's actions, a label's text is changed. Rather than writing code for each label and each event that can change the labels, I'm trying to do all of the changes in one event, but I need to specify the label's name using a variable. for instance, I have an integer (i) that is incremented in a loop. The label that I want to change in each pass of the loop might be Label+i.ToString. What I can't figure out is how to make a change to the label. This is what I'm trying, but I get a Cast not Valid error: DirectCast("Label"+i.ToString,System.Windows.Forms.Label).text="MyText" TIA Lee Lbjr,
Thanks to Peter I see that I misunderstood your question. To give you an extra alternative (typed here watch typos) \\\ For i as integer = 0 to Controls.count If Controls(i) = ("Label" & i.ToString) then Controls(i).text="MyText" End if Next /// This sample is by the way only with labels direct on a form. I hope this helps better. Cor Hi
the "Label"+i.ToString is an string, it is not the reference to the labeli object, directcast is used to cast certain reference to any other compatible type. e.g. Class A Class B herits A So the B instance can be casted to A or vice visa. For your scenario, I think we may enumerate the control in the form's controls collection as below. For Each o As Control In Me.Controls If TypeOf o Is Label Then Debug.WriteLine(o.Name) End If Next or We can use the reflection to get the instance directly. e.g. the code below will get the reference of the Label2. Dim lb As Label = Me.GetType().InvokeMember("_Label2", BindingFlags.Instance Or BindingFlags.GetField Or BindingFlags.NonPublic, Nothing, Me, New Object() {}) lb.Text = "Changed" NOTE: If we declare the the Label1 as below, we can get the reference by Label1, Friend Label1 As System.Windows.Forms.Label while if the Label1 is declared as below. Friend WithEvents Label1 As System.Windows.Forms.Label We need to get the reference by _Label1. Best regards, Peter Huang Microsoft Online Partner Support Get Secure! - www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no rights. Hi Cor,
Thanks for your knowledge sharing in the community. :) Best regards, Peter Huang Microsoft Online Partner Support Get Secure! - www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no rights. "lgbjr" <lgbjr@online.nospam> schrieb: Accessing controls by their names or indices> for instance, I have an integer (i) that is incremented in a loop. The > label that I want to change in each pass of the loop might be > Label+i.ToString. What I can't figure out is how to make a change to the > label. This is what I'm trying, but I get a Cast not Valid error: > > DirectCast("Label"+i.ToString,System.Windows.Forms.Label).text="MyText" <URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en> -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> I sometimes use Cor's code with a minor change:
For i as integer = 0 to Controls.count If Controls(i).Name = ("Label" & i.ToString) then Controls(i).text="MyText" End if Next Show quoteHide quote "lgbjr" wrote: > Hi All, > > I need to specify a control's name using a variable and I can't seem to get > it right. > > I have several labels on a form and depending on a user's actions, a label's > text is changed. Rather than writing code for each label and each event that > can change the labels, I'm trying to do all of the changes in one event, but > I need to specify the label's name using a variable. > > for instance, I have an integer (i) that is incremented in a loop. The label > that I want to change in each pass of the loop might be Label+i.ToString. > What I can't figure out is how to make a change to the label. This is what > I'm trying, but I get a Cast not Valid error: > > DirectCast("Label"+i.ToString,System.Windows.Forms.Label).text="MyText" > > TIA > Lee > > > >
SqlDateTime.MinValue, SqlDateTime.MaxValue: WATCH OUT!
IO function in Vb.Net slower than in Vb6.0 Datalist/datatable sorting Cannot Get MenuItem's name[Notsolved] Writing E-Mail Notfications New Problem with timers ComboBox needs help CurrencyManager Question VB.NET with ADO.NET compilation error. Dataset problem |
|||||||||||||||||||||||