[RESOLVED] Dynamically loaded RichTextBox can not set scrollbars property.-VBForums
Results 1 to 22 of 22

Thread: [RESOLVED] Dynamically loaded RichTextBox can not set scrollbars property.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2012
    Posts
    200

    Resolved [RESOLVED] Dynamically loaded RichTextBox can not set scrollbars property.

    I need to dynamically add the RichTextBox control on the form, but I can not set some properties of the RichTextBox during runtime, such as Appearance and ScrollBars. The system prompts that these properties are read-only.

    Although my new version will use HTML instead of Richtextbox, the existing version still needs to use Richtextbox.

    Is there any way to set these read-only properties such as the ScrollBars during runtime? Thanks in advance.

    Code:
    Option Explicit
    
    Private WithEvents RichTextBox1 As RichTextBox
    
    Private Sub Command1_Click()
        AddRichTextControl
    End Sub
    
    Private Sub AddRichTextControl()
        On Error GoTo ErrAdd
        
        Set RichTextBox1 = Me.Controls.Add("RICHTEXT.RichtextCtrl.1", "RichTextBox1")
        
        With RichTextBox1
            '.Appearance = rtfFlat
            '.BorderStyle =rtfNoBorder
            .ScrollBars = rtfBoth
        End With
        
        Exit Sub
        
    ErrAdd:
        MsgBox "Load Richtext control failed !" & vbCrLf & vbCrLf & Error
    End Sub
    Last edited by dreammanor; Apr 20th, 2017 at 09:43 AM.

  2. #2
    PowerPoster
    Join Date
    Oct 2013
    Posts
    2,492

    Re: Dynamically loaded RichTextBox can not set scrollbars property.

    Why not have the control on your form?
    If you don't want it to be visible then set the .Visible property to False

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2012
    Posts
    200

    Re: Dynamically loaded RichTextBox can not set scrollbars property.

    Quote Originally Posted by Arnoutdv View Post
    Why not have the control on your form?
    If you don't want it to be visible then set the .Visible property to False
    When I use Richtextbox control, there are often memory leaks, such as: out of memory, 0x7c92a3cd memory cannot be read. I suspect that the Richtextox control hasn't been fully freed. So I would like to try to dynamically load Richtextbox , maybe this can be better to release the memory occupied by Richtextbox .
    Last edited by dreammanor; Apr 20th, 2017 at 09:41 AM.

  4. #4
    Addicted Member
    Join Date
    Feb 2017
    Posts
    192

    Re: Dynamically loaded RichTextBox can not set scrollbars property.

    Quote Originally Posted by dreammanor View Post
    When I use Richtextbox control, there are often memory leaks, such as: out of memory, 0x7c92a3cd memory cannot be read. I suspect that the Richtextox control hasn't been fully freed. So I would like to try to dynamically load Richtextbox , maybe this can be better to release the memory occupied by Richtextbox .
    I use the Richtextbox control quite much and I don't get that error.
    I think that you must be doing something that particulary cause it.
    It would be good if you could isolate what it is.

    About the read only properties, you cannot change the properties values at runtime if they are read only, but often you can do the effect by other means.

    To show or hide the scrollbars at runtime there is an API:

    Code:
    Private Declare Function ShowScrollBar Lib "user32" (ByVal hWnd As Long, ByVal wBar As Long, ByVal bShow As Long) As Long
    
    Private Const SB_HORZ = 0
    Private Const SB_VERT = 1
    Private Const SB_BOTH = 3
    Code:
    Call ShowScrollBar(RichTextBox1.hWnd, SB_BOTH, False)
    I don't remeber if when you change the text you need to call it again or not.

  5. #5
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    2,326

    Re: Dynamically loaded RichTextBox can not set scrollbars property.

    I also rather extensively use the RTB, and I've never had any memory leak problems with it.

    However, I must agree that the read-only-during-runtime nature of certain things is often annoying to me.

    Regards,
    Elroy

  6. #6
    Fanatic Member
    Join Date
    Jun 2012
    Posts
    608

    Re: Dynamically loaded RichTextBox can not set scrollbars property.

    I extra did an update today in my RichTextBox control (http://www.vbforums.com/showthread.p...on-controls%29) to allow the ScrollBars property to be changed at run-time. I already allowed it in my TextBoxW control but anyhow not in the RichTextBox control. However, now it's done.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2012
    Posts
    200

    Re: Dynamically loaded RichTextBox can not set scrollbars property.

    Quote Originally Posted by Eduardo- View Post
    I use the Richtextbox control quite much and I don't get that error.
    I think that you must be doing something that particulary cause it.
    It would be good if you could isolate what it is.

    About the read only properties, you cannot change the properties values at runtime if they are read only, but often you can do the effect by other means.

    To show or hide the scrollbars at runtime there is an API:

    Code:
    Private Declare Function ShowScrollBar Lib "user32" (ByVal hWnd As Long, ByVal wBar As Long, ByVal bShow As Long) As Long
    
    Private Const SB_HORZ = 0
    Private Const SB_VERT = 1
    Private Const SB_BOTH = 3
    Code:
    Call ShowScrollBar(RichTextBox1.hWnd, SB_BOTH, False)
    I don't remeber if when you change the text you need to call it again or not.
    Eduardo, I have tested your method, it is feasible. There are only two minor questions:

    1. The scrollbar is a classic style, not a XP style or win10 style.
    2. The scrollbar can not be displayed automatically.

    Perhaps this method can be used as an alternative. Thank you very much, Eduardo.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Sep 2012
    Posts
    200

    Re: Dynamically loaded RichTextBox can not set scrollbars property.

    Quote Originally Posted by Krool View Post
    I extra did an update today in my RichTextBox control (http://www.vbforums.com/showthread.p...on-controls%29) to allow the ScrollBars property to be changed at run-time. I already allowed it in my TextBoxW control but anyhow not in the RichTextBox control. However, now it's done.
    Krool, I've tested your control, its scrollbar property can be changed at run-time, it is really great. But my project is not allowed to use subclass, I'm wondering if we can change the scrollbar property without subclass.

    I wrote the following code to change the scrollbar property, but without success. Could you help me find out where the problem is? Thanks very much.

    Code:
    Option Explicit
    
    Private Const WM_USER As Long = &H400
    Private Const GWL_STYLE As Long = (-16)
    Private Const GWL_EXSTYLE As Long = (-20)
    Private Const WS_HSCROLL As Long = &H100000
    Private Const WS_VSCROLL As Long = &H200000
    Private Const WM_HSCROLL As Long = &H114
    Private Const WM_VSCROLL As Long = &H115
    Private Const EM_SCROLL As Long = &HB5
    Private Const EM_LINESCROLL As Long = &HB6
    Private Const EM_SCROLLCARET As Long = &HB7
    Private Const EM_EXGETSEL As Long = (WM_USER + 52)
    Private Const ES_AUTOHSCROLL As Long = &H80
    Private Const ES_AUTOVSCROLL As Long = &H40
    
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongW" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongW" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    
    Private Sub Command1_Click()
       SetRichTextBoxScrollbars
    End Sub
    
    Private Sub SetRichTextBoxScrollbars()
        Dim dwStyle As Long, dwExStyle As Long, hRTBHandle As Long
        
        hRTBHandle = RichTextBox1.hwnd
        
        dwStyle = GetWindowLong(hRTBHandle, GWL_STYLE)
        dwExStyle = GetWindowLong(hRTBHandle, GWL_EXSTYLE)
            
        dwStyle = dwStyle Or WS_HSCROLL Or WS_VSCROLL Or ES_AUTOVSCROLL Or ES_AUTOHSCROLL
        
        SetWindowLong hRTBHandle, GWL_STYLE, dwStyle
        SetWindowLong hRTBHandle, GWL_EXSTYLE, dwExStyle
        
    End Sub

  9. #9
    Addicted Member
    Join Date
    Feb 2017
    Posts
    192

    Re: Dynamically loaded RichTextBox can not set scrollbars property.

    Quote Originally Posted by dreammanor View Post
    Eduardo, I have tested your method, it is feasible. There are only two minor questions:

    1. The scrollbar is a classic style, not a XP style or win10 style.
    2. The scrollbar can not be displayed automatically.

    Perhaps this method can be used as an alternative. Thank you very much, Eduardo.
    I see the problem.
    Better to use the Krool's control.
    You can compile the control in a separate OCX project, doing so it doesn't matter the subclass.

  10. #10
    Fanatic Member
    Join Date
    Jun 2012
    Posts
    608

    Re: Dynamically loaded RichTextBox can not set scrollbars property.

    Quote Originally Posted by Eduardo- View Post
    I see the problem.
    Better to use the Krool's control.
    You can compile the control in a separate OCX project, doing so it doesn't matter the subclass.
    Please use the pre-compiled VBCCR14.OCX.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Sep 2012
    Posts
    200

    Re: Dynamically loaded RichTextBox can not set scrollbars property.

    Well, using the pre-compiled VBCCR14.OCX is a good idea. But the scrollBars property can't be changed at run-time in the VBCCR14.OCX. Because the latest CCR14.OCX date is 19-Apr-2017. What should I do?

  12. #12
    Fanatic Member
    Join Date
    Jun 2012
    Posts
    608

    Re: Dynamically loaded RichTextBox can not set scrollbars property.

    Quote Originally Posted by dreammanor View Post
    Well, using the pre-compiled VBCCR14.OCX is a good idea. But the scrollBars property can't be changed at run-time in the VBCCR14.OCX. Because the latest CCR14.OCX date is 19-Apr-2017. What should I do?
    Haha. Ok... I will make an update for VBCCR14.OCX soon.

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Sep 2012
    Posts
    200

    Re: Dynamically loaded RichTextBox can not set scrollbars property.

    Much appreciated, Krool.

  14. #14
    Fanatic Member
    Join Date
    Jun 2012
    Posts
    608

    Re: Dynamically loaded RichTextBox can not set scrollbars property.

    Done. VBCCR14.OCX is updated.

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Sep 2012
    Posts
    200

    Re: Dynamically loaded RichTextBox can not set scrollbars property.

    I've installed the latest VBCCR14.OCX, it works very well. Thank you very much, Krool.

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Sep 2012
    Posts
    200

    Re: [RESOLVED] Dynamically loaded RichTextBox can not set scrollbars property.

    Hi Krool, I did a further test on your RichTextBox, now I have two problems:
    1. The pictures in rtf were loaded twice
    2. When I use TOM on the RichTextBox, once the form is closed, VB IDE crashes.

    Note:
    VBCCR14.OCX is downloaded from Krool's "ActiveX CommonControls (Replacement of the MS common controls)"
    http://www.vbforums.com/showthread.p...55#post5129155

    The test code is copied fom dilettante's "TOM Get Numbers Pictures Heights 2"
    http://www.vbforums.com/showthread.p...93#post5148193
    Attached Images Attached Images  
    Attached Files Attached Files
    Last edited by dreammanor; Today at 04:34 AM.

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Sep 2012
    Posts
    200

    Re: [RESOLVED] Dynamically loaded RichTextBox can not set scrollbars property.

    @dilettante, I would like to ask you two questions:
    1. How to unattach(stop) TOM interface from RichTextBox when I finished the RichTextBox operation ?
    2. How to use TOM or Window messages to achieve the operations similar to SelStart, SelLength, SelText ?

    Thanks very much !
    Last edited by dreammanor; Today at 04:43 AM.

  18. #18
    PowerPoster
    Join Date
    Feb 2006
    Posts
    16,328

    Re: [RESOLVED] Dynamically loaded RichTextBox can not set scrollbars property.

    Just set the ITextDocument interface reference variable to Nothing.

    ITextDocument.Selection is the user interface Selection, though you can create additional non-UI Selections to operate on the contents without disturbing the user's selected text and insertion point. ITextSelection objects have a number of "move" methods to change what is selected. This is described in your MSDN Library CD documentation. See "Text Object Model Interfaces" there.

    Name:  sshot.png
Views: 14
Size:  11.1 KB

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Sep 2012
    Posts
    200

    Re: [RESOLVED] Dynamically loaded RichTextBox can not set scrollbars property.

    How stupid I am, I thought I needed to use Win API to release the TOM interface. I actually did not think I just need to set the ITextDocument interface reference variable to Nothing.

    Now that I add the following code to the Form_Unload event, the second problem is resolved:

    Code:
    Private Sub Form_Unload(Cancel As Integer)
        Set TextDocument1 = Nothing
    End Sub
    Thanks very much, dilettante. However, one thing I do not understand why MS RichTextBox does not need to release interface variables? Does MS RichTextBox automatically release the TOM interface?

    Edit: Krool's RichTextBox need to release ITomDocument variable, otherwise VBIDE will crash.
    Last edited by dreammanor; Today at 08:55 AM.

  20. #20
    PowerPoster
    Join Date
    Feb 2006
    Posts
    16,328

    Re: [RESOLVED] Dynamically loaded RichTextBox can not set scrollbars property.

    VB always releases object references when they go out of scope. You shouldn't even need to do what you showed above.

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Sep 2012
    Posts
    200

    Re: [RESOLVED] Dynamically loaded RichTextBox can not set scrollbars property.

    For MS RichTextBox, we don't need to release the ITextDocument object variable. However, for Krool's RichTextBox, if we don't release the ITextDocument object variable, VB IDE will crash. I don't know why.

  22. #22
    PowerPoster
    Join Date
    Feb 2006
    Posts
    16,328

    Re: [RESOLVED] Dynamically loaded RichTextBox can not set scrollbars property.

    Maybe he's breaking the law someplace? Probably not handling reference counting properly.

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.