Underline a Portion of Label Text

  • Thread starter Thread starter pooba53
  • Start date Start date
P

pooba53

Is there a way to underline a portion of a label text within VB .NET
2003? I'd appreciate the tip.

Thanks.
 
Is there a way to underline a portion of a label text within VB .NET
2003? I'd appreciate the tip.

Thanks.

Not that I know of. Setting the font-style to include underline would
underline the entire text.
The Label control has no OwnerDraw mode, so you can't really go that
route with the Label control itslef.
I would look at using the RichTextBox, which makes it easy to
understand specific portions.
Just modify the style so it looks like a label (no borders, etc).

// CHRIS
 
That looks promising. I'm not sure, however, how to set the specific
parts of the text to underlined.
 
Maybe this will help
Imports System.ComponentModel

<ToolboxBitmap(GetType(ctrl_verticalLabel), "VertLabel.bmp")> _

Public Class ctrl_verticalLabel

'Since we are not using the additional resources/capabilities of

'UserControl we will inherit from Control instead to save overhead

'Inherits System.Windows.Forms.UserControl

Inherits System.Windows.Forms.Control

Private labelText As String

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

End Sub

'UserControl overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.Container

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

<System.Diagnostics.DebuggerStepThrough()> _

Private Sub InitializeComponent()

'

'VerticalLabel

'

Me.Size = New System.Drawing.Size(24, 100)

End Sub

#End Region

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)

Dim sngControlWidth As Single

Dim sngControlHeight As Single

Dim sngTransformX As Single

Dim sngTransformY As Single

Dim labelColor As Color

Dim labelBorderPen As New Pen(labelColor, 0)

Dim labelBackColorBrush As New SolidBrush(labelColor)

Dim labelForeColorBrush As New SolidBrush(MyBase.ForeColor)

MyBase.OnPaint(e)

sngControlWidth = Me.Size.Width

sngControlHeight = Me.Size.Height

e.Graphics.DrawRectangle(labelBorderPen, 0, 0, _

sngControlWidth, sngControlHeight)

e.Graphics.FillRectangle(labelBackColorBrush, 0, _

0, sngControlWidth, sngControlHeight)

' set the translation point for the

' graphics object - the new (0,0) location

sngTransformX = 0

sngTransformY = sngControlHeight

' translate the origin used for rotation and drawing

e.Graphics.TranslateTransform(sngTransformX, _

sngTransformY) ' (0, textwidth)

'set the rotation angle for vertical text

e.Graphics.RotateTransform(270)

' draw the text on the control

e.Graphics.DrawString(labelText, Font, _

labelForeColorBrush, 0, 0)

End Sub

<Category("ctrl_verticalLabel"), Description("Text is displayed vertically
in container")> _

Public Overrides Property Text() As String

Get

Return labelText

End Get

Set(ByVal Value As String)

labelText = Value

Invalidate()

End Set

End Property

End Class



Of course YOU WILL NEED TO ADD SOME PROPERTYS

StartUnderline and EndUnderline

good luck
 
Oh, good God...that seems like too much just to underline one word out
of a sentence representing a label ;-)

Me thinks I'll just tell the client "MS does not make an easy method
of underlining select words within a text label and that it will cost
too much in my time to get that word underlined." Let's just leave as
is.

Sheesh.
 
Oh, good God...that seems like too much just to underline one word out
of a sentence representing a label ;-)

Me thinks I'll just tell the client "MS does not make an easy method
of underlining select words within a text label and that it will cost
too much in my time to get that word underlined." Let's just leave as
is.

Sheesh.

You can try a Rich Text Box. Drop one on your form, then:

- give it a name, I'll call it RichTextBox1
- set BorderStyle to None
- set Text property to "I'd like to underline one of these words."
- set ReadOnly to True
- set TabStop to False

Handle the Form.Load event and put this in there:

Dim szWordToUnderline As String = "underline"
Dim szText As String = Me.RichTextBox1.Text
Me.RichTextBox1.Select(szText.IndexOf(szWordToUnderline),
szWordToUnderline.Length())
Me.RichTextBox1.SelectionFont = New Font(Me.RichTextBox1.Font,
FontStyle.Underline)
Me.RichTextBox1.Select(0, 0)

After that, you can call MsgBox(Me.RichTextBox1.Rtf()) to see what the
raw Rich Text looks like and if you want you can remove the code above
and just set the RichTextBox1.Rtf() property to the rich text
formatting.

After I run it, this is the RTF it generates:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0
Microsoft Sans Serif;}}
\viewkind4\uc1\pard\f0\fs17 I'd like to \ul underline\ulnone one of
these words.\par
}

So you could accomplish the same thing with the following code:

Dim SB As StringBuilder = New StringBuilder(256)
SB.Append("{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil
\fcharset0 Microsoft Sans Serif;}}")
SB.Append("\viewkind4\uc1\pard\f0\fs17 I'd like to \ul underline
\ulnone one of these words.\par}"
Me.RichTextBox1.Rtf() = SB.ToString()

Another option for figuring out what RTF to apply, you can always fire
up WordPad from the Windows Accessories. Define the formatting you
want there, then Save As .. to a .RTF file. Then open that RTF file
in a text editor (like Notepad) and you'll see the RTF formatting,
just like above.

I've used this technique myself ..

// CHRIS
 
Oh, good God...that seems like too much just to underline one word out
of a sentence representing a label ;-)

Me thinks I'll just tell the client "MS does not make an easy method
of underlining select words within a text label and that it will cost
too much in my time to get that word underlined." Let's just leave as
is.

Sheesh.

If you don't want to create your own label control, then draw the text
directly on the form in the OnPaint event. Use the
Graphics.DrawString method. You can draw part of the string using an
underlined font.

Chris
 
Back
Top