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.
Printable View
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.
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.
I have tried that already but it does nothing.
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?
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.
Is there a problem if a create a whole new textchanged event like this:
The above code does nothing. Neither if i change the order like txtbox1.text=label1.textCode:Private Sub txtbox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtbox1.TextChanged
Label1.Text = txtbox1.Text
End Sub
I already have the beneath code in the textchanged event:
If i add the line: Label1.Text = txtbox1.Text it generates aCode: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
"StackOverflowExepction was unhandled"
An unhandled exception of type 'System.StackOverflowException' occurred in Car Design.exe
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: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.Code:txtbox1.text=label1.text
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.
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.
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)
On Form_Load:Code:Private lblTbox As New Dictionary(Of TextBox, Label)
Then on your textbox_textchanged event (with multiple handlers)Code:lblTbox.Add(txtBox1, Label1)
lblTbox.Add(txtBox2, Label2)
'....etc
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
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.
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>