我发现这个Vertical Text on Button Control我觉得这看起来很简单,但我无法让它发挥作用。
Public Class VerticalButton3
Inherits System.Windows.Forms.Button
Private _VerticalText As String
Public Property VerticalText() As String
Get
Return _VerticalText
End Get
Set(ByVal value As String)
_VerticalText = value
End Set
End Property
Private Fmt As New StringFormat
Public Sub New()
Fmt.Alignment = StringAlignment.Center
Fmt.LineAlignment = StringAlignment.Center
End Sub
Protected Overrides Sub OnPaint(ByVal PaintEvt As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint (PaintEvt)
PaintEvt.Graphics.TranslateTransform(Width, 0)
PaintEvt.Graphics.RotateTransform (90)
PaintEvt.Graphics.DrawString(_VerticalText, Font, Brushes.Black, New Rectangle(0, 0, Height, Width), Fmt)
End Sub
End Class
但是我得到了垂直和水平文本。
我尝试使用与Vertical Label Control in VB.NET类似的Public Overrides Property Text()
,但这不起作用
如何仅获取垂直文本?
答案 0 :(得分:1)
我测试了你的代码。如果您将Text
属性设置为“”
这是我试过的代码
Private WithEvents vbtn As New VerticalButton3
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
vbtn.Top = 0
vbtn.Left = 0
vbtn.Text = "" ' Note Text property is set to ""
vbtn.VerticalText = "Vertical"
vbtn.Height = 100
Controls.Add(vbtn)
End Sub
或者,您可以在班级的构造函数中使用Text=""
Public Sub New()
Fmt.Alignment = StringAlignment.Center
Fmt.LineAlignment = StringAlignment.Center
Text = ""
End Sub
编辑:我认为最好覆盖Text
属性本身,因为如果错误地设置了Text
属性,则两者都可能出现。以下是覆盖Text
属性的方法(您可能不再需要VerticalText
属性)。
Public Shadows Property Text
Get
Return _VerticalText
End Get
Set(ByVal value)
_VerticalText = value
End Set
End Property