|
-
Sep 6th, 2009, 02:21 AM
#1
Thread Starter
Junior Member
Using a label to show textbox data
Hey, great forum you have here.
Does anyone know how to make label1 show the data in textbox1? I simply can't figure it out.
Value of type 'System.Windows.Forms.TextBox' cannot be converted to 'System.Windows.Forms.Label'.
Thanks for the help in advance :~)
Last edited by Th0ughts; Sep 6th, 2009 at 03:00 AM.
-
Sep 6th, 2009, 02:24 AM
#2
New Member
Re: Using a label to show textbox data
You are probably writing "textbox1" instead of "textbox1.text"
Here is the right code:
label1.text = textbox1.text
Remember, when wanting to refer to a string in a textbox(or any control with text in it), write ".text" after the control's name
Last edited by LeisureProgrammer; Sep 6th, 2009 at 02:27 AM.
-
Sep 6th, 2009, 02:24 AM
#3
Re: Using a label to show textbox data
It sounds like you are referring to the controls directly instead of their Text properties.
Label1.Text=TextBox1.Text
-
Sep 6th, 2009, 02:25 AM
#4
Re: Using a label to show textbox data
Hey Leisure Jinx you owe me a coke.
-
Sep 6th, 2009, 02:42 AM
#5
Thread Starter
Junior Member
Re: Using a label to show textbox data
What a prompt response, thanks guys! One more problem. The label does not seem to update as the textbox does. (it compiles properly)
As in, I type in the textbox, nothing appears in the label.
Any solution?
Label1.Text = TextBox1.Text
-
Sep 6th, 2009, 02:46 AM
#6
Re: Using a label to show textbox data
VB is an "event-driven" programming system - code is basically executed in response to events raised by user interaction.
In order for the label to keep updating you need to respond to the event that is raised when the user changes the contents of the textbox. Using that single line of code doesn't in any way link the two controls - you need to keep the two in synch manually.
If you double-click on your textbox in the form designer it will open a code window and create a procedure "textbox1_textchanged" which handles the textchanged event of the textbox, so you want to put your code to update the label in there :
Code:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Label1.Text = TextBox1.Text
End Sub
-
Sep 6th, 2009, 02:51 AM
#7
Thread Starter
Junior Member
Re: Using a label to show textbox data
 Originally Posted by keystone_paul
VB is an "event-driven" programming system - code is basically executed in response to events raised by user interaction.
In order for the label to keep updating you need to respond to the event that is raised when the user changes the contents of the textbox. Using that single line of code doesn't in any way link the two controls - you need to keep the two in synch manually.
If you double-click on your textbox in the form designer it will open a code window and create a procedure "textbox1_textchanged" which handles the textchanged event of the textbox, so you want to put your code to update the label in there :
Code:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Label1.Text = TextBox1.Text
End Sub
Beautiful, thanks a bunch! I appreciate it!
EDIT:
I thought of one more, perhaps more complicated idea. If the text put into the label is large (extends beyond X amount of characters), would it be possible to add an ellipsus in there and cut off the rest of the text? For instance, lets say I want my label to support 10 characters only. The textbox has "Lorem Ipsum etc;" the label would show "Lorem Ipsu..."
Last edited by Th0ughts; Sep 6th, 2009 at 02:59 AM.
-
Sep 6th, 2009, 03:01 AM
#8
Thread Starter
Junior Member
Re: Using a label to show textbox data
Bumping for the update, sorry. I'm afraid it wont be seen as I marked it as solved.
EDIT:
I thought of one more, perhaps more complicated idea. If the text put into the label is large (extends beyond X amount of characters), would it be possible to add an ellipsus in there and cut off the rest of the text? For instance, lets say I want my label to support 10 characters only. The textbox has "Lorem Ipsum etc;" the label would show "Lorem Ipsu..."
-
Sep 6th, 2009, 03:03 AM
#9
Re: Using a label to show textbox data
Yes thats perfectly possible... think about it logically and it should be obvious what you need to do.
NB if you've marked a thread resolved you can un-mark it resolved using the same menu, however as you're now asking a new question you should really post this as a new thread.
-
Sep 6th, 2009, 03:06 AM
#10
New Member
Re: Using a label to show textbox data
Change the "autoellipsis" property of the label to "true" , and the "autosize" to "false" OR, put this code into the Form Load event handler, I've changed those properties for you here:
vb Code:
Label1.AutoEllipsis = True Label1.AutoSize = False
I suggest you spend a few hours just playing with the properties, you'll learn a lot.
Last edited by LeisureProgrammer; Sep 6th, 2009 at 03:09 AM.
-
Sep 6th, 2009, 03:22 AM
#11
Thread Starter
Junior Member
Re: Using a label to show textbox data
 Originally Posted by keystone_paul
Yes thats perfectly possible... think about it logically and it should be obvious what you need to do.
NB if you've marked a thread resolved you can un-mark it resolved using the same menu, however as you're now asking a new question you should really post this as a new thread.
I was playing with If len > statements and decided to refresh this thread to see if there was any more help. I had no idea that this was a default property.
 Originally Posted by LeisureProgrammer
Change the "autoellipsis" property of the label to "true" , and the "autosize" to "false" OR, put this code into the Form Load event handler, I've changed those properties for you here:
vb Code:
Label1.AutoEllipsis = True
Label1.AutoSize = False
I suggest you spend a few hours just playing with the properties, you'll learn a lot.
I feel idiotic not knowing that this was a default property. Regardless, thanks for the assistance, guys!
-
Sep 6th, 2009, 03:23 AM
#12
Re: Using a label to show textbox data
 Originally Posted by LeisureProgrammer
Change the "autoellipsis" property of the label to "true" , and the "autosize" to "false" OR, put this code into the Form Load event handler, I've changed those properties for you here:
vb Code:
Label1.AutoEllipsis = True Label1.AutoSize = False
I suggest you spend a few hours just playing with the properties, you'll learn a lot.
Unfortunately using the auto-ellipsis property wont help if you explicitly want to limit the label to 10 characters as it works purely on screen size not the number of characters (unless you use a non-proportionally spaced font like courier and set the label up to be 10 characters wide).
-
Sep 6th, 2009, 03:30 AM
#13
New Member
Re: Using a label to show textbox data
EDITed some minor bugs... Use this one
Oh, sorry, sorta overlooked this part, TY
(extends beyond X amount of characters)
You were right about using the If, Then, statements... Here is the edited code to put in the textchange event handler
vb Code:
Dim mcl As Integer = 10 'Replace this with max char. length
If TextBox1.TextLength > mcl Then
Label1.Text = (TextBox1.Text.Substring(0, mcl) + "...")
ElseIf TextBox1.TextLength <= mcl Then
Label1.Text = TextBox1.Text
End If
Oh, and change the AutoSize property back to true
Last edited by LeisureProgrammer; Sep 6th, 2009 at 03:48 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|