如何在 vb.net 中的单选按钮选项文本上使用不同的字体样式(粗体和常规) [英] How to use different font style (bold and regular) on a radio button option text in vb.net
本文介绍了如何在 vb.net 中的单选按钮选项文本上使用不同的字体样式(粗体和常规)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在单选按钮选项中将文本的一部分设为粗体
我需要实现此输出,其中选项 A:"字体样式为粗体,而Pasta"只是常规字体样式.
I need to make a part of the text bold in a radio button option
I need to achieve this output wherein "Option A:" font style is bold and "Pasta" is just regular font style. radio button output
I've tried this code and string is data and it doesn't have font option
Public Class _1_5_register_event_s2
Private Sub _1_5_register_event_s2_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim optionA As String = "Option A ($0): "
Dim food As String = "Pasta"
optionA = FontStyle.Bold
food = FontStyle.Regular
rb_optionA.Text = optionA & food
End Sub
解决方案
You can override the radiobutton class.
Code:
Class
Public Class XRadioButton
Inherits RadioButton
Public Sub New()
UseVisualStyleBackColor = False
TextImageRelation = TextImageRelation.ImageAboveText
End Sub
Public Overrides Property Text As String
Get
Return ""
End Get
Set(ByVal value As String)
MyBase.Text = value
End Set
End Property
Public Property LeftText As String
Public Property RightText As String
Protected Overrides Sub OnPaint(ByVal pevent As PaintEventArgs)
MyBase.OnPaint(pevent)
Dim rect As Rectangle = ClientRectangle
rect.Inflate(-5, -5)
Using sf As StringFormat = New StringFormat() With {
.Alignment = StringAlignment.Center,
.LineAlignment = StringAlignment.Far
}
Using brush As Brush = New SolidBrush(ForeColor)
Dim font As Font = New Font(Me.Font, FontStyle.Bold)
pevent.Graphics.DrawString(LeftText, font, brush, rect, sf)
sf.Alignment = StringAlignment.Far
pevent.Graphics.DrawString(RightText, Me.Font, brush, rect, sf)
End Using
End Using
End Sub
End Class
Form
Public Class Form1
Private Sub Form1_Load_1(sender As Object, e As EventArgs) Handles MyBase.Load
Dim button As XRadioButton = New XRadioButton()
button.LeftText = "Hello"
button.RightText = "test"
Me.Controls.Add(button)
End Sub
End Class
Result:
这篇关于如何在 vb.net 中的单选按钮选项文本上使用不同的字体样式(粗体和常规)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文