none
How make bold in string in vb net RRS feed

  • Question

  • hai , help what is code make string to bold like this code
    dim asd as string
    asd = "how this text bold"


    • Edited by john 1123 Tuesday, June 6, 2017 6:30 PM
    Tuesday, June 6, 2017 6:28 PM

Answers

  • hai , help what is code make string to bold like this code
    dim asd as string
    asd = "how this text bold"


    A string cannot be bold, only its representation can be bold

    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.
    Sample Code:

    Public Class Form1
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim Target As String = ""
            RichTextBox1.Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " & _
                                "Duis est lacus, egestas non odio eu, rutrum porta libero. " & _
                                "Nullam eget volutpat dui. Sed id nibh id ex consequat ultricies. " &
                                "Vestibulum a nisl eu massa pharetra egestas. Nunc eleifend ultricies viverra. " & _
                                "Cras consequat lacinia metus, sollicitudin malesuada velit blandit lobortis. Donec id dolor lacus."
            Target = "egestas non odio"
            RichTextBox1.SelectionStart = RichTextBox1.Text.IndexOf(Target)
            RichTextBox1.SelectionLength = Target.Length
            RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold Or FontStyle.Strikeout)
    
            Target = "lacinia metus"
            RichTextBox1.SelectionStart = RichTextBox1.Text.IndexOf(Target)
            RichTextBox1.SelectionLength = Target.Length
            RichTextBox1.SelectionFont = New Font("Consolas", 24, FontStyle.Bold Or FontStyle.Underline)
            RichTextBox1.SelectionColor = Color.Green
    
            Target = "Donec id dolor lacus."
            RichTextBox1.SelectionStart = RichTextBox1.Text.IndexOf(Target)
            RichTextBox1.SelectionLength = Target.Length
            RichTextBox1.SelectionFont = New Font("Consolas", 8)
            RichTextBox1.SelectionColor = Color.Blue
    
            RichTextBox1.SelectionStart = 0
            RichTextBox1.SelectionLength = 0
        End Sub
    End Class
    

    Results:

    • Marked as answer by john 1123 Wednesday, June 7, 2017 4:19 AM
    Tuesday, June 6, 2017 7:14 PM

All replies

  • To display a bold text in a TextBox, use a RichTextBox

    Tuesday, June 6, 2017 6:35 PM
  • I dont know what it means?

    Tuesday, June 6, 2017 6:50 PM
  • If you want to configure for HTML "<stong>how</strong> this text bold". If this does not meet your needs please elaborate to exact what you want.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, June 6, 2017 6:56 PM
    Moderator
  • hai , help what is code make string to bold like this code
    dim asd as string
    asd = "how this text bold"


    A string is a string.

    Formatting is rendering information, but the string is still just a string.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Tuesday, June 6, 2017 7:04 PM
  • A string in memory which you make cannot get decoration. 

    If you want to present it bold then you need something which does that, likewise Karen shows. 

    If you want to store it as a kind of formatted text, then you need a format which can hold that like RTF, which Castorix shows. 

    Be aware there are more ways than these two alone, but what you want with that code alone, won't go.


    Success
    Cor

    Tuesday, June 6, 2017 7:05 PM
  • hai , help what is code make string to bold like this code
    dim asd as string
    asd = "how this text bold"


    A string cannot be bold, only its representation can be bold

    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.
    Sample Code:

    Public Class Form1
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim Target As String = ""
            RichTextBox1.Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " & _
                                "Duis est lacus, egestas non odio eu, rutrum porta libero. " & _
                                "Nullam eget volutpat dui. Sed id nibh id ex consequat ultricies. " &
                                "Vestibulum a nisl eu massa pharetra egestas. Nunc eleifend ultricies viverra. " & _
                                "Cras consequat lacinia metus, sollicitudin malesuada velit blandit lobortis. Donec id dolor lacus."
            Target = "egestas non odio"
            RichTextBox1.SelectionStart = RichTextBox1.Text.IndexOf(Target)
            RichTextBox1.SelectionLength = Target.Length
            RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold Or FontStyle.Strikeout)
    
            Target = "lacinia metus"
            RichTextBox1.SelectionStart = RichTextBox1.Text.IndexOf(Target)
            RichTextBox1.SelectionLength = Target.Length
            RichTextBox1.SelectionFont = New Font("Consolas", 24, FontStyle.Bold Or FontStyle.Underline)
            RichTextBox1.SelectionColor = Color.Green
    
            Target = "Donec id dolor lacus."
            RichTextBox1.SelectionStart = RichTextBox1.Text.IndexOf(Target)
            RichTextBox1.SelectionLength = Target.Length
            RichTextBox1.SelectionFont = New Font("Consolas", 8)
            RichTextBox1.SelectionColor = Color.Blue
    
            RichTextBox1.SelectionStart = 0
            RichTextBox1.SelectionLength = 0
        End Sub
    End Class
    

    Results:

    • Marked as answer by john 1123 Wednesday, June 7, 2017 4:19 AM
    Tuesday, June 6, 2017 7:14 PM
  • Hi john,

    You may not make bold a part of text for textbox, but you can make the entire text bold in textbox like this:

    TextBox1.Font = New Font(TextBox1.Font, FontStyle.Bold)

    Dim asd As String
            asd = "how this text bold"
            TextBox1.Text = asd
            TextBox1.Font = New Font(TextBox1.Font, FontStyle.Bold)

    In RichTextBox, it provides a SelectionFont property and you can perform a partial formatting like this:

    ' Make selected text bold programmatically:

    RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)

    RichTextBox1.ReadOnly = False
            RichTextBox1.Text = "We think your costs will be $1200.00"
            Dim selectionStart As Integer
            Dim selectionLength As Integer
            If RichTextBox1.Text.Contains("$") Then selectionStart = RichTextBox1.Text.IndexOf("$")
            selectionLength = RichTextBox1.Text.Length - selectionStart
            RichTextBox1.SelectionStart = selectionStart
            RichTextBox1.SelectionLength = selectionLength
            RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)
            RichTextBox1.ReadOnly = True

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 7, 2017 5:43 AM
    Moderator