-
Apr 20th, 2017, 06:43 AM
#1
Thread Starter
PowerPoster
[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.
-
Apr 20th, 2017, 07:35 AM
#2
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
-
Apr 20th, 2017, 07:59 AM
#3
Thread Starter
PowerPoster
Re: Dynamically loaded RichTextBox can not set scrollbars property.
Originally Posted by Arnoutdv
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.
-
Apr 20th, 2017, 12:52 PM
#4
Re: Dynamically loaded RichTextBox can not set scrollbars property.
Originally Posted by dreammanor
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.
-
Apr 20th, 2017, 01:15 PM
#5
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
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Apr 20th, 2017, 04:13 PM
#6
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.
-
Apr 21st, 2017, 11:02 AM
#7
Thread Starter
PowerPoster
Re: Dynamically loaded RichTextBox can not set scrollbars property.
Originally Posted by Eduardo-
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.
-
Apr 21st, 2017, 11:30 AM
#8
Thread Starter
PowerPoster
Re: Dynamically loaded RichTextBox can not set scrollbars property.
Originally Posted by Krool
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
-
Apr 21st, 2017, 12:21 PM
#9
Re: Dynamically loaded RichTextBox can not set scrollbars property.
Originally Posted by dreammanor
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.
-
Apr 21st, 2017, 12:24 PM
#10
Re: Dynamically loaded RichTextBox can not set scrollbars property.
Originally Posted by 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.
Please use the pre-compiled VBCCR14.OCX.
-
Apr 21st, 2017, 12:52 PM
#11
Thread Starter
PowerPoster
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?
-
Apr 21st, 2017, 01:08 PM
#12
Re: Dynamically loaded RichTextBox can not set scrollbars property.
Originally Posted by dreammanor
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.
-
Apr 21st, 2017, 01:25 PM
#13
Thread Starter
PowerPoster
Re: Dynamically loaded RichTextBox can not set scrollbars property.
-
Apr 21st, 2017, 01:55 PM
#14
Re: Dynamically loaded RichTextBox can not set scrollbars property.
Done. VBCCR14.OCX is updated.
-
Apr 21st, 2017, 10:19 PM
#15
Thread Starter
PowerPoster
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.
-
Apr 24th, 2017, 10:30 PM
#16
Thread Starter
PowerPoster
-
Apr 24th, 2017, 11:42 PM
#17
Thread Starter
PowerPoster
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; Apr 25th, 2017 at 04:43 AM.
-
Apr 25th, 2017, 07:01 AM
#18
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.
-
Apr 25th, 2017, 07:43 AM
#19
Thread Starter
PowerPoster
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; Apr 25th, 2017 at 08:55 AM.
-
Apr 25th, 2017, 08:02 AM
#20
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.
-
Apr 25th, 2017, 08:46 AM
#21
Thread Starter
PowerPoster
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.
-
Apr 25th, 2017, 11:20 AM
#22
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|