הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Tuesday, March 16, 2010 3:26 PM
How do I wrap button text? When the button text is to large for the width of the button the remaining characters should go to the next line instead of going off the button.
All replies (7)
Tuesday, March 16, 2010 3:33 PM ✅Answered
Is this a web or windows form? For windows forms at least this problem is taken care of internally -- meaning it auto-wraps text when it gets too long for the button's width. How are you getting a button to do this otherwise? Are you painting the text on yourself?
I suppose an naswer would be to insert a line feed character at the places you want the line breaks to be. This can be done a few ways -- through vb constants like VbCrLf or VbCr or directly though characters line CHR(10) or CHR(13). Note that CHR(13) is the carriage return(Cr) and CHR(10) is the line feed(Lf)
Wednesday, March 17, 2010 6:32 AM ✅Answered | 1 vote
If you use DrawString with a rectangle and a StringFormat object you can specify the wrapping mode by setting the FormatFlags property of the StringFormat object (NoClip seems suitable), and the text will be drawn within the rectangle, as best it can. Your rectangle could be the label bounds, or derived from the label bounds to allow for your graphics.
DrawString with a rectangle and string format is here:
http://msdn.microsoft.com/en-us/library/21kdfbzs.aspx
Tuesday, March 16, 2010 3:38 PM
I set my buttons AutoSize to OFF, and resize to accomodate the extra line. That I do using the Designer. In the Form Load Event, I set the Text using a string with embedded CR/LFs. It works for me, although I suspect that there is a better way...I just don't know about it. (Design is not my thing, really.)
If you are wanting to do this on the fly, you will need to determine how much text will fit into your button, which is probably a rectangle. There are plenty of tools for that in .NET.
Anyway, the Button control accepts and correctly interprets vbCrLf.
Tuesday, March 16, 2010 4:17 PM
I'm drawing it on.
buttonPath.AddArc(0, 0, 8, 8, 180, 90)
buttonPath.AddArc(MyBase.Width - 9, 0, 8, 8, 270, 90)
buttonPath.AddArc(MyBase.Width - 9, MyBase.Height - 9, 8, 8, 0, 90)
buttonPath.AddArc(0, MyBase.Height - 9, 8, 8, 90, 90)
buttonPath.CloseAllFigures()
e.Graphics.DrawString(_buttonText, MyBase.Font, multiBrush, ((MyBase.Width - 18) \ 2) - (size.Width \ 2), (MyBase.Height \ 2) - (size.Height \ 2))
Tuesday, March 16, 2010 4:30 PM
I'm drawing it on.
The answer from Dig Boy stays then still the sameSuccess
Cor
Wednesday, March 17, 2010 5:22 AM | 1 vote
Hi ChazParks2 ,
Please try the following code samples with one button on your FORM.
In addition to the reply by forum user Dig-Boy you can also use the keyword NewLine intead of VbCrLf
You can use it from either but not use both of the following IMPORTS statements.
In other words use one or the other IMPORTS statement.
Either.>>
Imports System.Environment
**
'OR**
Imports Microsoft.VisualBasic.ControlChars
otherwise you will get an ambiguous error like this.>>
Error 1 'NewLine' is ambiguous, imported from the namespaces or types 'Microsoft.VisualBasic.ControlChars, System.Environment'. C:\Users\John\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb 9 34 WindowsApplication1
which basically means Vb.Net does not know which NewLine you want from the two namespace-classes;
- Imports System.Environment
or
- Imports Microsoft.VisualBasic.ControlChars
Regards,
John
Imports System.Environment
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1.AutoSize = True
Button1.Text = "Line1" & NewLine & "line2."
End Sub
End Class
'OR .>>
Imports Microsoft.VisualBasic.ControlChars
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1.AutoSize = True
Button1.Text = "Line1" & NewLine & "line2."
End Sub
End Class
'OR fully qualify the NewLine keyword like this.>>
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1.AutoSize = True
Button1.Text = "Line1" & System.Environment.NewLine & "line2."
'OR.>>
Button1.Text = "Line1" & Microsoft.VisualBasic.ControlChars.NewLine & "line2."
End Sub
End Class
Friday, September 24, 2010 6:13 PM
While this may work for the full .NET Framework, here are a few clarifications for the unwary:
- The .NET Compact Framework is capable of wrapping text, but only if you use default formatting or specify a LineAlignment (that is, vertical alignment) of Near . Any other value disables text wrapping . This means that to vertically align the text to Center or Far, you must somehow calculate the size of the text area yourself and offset your drawing rectangle; there is only a single-line MeasureString() method that is inadequate for the job.
- Using NoClip (StringFormat flag) is not suitable for typical circumstances, contrary to previous posts. With this flag, if the text doesn't fit the rectangle, it will be drawn beyond the borders anyway. If you are going to the trouble of specifying a rectangle, you usually want things clipped so nothing falls outside.
There are a lot of nice things about the .NET Compact Framework, but these unintuitive and undocumented conditions are some of the many things that make writing attractive and full-featured applications laborious for mobile devices. You either have to find a third party product to compensate for the deficiencies or write it yourself.