Multi Lines In a label

DonnaF

Freshman
Joined
Mar 20, 2003
Messages
30
How do I get multi-lines to display on a label object in VB.Net? I have two lists boxes. Once has a phrase, and the other has numbers 1 to 10. When I click on a Print button, I want the selected phrase to print in the label, based on the number selected in the other listbox. I keep getting the phrase written in the same spot on the label, rather than multiple lines in the label.

Thanks, Donna
 
I am not currently at a computer that can test this, but you might try this:
Code:
'lblLabel is the name of the label

lblLabel.Text = "This is a line of text." & Environment.NewLine() &
                        "This is another line of text."

Hope this helps. Let us know if it works or not.

--Sean
 
Sean, I tried your suggestion, and it did give me two lines (see my code below). However, I had clicked three on the one list box, and it only went through the loop once. I got the two lines one time instead of three times. Since I'm fairly new to VB.Net, there's probably something wrong with my loop. I want to loop based on the value in the list box selected. Any thoughts on that? Thanks for your help, Donna


Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Dim IntNumber As Integer = 1

Do Until IntNumber > Val(lstNumber.SelectedItem)
'lblPhrase.Text = lstPhrase.SelectedItem + vbCrLf
'lblPhrase.Text = IntNumber & vbCrLf
lblPhrase.Text = IntNumber & Environment.NewLine & lstNumber.SelectedItem
IntNumber = IntNumber + 1
Loop
End Sub
 
It looks to me like you are just clearing the label every time you
iterate through it. You set the label's text to IntNumber &
Environment.NewLine & lstNumber.SelectedItem, and then you
do that 2 more times; it's not appending, just changing the text.

To fix this, change the code to this:
Visual Basic:
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Dim IntNumber As Integer

lblPhrase.Text = ""
For IntNumber = 1 To Val(lstNumber.SelectedItem)
    lblPhrase.Text &= IntNumber & Environment.NewLine & lstNumber.SelectedItem
    IntNumber = IntNumber + 1
  Next IntNumber
End Sub
Notice I changed it to a For loop, because that is more appropriate
in this case. The differences between them can be found in your MSDN
if you're curious.

Also, notice I changed the operator used to change the label to &=,
which appends text to the label, rather than just changes it.
 
Back
Top