vb.net How to Change A line Color in Textbox.text-VBForums
Results 1 to 5 of 5

Thread: vb.net How to Change A line Color in Textbox.text

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    11

    vb.net How to Change A line Color in Textbox.text

    Hello to all friends

    I want to change the color of a line of command in textbox.text

    for example :

    i use This Command Line For Prit text To textbox :

    Code:
    TextBox8.AppendText(Environment.NewLine & "We Love vbforums ...")
    TextBox8.AppendText("Ok")
    The output of the command is as follows:
    We Love vbforums ...Ok
    For example, I want to get "ok" in red ,Or show the line "We Love vbforums ..." with a different color

    How can we do that?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,845

    Re: vb.net How to Change A line Color in Textbox.text

    You can't do that with a TextBox. It has a ForeColor property that controls the colour of the entire text. If you want multiple colours then you will need to use a RichTextBox, which renders RTF markup much as a WebBrowser renders HTML. There are plenty of explanations and examples of how to colour text in a RichTextBox via code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    11

    Re: vb.net How to Change A line Color in Textbox.text

    Quote Originally Posted by jmcilhinney View Post
    You can't do that with a TextBox. It has a ForeColor property that controls the colour of the entire text. If you want multiple colours then you will need to use a RichTextBox, which renders RTF markup much as a WebBrowser renders HTML. There are plenty of explanations and examples of how to colour text in a RichTextBox via code.
    Well, explain for richbox.
    I do not have a problem with editing forecolor
    I just want to change the forecolor of a line of command
    Thanks

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,845

    Re: vb.net How to Change A line Color in Textbox.text

    As I said:
    Quote Originally Posted by jmcilhinney View Post
    There are plenty of explanations and examples of how to colour text in a RichTextBox via code.
    There's no need for me to repeat what has already been provided a multitude of times before, both here on this site and elsewhere. If you're not prepared to take a couple of minutes to find that information then I guess it mustn't be all that important.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,024

    Re: vb.net How to Change A line Color in Textbox.text

    RichTextBox is a little bit more complex than TextBox, but not much more so. You should spend a little time reading documentation to get familiar with it.

    One downside: unless you happen to know how to generate RTF, the only way you can format text in the RichTextBox is to make a selection, then apply your formatting to that selection. The WPF version and some third party controls have slightly more elegant ways to go about it, but a lot of the same concepts always apply.

    So let's say you want to keep appending text, but you want to change the color each time. Here's what you do.

    Open the RichTextBox documentation and think about the problem.

    We need to append text to the end. Is there any method or property with "Append" in it? Yes! There is an "AppendText()" method. So if we append a newline, then our text, it's the same thing as "adding a line". This problem is solved.
    Code:
    Sub AppendLine(ByVal line As String)
        _rtbOutput.AppendText($"{Environment.NewLine}{line}")
    End Sub
    If you're not using VS 2017, you have to use this syntax:
    Code:
    Sub AppendLine(ByVal line As String)
        Dim formattedLine = String.Format("{0}{1}", Environment.NewLine, line)
        _rtbOutput.AppendText(formattedLine)
    End Sub
    Now we have to figure out how to implement "select the last line". This is where people can get confused.

    A selection has a start and a length. The "start" indicates the character position inside the RichTextBox. The "Length" will be however long that line is. There is a .Lines property that can tell us how many lines there are, but since it's an array of Strings, we can't easily use it to find the index of the first character for a line. So we should search the documentation to see if there's something interesting with "First" or "Line" in it.

    It turns out there is a method .GetFirstCharIndexFromLine(). If we give it the index of a line, it will tell us what the right index for the first character is. It can return -1 if we send it a number that's too big, so we'll want to be ready to handle that.

    To make a selection, it's easiest to call the Select() method, which you should've found if you were asking, "How do I select things?" We can also use the SelectionStart and SelectionLength properties, but it's clunkier to use them for this.
    Code:
    Sub SelectLastLine()
        Dim lastLineIndex = _rtbOutput.Lines.Length - 1
        Dim firstCharIndex = _rtbOutput.GetFirstCharIndexFromLine(lastLineIndex)
        Dim length = _rtbOutput.Lines(lastLineIndex).Length
        If lastLineIndex <> -1 Then
            _rtbOutput.Select(firstCharIndex, length)
        End If
    End Sub
    OK. Now we want to make the text red. We should search to see if there's something with the word "Color" in it. .ForeColor is for the entire control, that's no good. .SelectionBackColor changes the background color of the selection, that's not quite what we want. .SelectionColor seems good!
    Code:
    Sub SetLineColor(ByVal textColor As Color)
        _rtbOutput.SelectionColor = textColor
    End Sub
    One last thing: the text is still selected, so it might look funny. The last thing we should probably do is clear the selection. This is a situation where using SelectionLength makes sense, because I'm about to tell you why you want to leave the cursor where it is. Odds are you also want to guarantee this text is in plain sight. To do that, you put the cursor where you want it and call ScrollToCaret().
    Code:
    Sub ClearSelection()
        _rtbOutput.SelectionLength = 0
    End Sub
    
    Sub ScrollToLine()
        _rtbOutput.ScrollToCaret()
    End Sub
    Now we can say what we mean. When you want to append a colored line:
    Code:
    Sub AppendColoredLine(ByVal text As String, ByVal textColor As Color)
        AppendLine(text)
        SelectLastLine()
        SetLineColor(textColor)
        ClearSelection()
        ScrollToLine()
    End Sub
    Nothing I post is production-ready. It is provided as-is, use it at your own risk.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.