none
draw rectangle and resize form RRS feed

  • Question

  • I have several panels in a form

    I changed the border style of each panel with addhandler to the following event

      Private Sub PanelPaint(sender As Object, e As PaintEventArgs)
            Dim r As New Rectangle(New Point(0, 0), New Size(sender.Width, sender.Height))
            Dim c = New Color
            c = PannelloBorderColor
            Dim s As Integer
            s = PanelloBorderSize
            Dim p As New Pen(c, s)
            e.Graphics.DrawRectangle(p, r)
           
        End Sub

    Now if I anchor a Panel to right nd the user resize the form in the right I get a lot od vertical line on the right.  ( see the image)

    How can avoid this effect. ?

    Tuesday, December 12, 2017 3:44 PM

Answers

  •  Since a Panel does not have the ResizeRedraw property accessible unless you are creating your own panel class,  you will need to capture the Panel Resize event and call the Refresh method or it's Invalidate method.  The Invalidate method would probably be better because,  you can specify for it to paint itself but, not all of the child controls inside of it.

     Also,  i highly recommend turning on Option Strict.  The 'sender' is an Object type and you should cast it to a Panel type.

     You will also need to take the size (width) of the pen size int account when using the DrawRectangle method or you will find that the borders are not right when the size gets bigger than 1 or 2.

     I only used one Panel in the example,  but it shows pretty close to what you need to do.

    Option Strict On
    Option Explicit On
    Option Infer Off

    Public Class Form1 Private PannelloBorderColor As Color = Color.Red Private PanelloBorderSize As Single = 2 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load AddHandler Panel1.Paint, AddressOf Panel_Paint AddHandler Panel1.Resize, AddressOf Panel_Resize End Sub Private Sub Panel_Paint(sender As Object, e As PaintEventArgs) Dim pnl As Panel = CType(sender, Panel) Using p As New Pen(PannelloBorderColor, PanelloBorderSize) e.Graphics.DrawRectangle(p, 0, 0, pnl.Width, pnl.Height) End Using End Sub Private Sub Panel_Resize(sender As Object, e As EventArgs) Dim pnl As Panel = CType(sender, Panel) pnl.Invalidate(False) End Sub End Class


     

     


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Tuesday, December 12, 2017 5:32 PM
    • Marked as answer by Claudio111 Tuesday, December 12, 2017 5:49 PM
    Tuesday, December 12, 2017 5:28 PM

All replies

  •  Since a Panel does not have the ResizeRedraw property accessible unless you are creating your own panel class,  you will need to capture the Panel Resize event and call the Refresh method or it's Invalidate method.  The Invalidate method would probably be better because,  you can specify for it to paint itself but, not all of the child controls inside of it.

     Also,  i highly recommend turning on Option Strict.  The 'sender' is an Object type and you should cast it to a Panel type.

     You will also need to take the size (width) of the pen size int account when using the DrawRectangle method or you will find that the borders are not right when the size gets bigger than 1 or 2.

     I only used one Panel in the example,  but it shows pretty close to what you need to do.

    Option Strict On
    Option Explicit On
    Option Infer Off

    Public Class Form1 Private PannelloBorderColor As Color = Color.Red Private PanelloBorderSize As Single = 2 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load AddHandler Panel1.Paint, AddressOf Panel_Paint AddHandler Panel1.Resize, AddressOf Panel_Resize End Sub Private Sub Panel_Paint(sender As Object, e As PaintEventArgs) Dim pnl As Panel = CType(sender, Panel) Using p As New Pen(PannelloBorderColor, PanelloBorderSize) e.Graphics.DrawRectangle(p, 0, 0, pnl.Width, pnl.Height) End Using End Sub Private Sub Panel_Resize(sender As Object, e As EventArgs) Dim pnl As Panel = CType(sender, Panel) pnl.Invalidate(False) End Sub End Class


     

     


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Tuesday, December 12, 2017 5:32 PM
    • Marked as answer by Claudio111 Tuesday, December 12, 2017 5:49 PM
    Tuesday, December 12, 2017 5:28 PM
  •  Since a Panel does not have the ResizeRedraw property accessible unless you are creating your own panel class,  you will need to capture the Panel Resize event and call the Refresh method or it's Invalidate method.  The Invalidate method would probably be better because,  you can specify for it to paint itself but, not all of the child controls inside of it.

     Also,  i highly recommend turning on Option Strict.  The 'sender' is an Object type and you should cast it to a Panel type.

     You will also need to take the size (width) of the pen size int account when using the DrawRectangle method or you will find that the borders are not right when the size gets bigger than 1 or 2.

     I only used one Panel in the example,  but it shows pretty close to what you need to do.

    Option Strict On
    Option Explicit On
    Option Infer Off

    Public Class Form1 Private PannelloBorderColor As Color = Color.Red Private PanelloBorderSize As Single = 2 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load AddHandler Panel1.Paint, AddressOf Panel_Paint AddHandler Panel1.Resize, AddressOf Panel_Resize End Sub Private Sub Panel_Paint(sender As Object, e As PaintEventArgs) Dim pnl As Panel = CType(sender, Panel) Using p As New Pen(PannelloBorderColor, PanelloBorderSize) e.Graphics.DrawRectangle(p, 0, 0, pnl.Width, pnl.Height) End Using End Sub Private Sub Panel_Resize(sender As Object, e As EventArgs) Dim pnl As Panel = CType(sender, Panel) pnl.Invalidate(False) End Sub End Class


     

     


    If you say it can`t be done then i`ll try it


    Tuesday, December 12, 2017 5:50 PM
  • Thank Iron

    it works perfectly

    I dont use the Option Script ON sinthe object is already a Panel :-)

    I I do it a get other errors from other part of the project

    Thank aging

    Bye

    Claudio

    Tuesday, December 12, 2017 5:51 PM
  • Thank Iron

    it works perfectly

    I dont use the Option Script ON sinthe object is already a Panel :-)

    I I do it a get other errors from other part of the project

    Thank aging

    Bye

    Claudio


     You are missing the point about casting the Object to a Panel but,  do it how you want.  With Option Strict Off,  you are letting the compiler decide what the sender Object should be instead of telling it what it should be.

    If you say it can`t be done then i`ll try it

    Tuesday, December 12, 2017 5:59 PM