RichTextBox Colors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AttiDude

    #1

    RichTextBox Colors

    Hi, I have some difficulties when adding text to a
    (multiline)rich textbox, to change the color of that line only.
    Changing colors on parts of text you want, need to be selected to
    change color. So I need to create a code that selects the text I just
    added to the richtextbox for a very short period so the user doesn't
    notices.
  • mayayana

    #2
    Re: RichTextBox Colors

    Where Start1 is the offset of the first character in the line:

    With RTB
    .SelStart = Start1
    .Span vbCr, True, True '-- selects line up to carriage return..
    .SelColor = 255 '-- [long color value - in this case
    red.]
    .SelLength = 0 '-- de-select line
    End With
    --
    --
    AttiDude <attidude1*nosp am*@hotmail.com > wrote in message
    news:pcsf709n48 oqq0hc7h0sj7nl0 opn50onha@4ax.c om...[color=blue]
    > Hi, I have some difficulties when adding text to a
    > (multiline)rich textbox, to change the color of that line only.
    > Changing colors on parts of text you want, need to be selected to
    > change color. So I need to create a code that selects the text I just
    > added to the richtextbox for a very short period so the user doesn't
    > notices.[/color]


    Comment

    • AttiDude

      #3
      Re: RichTextBox Colors

      Ok I've got now:

      Private Sub Command1_Click( )
      start1 = Len(RichTextBox 1.Text)
      RichTextBox1.Te xt = RichTextBox1.Te xt & Text1.Text
      With RichTextBox1
      .SelStart = start1
      .Span vbCr, True, True
      .SelColor = 255
      .SelLength = 0
      End With
      End Sub

      This way the richtextbox always makes the last added text red. So if I
      add a new text the old text turns black again.

      Is there a way to keep the previous text the curremt color?


      On Sat, 10 Apr 2004 15:13:52 GMT, "mayayana"
      <mayaXXyaYYna1a @mindZZspring.c om> wrote:
      [color=blue]
      >Where Start1 is the offset of the first character in the line:
      >
      >With RTB
      > .SelStart = Start1
      > .Span vbCr, True, True '-- selects line up to carriage return..
      > .SelColor = 255 '-- [long color value - in this case
      >red.]
      > .SelLength = 0 '-- de-select line
      >End With
      >--[/color]

      Comment

      • Rick Rothstein

        #4
        Re: RichTextBox Colors

        > RichTextBox1.Te xt = RichTextBox1.Te xt & Text1.Text

        You don't want to use the above line because that replace all the current
        text (colors, bolding, etc.) with the Text (which is not formatted, Text is
        just the characters) already in the RichTextBox plus those characters in the
        regular TextBox. Anyway, the proper (read... fastest) way is to "insert" the
        new text into the location of the text cursor. You do that by assigning
        (only) the new text to the SelText property. If you first change the
        SelColor property, then the newly inserted text will be that color. However,
        you must remember to set the SelColor back to the color it had before you
        changed it. Give this code a try instead of what you posted.

        Private Sub Command1_Click( )
        Dim CurrentColor As Long
        With RichTextBox1
        CurrentColor = .SelColor
        .SelColor = vbRed
        .SelText = Text1.Text
        .SelColor = CurrentColor
        End With
        End Sub

        Note that this will insert the text in the regular TextBox at the current
        text cursor location, wherever that might be. If you want it to always be
        added to the end of the current text, then add this line somewhere IN FRONT
        OF the .SelColor=vbRed line (but after the With statement).

        ..SelStart = Len(.Text)

        Rick - MVP


        "AttiDude" <attidude1*nosp am*@hotmail.com > wrote in message
        news:ds4g705rmf e0v5f06c40pebk1 59prl437f@4ax.c om...[color=blue]
        > Ok I've got now:
        >
        > Private Sub Command1_Click( )
        > start1 = Len(RichTextBox 1.Text)
        > RichTextBox1.Te xt = RichTextBox1.Te xt & Text1.Text
        > With RichTextBox1
        > .SelStart = start1
        > .Span vbCr, True, True
        > .SelColor = 255
        > .SelLength = 0
        > End With
        > End Sub
        >
        > This way the richtextbox always makes the last added text red. So if I
        > add a new text the old text turns black again.
        >
        > Is there a way to keep the previous text the curremt color?
        >
        >
        > On Sat, 10 Apr 2004 15:13:52 GMT, "mayayana"
        > <mayaXXyaYYna1a @mindZZspring.c om> wrote:
        >[color=green]
        > >Where Start1 is the offset of the first character in the line:
        > >
        > >With RTB
        > > .SelStart = Start1
        > > .Span vbCr, True, True '-- selects line up to carriage return..
        > > .SelColor = 255 '-- [long color value - in this case
        > >red.]
        > > .SelLength = 0 '-- de-select line
        > >End With
        > >--[/color]
        >[/color]


        Comment

        Working...