Results 1 to 5 of 5

Thread: [Resolved] [2008] Choose which label to write to

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2007
    Posts
    73

    Thumbs up [Resolved] [2008] Choose which label to write to

    Hey, I have a form with 3 labels, a textbox and a button. I want to write a number in the textbox (1-3) and then press the button. Then, if the number in the textbox is 1, I want to write in label1 "TEST", if it's 2 I want to write in label2 etc. I know I can do it with a whole lot of if statements, but I think there has to be a way to do it simply.

    I've tried this:

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Label(TextBox1.Text).text = "TEST"
        End Sub
    But it obviously didn't work...

    Hope someone can help
    Last edited by want a pie; Oct 29th, 2008 at 06:32 PM. Reason: resolved

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] Choose which label to write to

    vb Code:
    1. me.controls("label" & TextBox1.Text).text = "TEST"

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Choose which label to write to

    I would be more inclined to put the Labels in an array and then access the desired Label by index based on the value in the TextBox. That way you don't have to name your Labels identically with a numeric prefix.
    vb.net Code:
    1. Private labels As Label()
    2.  
    3. Private Sub Form1_Load(ByVal sender As Object, _
    4.                        ByVal e As EventArgs) Handles MyBase.Load
    5.     Me.labels = New Label() {Me.Label1, _
    6.                              Me.Label2, _
    7.                              Me.Label3}
    8. End Sub
    9.  
    10. Private Sub Button1_Click(ByVal sender As Object, _
    11.                           ByVal e As EventArgs) Handles Button1.Click
    12.     Dim index As Integer
    13.  
    14.     If Integer.TryParse(Me.TextBox1.Text, index) Then
    15.         index -= 1
    16.  
    17.         If index >= 0 AndAlso index < Me.labels.Length Then
    18.             Me.labels(index).Text = "TEST"
    19.         End If
    20.     End If
    21. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] Choose which label to write to

    Quote Originally Posted by jmcilhinney
    I would be more inclined to put the Labels in an array and then access the desired Label by index based on the value in the TextBox. That way you don't have to name your Labels identically with a numeric prefix.
    isn't that a suffix?

    regarding the labels, i'd agree with JMC for many labels, but i'd stick with what i posted for just 3 labels
    Last edited by .paul.; Oct 29th, 2008 at 06:30 PM.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2007
    Posts
    73

    Re: [2008] Choose which label to write to

    Thanks both of you, they both worked well

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width