Inserting text into a RichTextBox

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

    Inserting text into a RichTextBox

    I have a RTF control on a form and want to insert text programmaticall y. I
    thought it would be easy but it does not turn out that way. Can someone
    refer me to an Internet article that covers this action. When I insert the
    text, the formatting for the other content of the control is lost.

    In summary, I want to add the current date and time to a RTF control while
    maintaining the existing appearance.


  • Herfried K. Wagner [MVP]

    #2
    Re: Inserting text into a RichTextBox

    "genojoe" <genojoe@discus sions.microsoft .com> schrieb:[color=blue]
    > In summary, I want to add the current date and time to a RTF control while
    > maintaining the existing appearance.[/color]

    \\\
    With Me.RichTextBox1
    .Select(100, 0)
    .SelectedRtf = ... ' Or 'SelectedText'.
    End With
    ///

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • genojoe

      #3
      Re: Inserting text into a RichTextBox

      I am at a loss regarding how to use this advise.

      "Herfried K. Wagner [MVP]" wrote:
      [color=blue]
      > \\\
      > With Me.RichTextBox1
      > .Select(100, 0)
      > .SelectedRtf = ... ' Or 'SelectedText'.
      > End With
      > ///
      >
      > --
      > M S Herfried K. Wagner
      > M V P <URL:http://dotnet.mvps.org/>
      > V B <URL:http://classicvb.org/petition/>
      >
      >[/color]

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Inserting text into a RichTextBox

        "genojoe" <genojoe@discus sions.microsoft .com> schrieb:[color=blue]
        >I am at a loss regarding how to use this advise.
        >[color=green]
        >> \\\
        >> With Me.RichTextBox1
        >> .Select(100, 0)
        >> .SelectedRtf = ... ' Or 'SelectedText'.
        >> End With
        >> ///[/color][/color]

        Simply paste the text into the text-editor window. A richtextbox called
        'RichTextBox1' must exist and you will have to replace 100 with the insert
        position and the ellipsis with the text to insert.

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://classicvb.org/petition/>

        Comment

        • genojoe

          #5
          Re: Inserting text into a RichTextBox

          Thank you.
          I finally came up with the following code. I paste it here in case anyone
          else ever reads this sequence. Too bad Microsoft did not show something like
          this in Help for SelectedRtf. Its so simple; all you got to do is try it.

          Debug.WriteLine (Me.rtfUser.Tex t) 'Returns "Date: . Thank you"
          Me.rtfUser.Sele ct(6, 0)
          If Me.chkMakeBold. Checked Then
          Me.rtfUser.Sele ctedRtf = "{\rtf1\an..... .\uc1\b\f0\fs20 5-15-05
          \b0}"
          Else
          Me.rtfUser.Sele ctedText = "5-15-05"
          End If

          I center truncated the SelectedRtf text.



          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: Inserting text into a RichTextBox

            "genojoe" <genojoe@discus sions.microsoft .com> schrieb:[color=blue]
            > I finally came up with the following code. I paste it here in case anyone
            > else ever reads this sequence. Too bad Microsoft did not show something
            > like
            > this in Help for SelectedRtf. Its so simple; all you got to do is try it.
            >
            > Debug.WriteLine (Me.rtfUser.Tex t) 'Returns "Date: . Thank you"
            > Me.rtfUser.Sele ct(6, 0)
            > If Me.chkMakeBold. Checked Then
            > Me.rtfUser.Sele ctedRtf = "{\rtf1\an..... .\uc1\b\f0\fs20
            > 5-15-05
            > \b0}"
            > Else
            > Me.rtfUser.Sele ctedText = "5-15-05"
            > End If[/color]

            You could use the 'Select' method to insert the selected text and make it
            bold by assigning a bold font:

            \\\
            Me.RichTextBox1 .SelectionFont = _
            New Font(Me.RichTex tBox1.Selection Font, FontStyle.Bold)
            ///

            --
            M S Herfried K. Wagner
            M V P <URL:http://dotnet.mvps.org/>
            V B <URL:http://classicvb.org/petition/>

            Comment

            Working...