Results 1 to 8 of 8

Thread: [RESOLVED] Rich Text Box : .txt file display upon loading / updating

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    403

    Resolved [RESOLVED] Rich Text Box : .txt file display upon loading / updating

    A RTB is being used to load and display a text file. Then extra lines are written to the RTB (at the end) by the program. Finally the updated content of the RTB is written back into the source file. So far so good, it works perfectly.

    The simple query is how to make the content of the RTB constantly scroll up as each new line is added - thus the bottom of the RTB would always show the last line which has been added (ie. the end of the text) At present each time the RTB content is updated the first line of content is at the top of the RTB, but the last (latest) line is way below the bottom. It can be found using the vertical scrollbar, but as soon as the program updates the RTB it reverts to the "standard" display of first line at the top.

    I would appreciate any suggestion, or pointer to where to read up on this. I tried MSDN but without success.

    camoore

    Wales, UK

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Rich Text Box : .txt file display upon loading / updating

    You will need to play around with the actual scrolling part.
    Code:
    Option Explicit
    
    Private Declare Function SendMessage Lib "user32" Alias _
    "SendMessageA" _
    (ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any) As Long 
    
    Private Const EM_LINESCROLL = &HB6
    
    Private lScroll As Long
    Private lLines As Long
    
    'put this in an appropriate spot in your code that loads the richtextbox line by line
    lScroll = SendMessage(RichTextBox1.hWnd, EM_LINESCROLL, 0, ByVal lLines)
    You will also need to play around with lLines. For a first run set it to 1 (so it scrolls one line), but you may want to increase that.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    403

    Re: Rich Text Box : .txt file display upon loading / updating

    Thank you Hack. I have tried this routine, but it does not work for me. However it does not produce any errors either. The program continues to work as before - I update the RTB and it goes back to displaying the first line, not the last (most recent).

    The code works like this :

    ASTRINGVAR = Richtextbox1.text 'sets ASTRINGVAR to content of RTB1

    ASTRINGVAR = ASTRINGVAR & "*************************" & Chr(13)'add new line

    'the program determines what "*************************" should be

    Richtextbox1.text = ASTRINGVAR

    At each iteration, it follows that RTB1 gets more and more full until the latter part of ASTRINGVAR is no longer visible in the RTB, because it is bigger than the RTB size limit.

    So the problem is how somehow to make the RTB load ASTRINGVAR "back to front" such that the last line of it is visible (and preceeding lines) but only to the extent of the capacity of the RTB. At runtime I only want to see the last few lines of the data, including the latest entered.

    Can you please advise where/how in this code I should enter the final line of your suggestion.

    camoore

    Wales, UK

  4. #4
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Rich Text Box : .txt file display upon loading / updating

    Why don't you parse your ASTRINGVAR and retrieve only the last lines and show it in the RichTextBox? You can use MID$ or RIGHT$, or perhaps SPLIT if you want.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Rich Text Box : .txt file display upon loading / updating

    in the richtext box change event you can set the selstart to the end
    vb Code:
    1. rtb.selstart = len(rtb.text)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Rich Text Box : .txt file display upon loading / updating

    Instead of appending the values to a variable can you try appending it directly to the RichTextBox if it makes any difference?
    Code:
    RichTextBox1.SelStart = Len(RichTextBox1.Text)
    RichTextBox1.SelText vbNewLine & "*************************"
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    403

    Re: Rich Text Box : .txt file display upon loading / updating

    Thank you as always for such rapid replies.

    The approach I followed was that suggested by dee-u, which also occurred to me a few hours after my last post. This works very well (ie. just parse out the last **** characters and display only these in RTB1).

    A problem was that I needed to save the entire data file, each time it was re-written, to a source .txt file. For that I needed the whole file to be in a RTB. so as to be able to use the savefile.path,rtf text routine.

    The simple solution was to create a second (physically tiny) hidden Rich Text Box (RTB2). I write the Mid$ parsed end of string data (last 1000 characters in this case) into the main RT box (1) for display to user and the whole of the data into a hidden RT box (2). Then it is from the hidden RT (2) box that the source file.txt is updated each time.

    For information, the application is a tidegauge. RS232 data is received via a MSCOMM and parsed into its constituent elements. Every 30 seconds, or so, these elements are to be logged into a .txt file serving as a database. Those parsed elements are contained in Texts 15,14,53,2 and 51.

    Code follows :


    Dim PATH as String

    Path = C:\SPEEDPLOTTIDEDATA.txt

    Dim SPEEDPLOTTIDEDATA as STRING 'current value of .txt file (data file)

    Dim DATASTRINGLEN as Single 'will be length of SPEEDPLOTTIDEDATA


    '(Note below that Text 15, 14, 53, 2, 51 are updated data values in the program).

    SPEEDPLOTTIDEDATA = SPEEDPLOTTIDEDATA & "#DATA, DATE:" & Text15 & ", TIME:" & Text14

    SPEEDPLOTTIDEDATA = SPEEDPLOTTIDEDATA & ", SPEED:" & Text53 & ", DEPTH:" & Text2

    SPEEDPLOTTIDEDATA = SPEEDPLOTTIDEDATA & ", TEMP(C):" & Text51 & "$" & Chr(13) & Chr(13)

    DATASTRINGLEN = Len(SPEEDPLOTTIDEDATA)

    Dim CHARNUM As Single

    Dim CHARNUM1 As Single

    CHARNUM1 = DATASTRINGLEN

    CHARNUM = (CHARNUM1 - 1000) '1000 characters may be displayed in the current size of RTB 1

    If CHARNUM < 1 Then CHARNUM = 1 'this to avoid errors if Charnum <1

    RichTextBox1 = Mid$(SPEEDPLOTTIDEDATA, CHARNUM, CHARNUM1)

    RichTextBox2.Text = SPEEDPLOTTIDEDATA

    PATH = "C:\SPEEDPLOTTIDEDATA.txt"

    RichTextBox2.SaveFile PATH, rtfText 'save the RT Box (2) content back to the source file

    'end of illustrative program



    Thanks to all, I will mark thread resolved.


    camoore

    Wales. UK
    Last edited by camoore; Sep 5th, 2009 at 06:52 PM. Reason: I incorrectly referred to Rich Text Files. This should have been Rich Text Boxes. Corrected, in case it causes any confusion

  8. #8
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: [RESOLVED] Rich Text Box : .txt file display upon loading / updating

    Glad to be of help. =)
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

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



Click Here to Expand Forum to Full Width