locked
How to scroll a RealTextBox so that current position is at top of page RRS feed

  • Question

  • My program has a feature that finds text in a RTB. Once the text is found, it sits at the last line visible in the RTB. How can I get that line to move to the top of the visible page?

    Robert Homes

    Thursday, December 11, 2014 7:54 PM

Answers

  • Hi,

     There really is no set method to do this so, it is kind of up to your imagination i guess. I found on a test form that if i found the text and then set the selection start 1 line below that and use the scrolltocaret to scroll 1 line past the found text. Then if i found it again it set itself to the top of the RTB instead of the bottom. You need the RTB`s HideSelection property set to False.

     Its a little ugly but, it works on my test app.  8)

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim indx As Integer = RichTextBox1.Find("Text To Find")
            If indx = -1 Then Exit Sub
            Dim line As Integer = RichTextBox1.GetFirstCharIndexFromLine(RichTextBox1.GetLineFromCharIndex(indx) + 1)
            If line = -1 Then line = RichTextBox1.TextLength
            RichTextBox1.SelectionStart = line
            RichTextBox1.ScrollToCaret()
            RichTextBox1.Find("Text To Find")
        End Sub


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Thursday, December 11, 2014 8:55 PM
    • Proposed as answer by Reed KimbleMVP Thursday, December 11, 2014 10:23 PM
    • Marked as answer by Robert Homes Thursday, December 11, 2014 11:13 PM
    • Unmarked as answer by Robert Homes Thursday, December 11, 2014 11:14 PM
    • Marked as answer by Robert Homes Thursday, December 11, 2014 11:17 PM
    Thursday, December 11, 2014 8:54 PM
  • Thanks. Your code made me think of this improvement, just 2 lines:

    rtb.selectionstart = rtb.textlength

    rtb.find("text to find")

    That's good enough for what I'm doing, anyway. Of course, it only works if what you're looking for is the FIRST occurance of "text to find".


    Robert Homes


    • Marked as answer by Robert Homes Thursday, December 11, 2014 11:17 PM
    • Edited by Robert Homes Thursday, December 11, 2014 11:19 PM
    Thursday, December 11, 2014 11:16 PM

All replies

  • Hi,

     There really is no set method to do this so, it is kind of up to your imagination i guess. I found on a test form that if i found the text and then set the selection start 1 line below that and use the scrolltocaret to scroll 1 line past the found text. Then if i found it again it set itself to the top of the RTB instead of the bottom. You need the RTB`s HideSelection property set to False.

     Its a little ugly but, it works on my test app.  8)

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim indx As Integer = RichTextBox1.Find("Text To Find")
            If indx = -1 Then Exit Sub
            Dim line As Integer = RichTextBox1.GetFirstCharIndexFromLine(RichTextBox1.GetLineFromCharIndex(indx) + 1)
            If line = -1 Then line = RichTextBox1.TextLength
            RichTextBox1.SelectionStart = line
            RichTextBox1.ScrollToCaret()
            RichTextBox1.Find("Text To Find")
        End Sub


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Thursday, December 11, 2014 8:55 PM
    • Proposed as answer by Reed KimbleMVP Thursday, December 11, 2014 10:23 PM
    • Marked as answer by Robert Homes Thursday, December 11, 2014 11:13 PM
    • Unmarked as answer by Robert Homes Thursday, December 11, 2014 11:14 PM
    • Marked as answer by Robert Homes Thursday, December 11, 2014 11:17 PM
    Thursday, December 11, 2014 8:54 PM
  • As IronRazerz said, there's no set way to do this.  And as he's shown, there isn't really a *good* way to do it either.  To be honest, the RichTextBox is good at displaying an existing RTF file, is OK at editing simple RTF files, and is down-right lousy at anything else.  Trying to use the RichTextBox for any kind of structured document composition is generally an exercise in futility (and frustration).  The end result is always kludge-y at best; I suppose the exception is when you overcome the limitations by editing the raw RTF directly and using Win32 API calls to interact with the control, but that is typically a ton of work for very little pay-off.

    In most cases where you need more than rudimentary rich-text editing, it is best to forget the RichTextBox and find (or build) a more purpose-specific control.  In many cases Scintilla is the answer.  While it may offer its own challenges in terms of figuring out how to use it correctly, it isn't likely to limit your document editing capabilities (indeed, the biggest problem may be wading through all of the features).

    If the application doesn't warrant the effort of integrating a new control, then you'll probably have to settle for something similar to what was shown.  I wonder how it would work if you just got the start position of the line, moved the selection start to the end, scrolled to caret, moved the selection start to the line start position, and then scrolled to caret again?  It might be a difference of less-slow/more-ugly versus more-slow/less-ugly (either way, its slow and ugly lol).

     

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Thursday, December 11, 2014 10:21 PM
  • Reed:

    I really have no problem with the faults of Real Text files. For this reason: I use them to display what's in them, but not to edit the text in there. I have WordPad, which I think I got with Microsoft Office, or maybe it comes with Windows. I use that to edit any RealText files I need. Then in the program if I want to have an edit feature, I just call WordPad like this:

    Process.Start(<rtf-name>)

    For this to work, you have to set WordPad as the Windows default program to handle RealText files. But it all works great for me.

    I'll look into Scintilla, though. Thanks for that!


    Robert Homes

    Thursday, December 11, 2014 11:10 PM
  • Thanks. Your code made me think of this improvement, just 2 lines:

    rtb.selectionstart = rtb.textlength

    rtb.find("text to find")

    That's good enough for what I'm doing, anyway. Of course, it only works if what you're looking for is the FIRST occurance of "text to find".


    Robert Homes


    • Marked as answer by Robert Homes Thursday, December 11, 2014 11:17 PM
    • Edited by Robert Homes Thursday, December 11, 2014 11:19 PM
    Thursday, December 11, 2014 11:16 PM
  • Reed:

    I really have no problem with the faults of Real Text files. For this reason: I use them to display what's in them, but not to edit the text in there. I have WordPad, which I think I got with Microsoft Office, or maybe it comes with Windows. I use that to edit any RealText files I need. Then in the program if I want to have an edit feature, I just call WordPad like this:

    Process.Start(<rtf-name>)

    For this to work, you have to set WordPad as the Windows default program to handle RealText files. But it all works great for me.

    I'll look into Scintilla, though. Thanks for that!


    Robert Homes

    RTF is Rich Text Format file not Real Text File. RTF is a format. RichTextBox or WordPad can edit/display RTF files. Images in RTF files are in "Meta format" of the binary image information I believe of the Image after conversion to Bitmap but didn't check that to be certain and may be wrong.

    Therefore when RichTextBox1.RTF is saved to a file the file extension should be .RTF. It is saved as TEXT and you can use My.Computer.FileSystem.SaveAllText("C:\Users\John\Desktop\RTB1.RTF", RichTextBox1.RTF, False) to save it. Then you could display that file in WordPad or an RTB using RichTextBox1.RTF = My.Computer.FileSystem.ReadAllText("C:\Users\John\Desktop\RTB1.RTF"). You can open an RTF file in NotePad which will display all of the textual coding in text form that the RTF file uses which is displayed below for the WordPad image below it.

    RTF file created in WordPad but opened in Notepad which does not know RTF formatting. Image of it is below and note that the WordPad window has text, some underscored bolded and red highlighted text and an image in it also. If you copied and pasted the below text into Notepad then saved it as type All Files adding an extension manually of .RTF to the filename being used then brought up WordPad and opened the file or read the file into a RichTextBox using the previous code I displayed the result would be the part of the image below for WordPads text window being displayed.

    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
    {\colortbl ;\red255\green0\blue0;}
    {\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sa200\sl276\slmult1\lang9\f0\fs22 Hello. How are you today?\par
    I am \highlight1\ul\fs44 fine\highlight0\ulnone\fs22 .\par
    \par
    \pard\sa200\sl240\slmult1{\pict\wmetafile8\picw794\pich794\picwgoal450\pichgoal450 
    010009000003ae05000000008505000000000400000003010800050000000b0200000000050000
    000c021e001e00030000001e000400000007010400040000000701040085050000410b2000cc00
    1e001e00000000001e001e0000000000280000001e0000001e0000000100180000000000c80a00
    000000000000000000000000000000000000000000000000000008080800000000000000000008
    080800000000000000000008080800000000000000000008080800000000000000000008080800
    000000000000000008080800000000000000000008080800000000000000000808080000000808
    080000000808080000000808080000000808080000000808080000000808080000000808080000
    000808080000000808080000000808080000000808080000000808080000000808080000000808
    080000000000000000080808000000000000000000080808000000080000000000080808000000
    000000000000080808000000080000000000080808000000080000000000080808000000080000
    000000080808000000080000000000080808000008000000000000000000000008080800000008
    000000000008080000000000000010102908080800000008000000000008080800000008000000
    000008080800000008000000000008080800000000000000000000000000000000000000004239
    39a5a5a5a5a5a50000000000000000000000000000000000000800004242ff0808180000000000
    000000000000000000000000000000000000000000000000000000000000000000006b6b6b8c8c
    8c4242420000000000000000efe7e7a5a5a5ffffff181818000000080000000000080808081010
    2121843139ff101010000000080808000000080000000000080808000000080000000000080808
    000000000000000000e7e7e7efefef8c84840000000000000000dededed6d6d6ffffff39393900
    00000000000800000810313131a52931ef3129ff08100000000000000000000000000000000000
    00000000000000000800000000000000000000008c8c8cd6d6d6b5b5b5efefef39393900008c8c
    8cf7f7f7949494f7f7f7dedede0000000000000000003139b52931b53131ef3131ef2118310000
    00000000000000000000000000080000000000080808000000080000000000737373c6c6c6ffff
    ffffffff9c9c9cadadad0000737373efefefe7e7e7f7f7f7c6c6c69c9c9c0000001008182931ff
    3131c62929c63131ff181863100800cecece9c9c9ca5a5a5737373000000000000000000000000
    000000525252cececeffffffffffffffffffcececeb5b5b50000181818d6ceceffffffffffffff
    ffffb5b5b5a5a59c39428c3129e72129ce3139f72121de394273e7e7defffffffffffff7f7f7ff
    ffff4242420000000000000000008c8c8ce7e7e7ffffffffffff7b7b7b7b7b7bdedede42424200
    000000003939394242426b6b6bbdbdbdffffffe7e7de4a4a942921c63131d62129d618187bf7f7
    dededede8484849494947b7b7bd6d6d6efefef0000002121217b7b7bcececeffffffbdbdbd0000
    000000000000000000000000000000080000000000000000000000000000393939f7f7f7a5adb5
    2121b53129ad1821efadb5a5cecece8484841818100000000808106b6b6bcececea5a5a59c9494
    ffffffffffff737373000000000000080000000000080000000000000000000000000000000008
    0000000000000000000000948c7b3131ce3131a51818d6ceceb594949c7b73736b6363d6d6d68c
    8c8c7373738c8c8cbdbdbdc6c6c6ffffff21212100000000000000000000000008000000000000
    000000000800000000000808080000000800000000000800080000003129732973a52921948c94
    8c5a525ad6d6d6d6d6d6dededec6c6c6b5b5b58c8c8c9c9c9c9c9c9c0808080000000000000800
    000000000808080000000800000000000000000000080000000000000000000000000000000000
    000000217b7b18638c738c7b6b6363a5a5a5ffffff5a5a5a8c8c849c9c9cffffffadadad5a5a5a
    424242000000000000000000000000080000000000000000000000080000000008080800000008
    000000000008080800000000000021217b295ab5739494ffffff7b7b7bf7f7f7ffffff4a4a4a00
    00008c8c8cffffffffffffc6c6c6c6c6c621212100000000000008080800000008000000000008
    080800000000000000000000000000000800000000000000000000002118521821f78c84b58c94
    8c1010101010104a4a52d6d6d6292929efefef313131393139636363ffffffa59c9c0000000000
    000000000000000000000800000000000000000000080000000000080808000000080000000000
    0008000000002129ce2931941810081818182121211010106b6b6bffffff7b7b7b100810212121
    000000dedede212121000000000000080000000000080808000000080000000000000000000008
    00000000000000000000000000000000001008087b73522121de39394a5a635210101018181852
    5252c6c6c6393939101010080808101010bdbdc67b7b7b18101000000000000008000000000000
    000000000008000000000800000000000800000000000000000000005a5a5aa5a5a5d6d6c63942
    ad2129ff5a5ab5ffffef4a52429c949cb5b5b5ffffffadadadb5b5b5ffffffdee7e7bdbdbdadad
    ad2118180000000000000800000000000808080000000000000000000000000000000000000000
    737373a5a5a5ffffffe7e7de2939a53129b52129ff1821c69494d6312921f7f7efffffffffffff
    fffffff7fff7fffff7313984fffff7efefef8c8c8c000000000000000000000000000000000021
    2121737373393939525252dededeffffffffffffadadad0000002921b53139ad3129d62939ff18
    29ff2929f72121186b6bce9494b5a5a5bd8c8cad5252ad1008735a5252ffffffffffffb5b5b521
    21210000000000000000000000a5a5a5ffffffdededec6c6c6efefefffffff736b6b0000000000
    000810003131f72929c62931e72931f72931f72129e71818522121ff1821ff2129ef2129de1818
    4a000000080808ffffffefefefdedededededebdbdbd2121210000393939e7e7e7cececeffffff
    e7e7e729292900000000000008080800000029294a2931ce3131f72931ff2931f72929c63131ce
    29294a2931943131de312984000000000008000000000000adadadcececed6d6d6ffffffc6c6c6
    00004a4a4affffffb5b5b5efefef94949400000000000008000000000000000000000021215a31
    29ce3139ad2131e72939ef2931e72931de3931de29296b00000000000000000008000000000039
    3939ffffffcececec6c6c61818180000313131f7f7f7cececeffffff0000000000000800000000
    000800000000000800080000000808082121732929bd3939de3931d62121841010180000000000
    00000000080000000000000000000000efefefffffffa5a5a500000000000000003131319c9c9c
    424242000000000000000000000000000000000000000000000000000000000000000000000000
    000000000000000000000000000000000000000000000000000000000000101010c6c6c64a4a4a
    000000000008080800000000000000000008080800000008080800000008080800000008080800
    000008080800000008080800000008080800000008080800000008080800000008080800000008
    080800000000000000000000000000000000000000000000000000000808080000000000000000
    000808080000000000000000000808080000000000000000000808080000000000000000000808
    080000000000000000000808080000000000000000000000000000000000000000080808000000
    080808000000080808000000080808000000080808000000080808000000080808000000080808
    000000080808000000080808000000080808000000080808000000080808000000080808000000
    0808080000000000040000002701ffff030000000000
    }\par
    \par
    \pard\sa200\sl276\slmult1\par
    }
     


    La vida loca






    Sunday, December 14, 2014 6:18 AM