Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

I am not currently at a computer that can test this, but you might try this:

'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

Posted

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

  • *Experts*
Posted

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:

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...