-
Aug 31st, 2019, 09:31 AM
#1
Thread Starter
New Member
Double Buffered Panel & Transparent RichTextBox / Link VScrollBar to RichTextBox?
Hello!
I found this site recently and it has been a big help, lots of good information here. In the last day or so I've run in to two problems, so sadly my first post here is going to be a request for help.
My first issue was a panel flashing when it was made visible, thanks to info found on this site this was solved by using this:
vb.net Code:
Public Class DoubleBufferedPanel
Inherits Panel
Public Sub New()
Me.DoubleBuffered = True
End Sub
End Class
The second issue is that I needed a RichTextBox that would show the background image of then panel it was located on, again thanks to this site this solved the issue:
vb.net Code:
Imports System.Runtime.InteropServices
Public Class TransparentRichTextBox
Inherits RichTextBox
<DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function LoadLibrary(ByVal lpFileName As String) As IntPtr
End Function
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim params As CreateParams = MyBase.CreateParams
If LoadLibrary("msftedit.dll") <> IntPtr.Zero Then
params.ExStyle = params.ExStyle Or &H20
params.ClassName = "RICHEDIT50W"
End If
Return params
End Get
End Property
End Class
This all seemed good until I noticed a small issue, for some reason when using TransparentRichTextBox on a DoubleBufferedPanel, the scrollbars vanish and will not show up until the mouse hovers over where they should be and they then seem to gracefully fade into view. To rule out anything else in my project I created the small test below:
vb.net Code:
Option Strict On
Option Explicit On
Imports System.Runtime.InteropServices
Public Class Form1
Public Class DoubleBufferedPanel
Inherits Panel
Public Sub New()
Me.DoubleBuffered = True
End Sub
End Class
Public Class TransparentRichTextBox
Inherits RichTextBox
<DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function LoadLibrary(ByVal lpFileName As String) As IntPtr
End Function
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim params As CreateParams = MyBase.CreateParams
If LoadLibrary("msftedit.dll") <> IntPtr.Zero Then
params.ExStyle = params.ExStyle Or &H20
params.ClassName = "RICHEDIT50W"
End If
Return params
End Get
End Property
End Class
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' Form stuff
Me.DoubleBuffered = True
Me.Size = New Size(675, 535)
Me.BackColor = Color.LightGreen
' Create DoubleBufferedPanel
Dim DoubleBufferedPanel1 As New DoubleBufferedPanel
With DoubleBufferedPanel1
.Name = "DoubleBufferedPanel1"
.BackColor = Color.LightBlue
.Width = 640
.Height = 480
.Top = 10
.Left = 10
.BackgroundImage = System.Drawing.Image.FromFile("C:\Windows\Web\Wallpaper\Windows\img0.jpg")
.BackgroundImageLayout = ImageLayout.Stretch
End With
Me.Controls.Add(DoubleBufferedPanel1)
' Create TransparentRichTextBox, fill with text & return to top
Dim TransparentRichTextBox1 As New TransparentRichTextBox
With TransparentRichTextBox1
.Name = "TransparentRichTextBox1"
.Width = 620
.Height = 460
.Top = 10
.Left = 10
.ReadOnly = True
.WordWrap = False
.ScrollBars = RichTextBoxScrollBars.ForcedVertical
End With
For Line As Integer = 1 To 1000
TransparentRichTextBox1.AppendText(String.Format("{0} {1}{2}", "Test Text", Line, vbCrLf))
Next
TransparentRichTextBox1.Select(0, 0)
' Add TransparentRichTextBox1 to DoubleBufferedPanel1
DoubleBufferedPanel1.Controls.Add(TransparentRichTextBox1)
End Sub
End Class
This displays the same behaviour as my main project did even when just using a background colour instead of an image. What I've noticed so far in addition to the what I described above is that if you click in the TransparentRichTextBox and use your mouse wheel to scroll down then the middle portion of the scrollbars appear but the up and down arrows do not.
If anyone can point out where I am going wrong I would appreciate it.
Thanks
Last edited by JackBrig; Sep 1st, 2019 at 06:45 AM.
-
Aug 31st, 2019, 11:50 AM
#2
Thread Starter
New Member
Link VScrollBar to RichTextBox?
Instead of fixing this issue I've been thinking (yes, it did cause some mild pain ), is it possible to link/sync a VScrollBar to a RichTextBox in a way that works well?
There are some examples on other sites but every one I've found is easily confused depending on the text file in question, some confused by blank lines and word wrap etc. If I can bypass the need to use the built in scrollbar of a RichTextBox then that would solve the problem.
Thanks!
Last edited by JackBrig; Aug 31st, 2019 at 01:02 PM.
-
Sep 3rd, 2019, 04:26 PM
#3
Re: Double Buffered Panel & Transparent RichTextBox / Link VScrollBar to RichTextBox?
Did you try setting your rtb scrollbars to none, and your panel autoscroll to true?
You’ll need yo set the size of your rtb so it displays sll of your text...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 4th, 2019, 08:52 AM
#4
Thread Starter
New Member
Re: Double Buffered Panel & Transparent RichTextBox / Link VScrollBar to RichTextBox?
Originally Posted by .paul.
Did you try setting your rtb scrollbars to none, and your panel autoscroll to true?
You’ll need yo set the size of your rtb so it displays sll of your text...
I did give that a try, while it solves the scrollbar display issues it breaks other things. Doing it this way would stop mouse scrolling, when moving the panels scrollbars the background image becomes corrupted and I have to manually set the RichTextBox height in the example but I can't figure out how to reliably autosize the RichTextBox base on the contents.
Due to the layout I'm having to deal with I had to do it like this:
vb.net Code:
Option Strict On Option Explicit On Imports System.Runtime.InteropServices Public Class Form1 Public Class DoubleBufferedPanel Inherits Panel Public Sub New() Me.DoubleBuffered = True End Sub End Class Public Class TransparentRichTextBox Inherits RichTextBox <DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _ Private Shared Function LoadLibrary(ByVal lpFileName As String) As IntPtr End Function Protected Overrides ReadOnly Property CreateParams() As CreateParams Get Dim params As CreateParams = MyBase.CreateParams If LoadLibrary("msftedit.dll") <> IntPtr.Zero Then params.ExStyle = params.ExStyle Or &H20 params.ClassName = "RICHEDIT50W" End If Return params End Get End Property End Class Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load ' Form stuff Me.DoubleBuffered = True Me.Size = New Size(675, 535) Me.BackColor = Color.LightGreen ' Create DoubleBufferedPanel Dim DoubleBufferedPanel1 As New DoubleBufferedPanel With DoubleBufferedPanel1 .Name = "DoubleBufferedPanel1" .BorderStyle = BorderStyle.None .BackColor = Color.LightBlue .Width = 640 .Height = 480 .Top = 10 .Left = 10 .BackgroundImage = System.Drawing.Image.FromFile("C:\Windows\Web\Wallpaper\Windows\img0.jpg") .BackgroundImageLayout = ImageLayout.Stretch End With Me.Controls.Add(DoubleBufferedPanel1) Dim DoubleBufferedPanel2 As New DoubleBufferedPanel With DoubleBufferedPanel2 .Name = "DoubleBufferedPanel2" .BorderStyle = BorderStyle.None .AutoScroll = True .BackColor = Color.Transparent .Width = 620 .Height = 460 .Top = 10 .Left = 10 End With DoubleBufferedPanel1.Controls.Add(DoubleBufferedPanel2) ' Create TransparentRichTextBox, fill with text & return to top Dim TransparentRichTextBox1 As New TransparentRichTextBox With TransparentRichTextBox1 .Name = "TransparentRichTextBox1" .BorderStyle = BorderStyle.None .Width = 603 '640 .Height = 480 .Top = 0 '10 .Left = 0 ' 10 .ReadOnly = True .WordWrap = False .ScrollBars = RichTextBoxScrollBars.None .Height = 5000 End With For Line As Integer = 1 To 1000 TransparentRichTextBox1.AppendText(String.Format("{0} {1}{2}", "Test Text", Line, vbCrLf)) Next TransparentRichTextBox1.Select(0, 0) ' Add TransparentRichTextBox1 to DoubleBufferedPanel1 DoubleBufferedPanel2.Controls.Add(TransparentRichTextBox1) End Sub End Class
Ideally I need to find out what's causing the scrollbar vanishing act in my first example.
Thanks!
-
Sep 4th, 2019, 09:21 AM
#5
Addicted Member
Re: Double Buffered Panel & Transparent RichTextBox / Link VScrollBar to RichTextBox?
Hello,
Sorry to interrupt, but could you try this?
Code:
Option Strict On
Option Explicit On
Imports System.Runtime.InteropServices
Public Class Form1
Private Panel As New System.Windows.Forms.Panel
Private Rtb As New TransparentRichTextBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
With Me
.Controls.Add(Panel)
.Panel.Dock = DockStyle.Fill
.AutoScroll = False '<--- Think this was the reason for the fade.
.Panel.BackgroundImage = System.Drawing.Image.FromFile("C:\Windows\Web\Wallpaper\Windows\img0.jpg")
.Panel.Controls.Add(.Rtb)
With .Rtb
.Dock = DockStyle.Fill
.SelectionBackColor = Color.Transparent
.Focus()
End With
End With
For I As Integer = 0 To 100
Me.Rtb.AppendText($"{I.ToString}{vbCrLf}")
Next
End Sub
End Class
Friend Class TransparentRichTextBox
Inherits System.Windows.Forms.RichTextBox
<DllImport("kernel32.dll", CharSet:=CharSet.Auto)>
Private Shared Function LoadLibrary(ByVal lpFileName As String) As IntPtr
End Function
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim params As CreateParams = MyBase.CreateParams
If LoadLibrary("msftedit.dll") <> IntPtr.Zero Then
params.ExStyle = params.ExStyle Or &H20
params.ClassName = "RICHEDIT50W"
End If
Return params
End Get
End Property
End Class
Utterly useless, but always willing to help
As a finishing touch god created the dutch
-
Sep 4th, 2019, 09:25 AM
#6
Addicted Member
Re: Double Buffered Panel & Transparent RichTextBox / Link VScrollBar to RichTextBox?
Last edited by Goggy; Sep 4th, 2019 at 09:28 AM.
Utterly useless, but always willing to help
As a finishing touch god created the dutch
-
Sep 4th, 2019, 09:49 AM
#7
Thread Starter
New Member
Re: Double Buffered Panel & Transparent RichTextBox / Link VScrollBar to RichTextBox?
Originally Posted by Goggy
Hello,
Sorry to interrupt, but could you try this?
Code:
Option Strict On
Option Explicit On
Imports System.Runtime.InteropServices
Public Class Form1
Private Panel As New System.Windows.Forms.Panel
Private Rtb As New TransparentRichTextBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
With Me
.Controls.Add(Panel)
.Panel.Dock = DockStyle.Fill
.AutoScroll = False '<--- Think this was the reason for the fade.
.Panel.BackgroundImage = System.Drawing.Image.FromFile("C:\Windows\Web\Wallpaper\Windows\img0.jpg")
.Panel.Controls.Add(.Rtb)
With .Rtb
.Dock = DockStyle.Fill
.SelectionBackColor = Color.Transparent
.Focus()
End With
End With
For I As Integer = 0 To 100
Me.Rtb.AppendText($"{I.ToString}{vbCrLf}")
Next
End Sub
End Class
Friend Class TransparentRichTextBox
Inherits System.Windows.Forms.RichTextBox
<DllImport("kernel32.dll", CharSet:=CharSet.Auto)>
Private Shared Function LoadLibrary(ByVal lpFileName As String) As IntPtr
End Function
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim params As CreateParams = MyBase.CreateParams
If LoadLibrary("msftedit.dll") <> IntPtr.Zero Then
params.ExStyle = params.ExStyle Or &H20
params.ClassName = "RICHEDIT50W"
End If
Return params
End Get
End Property
End Class
Just gave it a try, it works until I add a double buffered panel to get rid of the flicker and then it's back to square one with vanishing scrollbars.
I suppose the short version of my problem is I need a double buffered Panel that contains a transparent RichTextBox but for reasons beyond my understanding a double buffered Panel will break the scrollbars of a RichTextBox. On my main project the panel is made visible or not by the user but to rule out as much as I could from my main project I have been working with this small example.
I've attached some pictures of the issue presented by initial example code:
Start.png shows what happens when running program but not having the mouse near the window.
MouseonscrollBar.png shows what happens when the mouse is moved over the area where the scrollbars should be.
MouseScroll.png shows what happens when using the mouse to scroll (missing arrows).
Edit: I also tried replacing Panel with PictureBox and GroupBox (could not really use due to unremovable border) and the issue was the same.
Thanks!
Last edited by JackBrig; Sep 4th, 2019 at 09:59 AM.
-
Sep 4th, 2019, 09:58 AM
#8
Re: Double Buffered Panel & Transparent RichTextBox / Link VScrollBar to RichTextBox?
Is this running on a Windows 10 system?
-
Sep 4th, 2019, 10:01 AM
#9
Thread Starter
New Member
Re: Double Buffered Panel & Transparent RichTextBox / Link VScrollBar to RichTextBox?
Originally Posted by jdc2000
Is this running on a Windows 10 system?
Tested on 7 to 10, same results - I have also tried on different hardware and even inside a VM. The old project I'm trying to modify was done in VB.NET 2010 but I have tried my example in 2010 and 2019 also with the same results.
Thanks!
Last edited by JackBrig; Sep 4th, 2019 at 10:04 AM.
-
Sep 4th, 2019, 10:13 AM
#10
Re: Double Buffered Panel & Transparent RichTextBox / Link VScrollBar to RichTextBox?
OK, I asked because the scrollbar behavior you are seeing seems to be the current Windows 10 default behavior, and the behavior of IE11 on Windows 7. You could try setting the theme to Classic on Windows 7 to see if it still happens.
-
Sep 4th, 2019, 10:22 AM
#11
Thread Starter
New Member
Re: Double Buffered Panel & Transparent RichTextBox / Link VScrollBar to RichTextBox?
Originally Posted by jdc2000
OK, I asked because the scrollbar behavior you are seeing seems to be the current Windows 10 default behavior, and the behavior of IE11 on Windows 7. You could try setting the theme to Classic on Windows 7 to see if it still happens.
I can't see the behaviour anywhere else but in my example, it's actually even worse under Windows 7 Classic mode with the scrollbar not appearing on mouse over and the arrows don't show until you click where they should be:
The scrollbar problem goes away if the panel is not double buffered but then I get insane flicker.
Thanks!
Last edited by JackBrig; Sep 4th, 2019 at 10:28 AM.
-
Sep 4th, 2019, 11:08 AM
#12
Addicted Member
Re: Double Buffered Panel & Transparent RichTextBox / Link VScrollBar to RichTextBox?
What would happen if you made the richtextbox double buffered. Can't test it on my phone ;-)
Utterly useless, but always willing to help
As a finishing touch god created the dutch
-
Sep 4th, 2019, 11:29 AM
#13
Thread Starter
New Member
Re: Double Buffered Panel & Transparent RichTextBox / Link VScrollBar to RichTextBox?
Originally Posted by Goggy
What would happen if you made the richtextbox double buffered. Can't test it on my phone ;-)
Scrollbars show but you get crazy flickering on scrolling
Thanks!
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
|