Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a bold text in Rich TextBox programatically using VB.NET

I have this code:

print_text.Text = "Patient number: " + ds.Tables("patients").Rows(0).Item(0)
print_text.AppendText(Environment.NewLine)
print_text.Text = print_text.Text + "Last name: " + ds.Tables("patients").Rows(0).Item(1)
print_text.AppendText(Environment.NewLine)

Now the above data i am adding programatically and it works fine. However in the above code i want to add Patient number and Last name in bold font.

like image 442
Django Anonymous Avatar asked Oct 08 '12 04:10

Django Anonymous


People also ask

How do I make text bold in RichTextBox vb net?

Answers. As was stated. you can use a RichTextBox or a plain TextBox with the Font Property set to Bold. The advantage (possibly) of a RichTextBox is that you can choose what text is displayed as Bold, or a different color.

How to change font Color in RichTextBox c#?

Richtextbox font colorForeColor = Color. FromArgb(255, 108, 105, 105);

How do I make text bold in RTF?

In order to make the text bold you just need to surround the text with \b and use the Rtf member.

How do I make text bold in RichTextBox C#?

SelectionFont = new Font(textBox. Font, FontStyle. Bold); The first message, it works perfectly fine, the name is in bold.


2 Answers

When using a RichTextBox, why not just use RTF?


Example:

Sub Main
    Dim f = new Form()
    Dim print_text = new RichTextBox() With {.Dock = DockStyle.Fill}
    f.Controls.Add(print_text)

    Dim sb = new System.Text.StringBuilder()
    sb.Append("{\rtf1\ansi")
    sb.Append("This number is bold: \b 123\b0 ! Yes, it is...")
    sb.Append("}")
    print_text.Rtf = sb.ToString()

    f.ShowDialog()
End Sub

Result:

RichTextBox with bold text

MSDN


This way, you can also easily wrap the RTF stuff into extension methods:

Module RtfExtensions

    <Extension()>
    Public Function ToRtf(s As String) As String
        Return "{\rtf1\ansi" + s + "}"
    End Function

    <Extension()>
    Public Function ToBold(s As String) As String
        Return String.Format("\b {0}\b0 ", s)
    End Function

End Module

and use it like

Dim text = "This number is bold: " + "123".ToBold() + "! Yes, it is..."
print_text.Rtf = text.ToRtf()
like image 87
sloth Avatar answered Nov 14 '22 23:11

sloth


Use the RichTextBox.SelectionFont Property.
Check these MSDN Links on how to do this: Link 1 and Link 2

Hope it helps.
EDIT:

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim len As Integer
    RichTextBox1.Text = "Patient number: " + " 12345"
    RichTextBox1.SelectionStart = 0
    RichTextBox1.SelectionLength = "Patient number".Length
    RichTextBox1.SelectionFont = New Font("Arial", 12, FontStyle.Bold)
    RichTextBox1.SelectionLength = 0
    RichTextBox1.AppendText(Environment.NewLine)
    len = RichTextBox1.Text.Length
    RichTextBox1.AppendText("Last name: " + " ABCD")
    RichTextBox1.SelectionStart = len
    RichTextBox1.SelectionLength = "Last name".Length
    RichTextBox1.SelectionFont = New Font("Arial", 12, FontStyle.Bold)
    RichTextBox1.SelectionLength = 0
End Sub
like image 35
Saurabh R S Avatar answered Nov 14 '22 21:11

Saurabh R S