Results 1 to 13 of 13

Thread: Double Buffered Panel & Transparent RichTextBox / Link VScrollBar to RichTextBox?

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    7

    Question 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:
    1. Public Class DoubleBufferedPanel
    2.         Inherits Panel
    3.         Public Sub New()
    4.             Me.DoubleBuffered = True
    5.         End Sub
    6.     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:
    1. Imports System.Runtime.InteropServices
    2.  
    3.     Public Class TransparentRichTextBox
    4.         Inherits RichTextBox
    5.  
    6.         <DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _
    7.         Private Shared Function LoadLibrary(ByVal lpFileName As String) As IntPtr
    8.         End Function
    9.  
    10.         Protected Overrides ReadOnly Property CreateParams() As CreateParams
    11.             Get
    12.                 Dim params As CreateParams = MyBase.CreateParams
    13.                 If LoadLibrary("msftedit.dll") <> IntPtr.Zero Then
    14.                     params.ExStyle = params.ExStyle Or &H20
    15.                     params.ClassName = "RICHEDIT50W"
    16.                 End If
    17.                 Return params
    18.             End Get
    19.         End Property
    20.  
    21.     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:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Imports System.Runtime.InteropServices
    5.  
    6. Public Class Form1
    7.  
    8.     Public Class DoubleBufferedPanel
    9.         Inherits Panel
    10.         Public Sub New()
    11.             Me.DoubleBuffered = True
    12.         End Sub
    13.     End Class
    14.  
    15.     Public Class TransparentRichTextBox
    16.         Inherits RichTextBox
    17.  
    18.         <DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _
    19.         Private Shared Function LoadLibrary(ByVal lpFileName As String) As IntPtr
    20.         End Function
    21.  
    22.         Protected Overrides ReadOnly Property CreateParams() As CreateParams
    23.             Get
    24.                 Dim params As CreateParams = MyBase.CreateParams
    25.                 If LoadLibrary("msftedit.dll") <> IntPtr.Zero Then
    26.                     params.ExStyle = params.ExStyle Or &H20
    27.                     params.ClassName = "RICHEDIT50W"
    28.                 End If
    29.                 Return params
    30.             End Get
    31.         End Property
    32.  
    33.     End Class
    34.  
    35.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    36.  
    37.         ' Form stuff
    38.         Me.DoubleBuffered = True
    39.         Me.Size = New Size(675, 535)
    40.         Me.BackColor = Color.LightGreen
    41.  
    42.         ' Create DoubleBufferedPanel
    43.         Dim DoubleBufferedPanel1 As New DoubleBufferedPanel
    44.  
    45.         With DoubleBufferedPanel1
    46.             .Name = "DoubleBufferedPanel1"
    47.             .BackColor = Color.LightBlue
    48.             .Width = 640
    49.             .Height = 480
    50.             .Top = 10
    51.             .Left = 10
    52.             .BackgroundImage = System.Drawing.Image.FromFile("C:\Windows\Web\Wallpaper\Windows\img0.jpg")
    53.             .BackgroundImageLayout = ImageLayout.Stretch
    54.         End With
    55.  
    56.         Me.Controls.Add(DoubleBufferedPanel1)
    57.  
    58.         ' Create TransparentRichTextBox, fill with text & return to top
    59.         Dim TransparentRichTextBox1 As New TransparentRichTextBox
    60.  
    61.         With TransparentRichTextBox1
    62.             .Name = "TransparentRichTextBox1"
    63.             .Width = 620
    64.             .Height = 460
    65.             .Top = 10
    66.             .Left = 10
    67.             .ReadOnly = True
    68.             .WordWrap = False
    69.             .ScrollBars = RichTextBoxScrollBars.ForcedVertical
    70.         End With
    71.  
    72.         For Line As Integer = 1 To 1000
    73.             TransparentRichTextBox1.AppendText(String.Format("{0} {1}{2}", "Test Text", Line, vbCrLf))
    74.         Next
    75.  
    76.         TransparentRichTextBox1.Select(0, 0)
    77.  
    78.         ' Add TransparentRichTextBox1 to DoubleBufferedPanel1
    79.         DoubleBufferedPanel1.Controls.Add(TransparentRichTextBox1)
    80.  
    81.     End Sub
    82. 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.

  2. #2

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    7

    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.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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...

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    7

    Re: Double Buffered Panel & Transparent RichTextBox / Link VScrollBar to RichTextBox?

    Quote Originally Posted by .paul. View Post
    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:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Imports System.Runtime.InteropServices
    5.  
    6. Public Class Form1
    7.  
    8.     Public Class DoubleBufferedPanel
    9.         Inherits Panel
    10.         Public Sub New()
    11.             Me.DoubleBuffered = True
    12.         End Sub
    13.     End Class
    14.  
    15.     Public Class TransparentRichTextBox
    16.         Inherits RichTextBox
    17.  
    18.         <DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _
    19.         Private Shared Function LoadLibrary(ByVal lpFileName As String) As IntPtr
    20.         End Function
    21.  
    22.         Protected Overrides ReadOnly Property CreateParams() As CreateParams
    23.             Get
    24.                 Dim params As CreateParams = MyBase.CreateParams
    25.                 If LoadLibrary("msftedit.dll") <> IntPtr.Zero Then
    26.                     params.ExStyle = params.ExStyle Or &H20
    27.                     params.ClassName = "RICHEDIT50W"
    28.                 End If
    29.                 Return params
    30.             End Get
    31.         End Property
    32.  
    33.     End Class
    34.  
    35.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    36.  
    37.         ' Form stuff
    38.         Me.DoubleBuffered = True
    39.         Me.Size = New Size(675, 535)
    40.         Me.BackColor = Color.LightGreen
    41.  
    42.         ' Create DoubleBufferedPanel
    43.         Dim DoubleBufferedPanel1 As New DoubleBufferedPanel
    44.  
    45.         With DoubleBufferedPanel1
    46.             .Name = "DoubleBufferedPanel1"
    47.             .BorderStyle = BorderStyle.None
    48.             .BackColor = Color.LightBlue
    49.             .Width = 640
    50.             .Height = 480
    51.             .Top = 10
    52.             .Left = 10
    53.             .BackgroundImage = System.Drawing.Image.FromFile("C:\Windows\Web\Wallpaper\Windows\img0.jpg")
    54.             .BackgroundImageLayout = ImageLayout.Stretch
    55.         End With
    56.  
    57.         Me.Controls.Add(DoubleBufferedPanel1)
    58.  
    59.         Dim DoubleBufferedPanel2 As New DoubleBufferedPanel
    60.  
    61.         With DoubleBufferedPanel2
    62.             .Name = "DoubleBufferedPanel2"
    63.             .BorderStyle = BorderStyle.None
    64.             .AutoScroll = True
    65.             .BackColor = Color.Transparent
    66.             .Width = 620
    67.             .Height = 460
    68.             .Top = 10
    69.             .Left = 10
    70.         End With
    71.  
    72.         DoubleBufferedPanel1.Controls.Add(DoubleBufferedPanel2)
    73.  
    74.         ' Create TransparentRichTextBox, fill with text & return to top
    75.         Dim TransparentRichTextBox1 As New TransparentRichTextBox
    76.  
    77.         With TransparentRichTextBox1
    78.             .Name = "TransparentRichTextBox1"
    79.             .BorderStyle = BorderStyle.None
    80.             .Width = 603 '640
    81.             .Height = 480
    82.             .Top = 0 '10
    83.             .Left = 0 ' 10
    84.             .ReadOnly = True
    85.             .WordWrap = False
    86.             .ScrollBars = RichTextBoxScrollBars.None
    87.             .Height = 5000
    88.         End With
    89.  
    90.         For Line As Integer = 1 To 1000
    91.             TransparentRichTextBox1.AppendText(String.Format("{0} {1}{2}", "Test Text", Line, vbCrLf))
    92.         Next
    93.  
    94.         TransparentRichTextBox1.Select(0, 0)
    95.  
    96.         ' Add TransparentRichTextBox1 to DoubleBufferedPanel1
    97.         DoubleBufferedPanel2.Controls.Add(TransparentRichTextBox1)
    98.  
    99.     End Sub
    100. End Class

    Ideally I need to find out what's causing the scrollbar vanishing act in my first example.

    Thanks!

  5. #5
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    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

  6. #6
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: Double Buffered Panel & Transparent RichTextBox / Link VScrollBar to RichTextBox?

    Double post
    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

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    7

    Re: Double Buffered Panel & Transparent RichTextBox / Link VScrollBar to RichTextBox?

    Quote Originally Posted by Goggy View Post
    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.
    Name:  Start.jpg
Views: 648
Size:  26.0 KB

    MouseonscrollBar.png shows what happens when the mouse is moved over the area where the scrollbars should be.
    Name:  MouseonscrollBar.jpg
Views: 663
Size:  29.5 KB

    MouseScroll.png shows what happens when using the mouse to scroll (missing arrows).
    Name:  MouseScroll.jpg
Views: 810
Size:  28.8 KB

    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.

  8. #8
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

    Re: Double Buffered Panel & Transparent RichTextBox / Link VScrollBar to RichTextBox?

    Is this running on a Windows 10 system?

  9. #9

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    7

    Re: Double Buffered Panel & Transparent RichTextBox / Link VScrollBar to RichTextBox?

    Quote Originally Posted by jdc2000 View Post
    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.

  10. #10
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

    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.

  11. #11

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    7

    Re: Double Buffered Panel & Transparent RichTextBox / Link VScrollBar to RichTextBox?

    Quote Originally Posted by jdc2000 View Post
    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!
    Attached Images Attached Images     
    Last edited by JackBrig; Sep 4th, 2019 at 10:28 AM.

  12. #12
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    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

  13. #13

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    7

    Re: Double Buffered Panel & Transparent RichTextBox / Link VScrollBar to RichTextBox?

    Quote Originally Posted by Goggy View Post
    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
  •  



Click Here to Expand Forum to Full Width