Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Public Sub Rename(ByVal k As Integer)
        Dim lbl As Label
        Dim j As Integer
        For j = k To 50
            lbl = Controls("Label" + j.ToString())
            lbl.Text = (j - 1).ToString()
            Next
    End Sub



I want to change the label text from label k to label 50. k will be given as input. I tried the above code. it's working in other project. but in my current project it's
not working. is thr any other way to rename labels in VB.NET?
Posted
Updated 22-Feb-15 18:31pm
v2
Comments
Tomas Takac 22-Feb-15 5:09am    
What do you mean by "it doesn't work"? What exactly does the Controls() method? Or is it an array or dictionary? This is why I don't like VB.NET...

Try using the Page.FindControl[^] method.
 
Share this answer
 
Comments
dipankarnalui 22-Feb-15 11:34am    
i want to use labels in my project. i want to increase the label.text at run time. suppose if the label.text is 2 then at run time it will be 3. like wise i want to implement for 50 labels inside a for loop. suppose i have 50 labels. label1 to label50. now in run time i want to show that label1.text= 2 , label2.text = 3 and so on...
OriginalGriff 22-Feb-15 12:13pm    
Yes, I guessed that...now, did you try FindControl?
dipankarnalui 22-Feb-15 21:04pm    
FindControl is not a member of System.EventArgs
OriginalGriff 23-Feb-15 6:07am    
Who said it was?
dipankarnalui 23-Feb-15 7:06am    
it was the error while using FindControl.. OK thank u all.. actually problem was with cross thread ..
Try to use Page.FindControl("Label" + j.ToString())

look at the code below....
C#
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

       Dim i As Integer = 1
       Rename(i)

   End Sub
   Public Sub Rename(ByVal k As Integer)

 Dim lbl As Label
       Dim j As Integer
       For j = k To 11
           lbl = Page.FindControl("Label" + j.ToString())
           lbl.Text = (j - 1).ToString()
       Next
   End Sub
 
Share this answer
 
v2
Comments
dipankarnalui 23-Feb-15 7:16am    
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False


problem was with cross thread ..after using the above code it's working fine.. thank u all..
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900