VB.net Labels: Changing Caption on Multiple Lines

  • Thread starter Pauly Man
  • Start date
In summary, the code in VB6 and earlier to change the caption of a label used a Chr(13) keyword to create a two-line message with the caption. VB.net ignores the Chr(13) keyword and the code no longer works. To change the caption of a label in VB.net, use the vbCrLf constant to create a new text line.
  • #1
Pauly Man
129
0
In VB6 and earlier if you wanted to have your program change the caption of a label you would code something like this:

Code:
dim L,L1,L2 as string
L1 = "Hello There!"
L2 = "I am Paul."
L = L1 & Chr(13) & Chr(13) & L2
lblMyLabel.Caption = L

The above code would create a label message on two lines as follows:

Code:
Hello There!
I am Paul.

Now this doesn't work in VB.net. (I know that .caption is replaced with .text). VB.net completely ignores the Chr() keyword. Does anyone else know how the above can be done now?
 
Computer science news on Phys.org
  • #2
Have you tried "\n"? Maybe Microsoft has switched to the standard convention. (ha, ha, I know)
 
  • #3
I tried the "\n" and it doesn't work. :frown:
 
  • #4
Sorted it. FOr some reason, Chr(13) is no longer recognised as a line break, but Chr(10) works.
 
  • #5
chr(13) is a carridge return, chr(10) is a linefeed. You should ideally use both when making a new text line.
Try using the constant vbCrLf instead.
i.e. L = L1 & vbCrLf & L2
 
  • #6
The vbCrLf works. And I can see where it gets its name, ie. vbCreateLinefeed. But I can't find any of these constants in the help files, so how do you find out about them?
 
  • #7
I thought it would work. :smile:

For the vbCRLF constant:
It's mentioned in the "Working with Text Boxes" topic.

For constants in general:
In VB6, look in the Help-Contents-VisualBasicDocumentation-Reference-Constants, then there's several items that each have many subitems. It's in there somewhere.

I'm not sure where it is located for vb.net help, but I would assume it's similar. You might try searching the help for "reserved words", "constant", or something. Maybe even searching "vb" will help since they all (most) start with vb for visualbasic.

And I can see where it gets its name, ie. vbCreateLinefeed.
Acutally, it stands for "CarridgeReturn-LineFeed".
A "CarridgeReturn" is originally from the typewriter. The metal bar across the top is called a carridge return. When you push it to the right (to begin typing at the left of the page) it rolled the typewriter "carridge" up one line as it "returned" from the right. It's been used for computers ever since they were connected to a CRT and keyboard.
(edit: Maybe since they were connected to the first printers instead of CRTs.)
 
  • #8
Hello guys.
I just ran into your post trying to find out how to put line feed into my label box for class.
I referenced back and found it under Visual Basic Langauge Reference> Print and Display Constants in VB.NET.
Hope this helps. I just stared VB.NET class this week.
~Ocilimond
 

Related to VB.net Labels: Changing Caption on Multiple Lines

1. How do I change the caption on a VB.net label?

To change the caption on a VB.net label, you can use the .Text property. For example: myLabel.Text = "New Caption". This will update the label's caption to display the new text.

2. Can I change the caption on a label to multiple lines in VB.net?

Yes, you can change the caption on a label to multiple lines in VB.net by using the Environment.NewLine string. For example: myLabel.Text = "Line 1" + Environment.NewLine + "Line 2". This will display "Line 1" on the first line and "Line 2" on the second line.

3. How do I change the font style and size for a label's caption in VB.net?

To change the font style and size for a label's caption in VB.net, you can use the .Font property. This allows you to specify the font family, size, and style. For example: myLabel.Font = New Font("Arial", 12, FontStyle.Bold) will set the label's font to Arial, size 12, and bold.

4. Is it possible to change the color of a label's caption in VB.net?

Yes, you can change the color of a label's caption in VB.net by using the .ForeColor property. This allows you to specify a color using RGB values or predefined colors. For example: myLabel.ForeColor = Color.Red will set the label's caption to display in red.

5. How can I dynamically change the caption on a label based on user input in VB.net?

You can dynamically change the caption on a label based on user input in VB.net by using the .Text property and assigning it a value based on the user's input. For example: myLabel.Text = userInputTextBox.Text will update the label's caption to display whatever the user has entered in the text box.

Similar threads

Replies
3
Views
3K
  • Electromagnetism
Replies
16
Views
1K
  • Calculus and Beyond Homework Help
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Introductory Physics Homework Help
Replies
2
Views
11K
Replies
33
Views
2K
  • Special and General Relativity
Replies
7
Views
1K
  • Programming and Computer Science
Replies
19
Views
1K
  • General Discussion
Replies
12
Views
1K
  • Classical Physics
Replies
1
Views
3K
Back
Top