Results 1 to 13 of 13

Thread: Dynamically change label text from textbox input

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Posts
    24

    Dynamically change label text from textbox input

    Is there a way that a label's text is updated each time the input of a textbox changes? The textbox and the label are on the same form. I looked everywhere but didn't find a solution that worked.

  2. #2
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    910

    Re: Dynamically change label text from textbox input

    Yes, look through the events for the textbox and you will see one for TextChanged. If you set the label's text equal to the textboxes text each time there is a change it will change automatically.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Posts
    24

    Re: Dynamically change label text from textbox input

    I have tried that already but it does nothing.

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

    Re: Dynamically change label text from textbox input

    jre1229's advice is spot on so, if you tried it and it didn't work, you did it wrong. As you haven't shown us what you did, we can;t tell you what you did wrong.
    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

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Posts
    24

    Re: Dynamically change label text from textbox input

    Quote Originally Posted by jmcilhinney View Post
    jre1229's advice is spot on so, if you tried it and it didn't work, you did it wrong. As you haven't shown us what you did, we can;t tell you what you did wrong.
    I already have a public sub that handles the TextChanged event of the text box, and if i add the line txtbox1.Text = Label1.Text, it creates an exception. so i created a new textchanged event for the textbox and added the code:
    txtbox1.Text = Label1.Text but nothing.

    I have also tried Label1.Text = txtbox1.Text.

    I have done something wrong that's for sure but what is it?

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

    Re: Dynamically change label text from textbox input

    There are so many different exceptions. If only there was a way for us to know what exception was being thrown and what the error message was. Hang on... there is! You could tell us.
    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

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Posts
    24

    Re: Dynamically change label text from textbox input

    Is there a problem if a create a whole new textchanged event like this:

    Code:
    Private Sub txtbox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtbox1.TextChanged
            Label1.Text = txtbox1.Text
        End Sub
    The above code does nothing. Neither if i change the order like txtbox1.text=label1.text

    I already have the beneath code in the textchanged event:
    Code:
    Private Sub txtbox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtbox1.TextChanged
            'Allows only numerical chars
            Dim theText As String = txtbox1.Text
            Dim Letter As String
            Dim SelectionIndex As Integer = txtbox1.SelectionStart
            Dim Change As Integer
    
            For x As Integer = 0 To txtbox1.Text.Length - 1
                Letter = txtbox1.Text.Substring(x, 1)
                If charsAllowed.Contains(Letter) = False Then
                    theText = theText.Replace(Letter, String.Empty)
                    Change = 1
                End If
            Next
            txtbox1.Text = theText
            txtbox1.Select(SelectionIndex - Change, 0)
            'Display dimension in label
        End Sub
    If i add the line: Label1.Text = txtbox1.Text it generates a

    "StackOverflowExepction was unhandled"
    An unhandled exception of type 'System.StackOverflowException' occurred in Car Design.exe

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

    Re: Dynamically change label text from textbox input

    There's no problem with having two handlers for the same event but there's also no point.

    It doesn't make sense to do this:
    Code:
    txtbox1.text=label1.text
    The one on the right is what you're assigning and the one on the left is what you're assigning to, so if the Label is what you want to change then putting the Label on the right doesn't make sense.

    The reason that you were getting a stack overflow is because you were changing the Text in the TextChanged event, which raised the TextChanged event, which changed the Text, Which raised the TextChanged event, etc, etc.

    The first code snippet does exactly what you want: when the Text of the TextBox changes it updates the Text of the Label to the same value. If it's doesn't appear to be working then I can only assume that it;s because that handler never gets to be executed because the other one throws the exception first. Just get rid of the second handler and keep the first one and it will work.
    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

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Posts
    24

    Re: Dynamically change label text from textbox input

    I finally figured it out. My mistake (stupid indeed) was the thing you mentioned, i put the code on the wrong side of the equal sign. Ok problem solved now.

    One more thing. I have 12 textboxes and 12 labels. How can i assign each labels text to each textbox, without changing the textchanged event twelve times?

    Thanks for your assistance, i appreciate it a lot.

  10. #10
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: Dynamically change label text from textbox input

    A few ways you could go about it (although a separate thread should have been created for this question)

    One way would be able to create a dictionary (Of TextBox, Label) or vice versa and pair them up that way. Alternatively you could create parallel arrays (not much point when they're static and a Dictionary is much more reliable. Lastly if there is some common factor to the names (i.e Label1 corresponds to TextBox1, Label5 corresponds to TextBox5 ... etc) you could loop through an increasing integer value and pair them that way. I'll go through the dictionary method here because I feel it's the best

    Global declaration (will probably need it elsewhere, and it saves re-declaring the whole list every time the text is changed)
    Code:
    Private lblTbox As New Dictionary(Of TextBox, Label)
    On Form_Load:
    Code:
    lblTbox.Add(txtBox1, Label1)
    lblTbox.Add(txtBox2, Label2)
    '....etc
    Then on your textbox_textchanged event (with multiple handlers)
    Code:
    Dim curTextBox As TextBox = sender 'identify which textbox is raising the event
    lblTbox(curTextBox).Text = curTextBox.Text 'gets the label corresponding to the current textbox and sets its Text property
    If I helped you out, please take the time to rate me

  11. #11
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    910

    Re: Dynamically change label text from textbox input

    You probably just could have copied the code from the first one to the other 11 and just changed the number faster than reading this message.

  12. #12
    New Member
    Join Date
    Sep 2012
    Location
    Noida, India
    Posts
    1

    Resolved Re: Dynamically change label text from textbox input

    Simple..

    Code:
     <body>
            <form id="form1" runat="server">
                
                <asp:TextBox ID="TextBox1" runat="server" onkeyup="document.getElementById('Label1').innerHTML=this.value;"></asp:TextBox>
                <asp:label id="Label1" runat="server"></asp:label>
    
           </form>
        
    
    </body>
    Attached Files Attached Files

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

    Re: Dynamically change label text from textbox input

    Quote Originally Posted by gopaltiwari2012 View Post
    Simple..

    Code:
     <body>
            <form id="form1" runat="server">
                
                <asp:TextBox ID="TextBox1" runat="server" onkeyup="document.getElementById('Label1').innerHTML=this.value;"></asp:TextBox>
                <asp:label id="Label1" runat="server"></asp:label>
    
           </form>
        
    
    </body>
    It's nice to try to help but providing an incorrect solution to a thread that was resolved almost two years ago in an inauspicious start.
    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

Tags for this Thread

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