none
Panel border color RRS feed

Answers

  • How can I change the Panel border color  ?

    Claudio

    Hi

    Here is one way

    ' Form1 with Panel1
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
            Dim r As New Rectangle(New Point(0, 0), New Size(Panel1.Width - 3, Panel1.Height - 3))
            Dim p As New Pen(Brushes.Red, 4)
    
            e.Graphics.DrawRectangle(p, r)
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Proposed as answer by Lucy Qu Tuesday, December 12, 2017 2:53 PM
    • Marked as answer by Claudio111 Tuesday, December 12, 2017 2:56 PM
    Tuesday, December 12, 2017 12:57 PM
  • what rise the event ?

    Hi

    Try it. Start a new project and put Panel1 onto form and in the Panel1 Paint event handler put the code I posted.

    The event is raised by the operating system anytime the Panel needs to be redrawn - such as changing the window size, moving the window and many other things.

    If you want to force a redraw, you can use Panel1.Invalidate


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, December 12, 2017 1:39 PM
    • Marked as answer by Claudio111 Tuesday, December 12, 2017 2:56 PM
    Tuesday, December 12, 2017 1:38 PM

All replies

  • How can I change the Panel border color  ?

    Claudio

    Hi

    Here is one way

    ' Form1 with Panel1
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
            Dim r As New Rectangle(New Point(0, 0), New Size(Panel1.Width - 3, Panel1.Height - 3))
            Dim p As New Pen(Brushes.Red, 4)
    
            e.Graphics.DrawRectangle(p, r)
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Proposed as answer by Lucy Qu Tuesday, December 12, 2017 2:53 PM
    • Marked as answer by Claudio111 Tuesday, December 12, 2017 2:56 PM
    Tuesday, December 12, 2017 12:57 PM
  • what rise the event ?
    Tuesday, December 12, 2017 1:04 PM
  • what rise the event ?

    Hi

    Try it. Start a new project and put Panel1 onto form and in the Panel1 Paint event handler put the code I posted.

    The event is raised by the operating system anytime the Panel needs to be redrawn - such as changing the window size, moving the window and many other things.

    If you want to force a redraw, you can use Panel1.Invalidate


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, December 12, 2017 1:39 PM
    • Marked as answer by Claudio111 Tuesday, December 12, 2017 2:56 PM
    Tuesday, December 12, 2017 1:38 PM
  • sorry

    i have a lot of panels and i do not want to use the panel name in your sub

    So I solved the problem with AddHandler panel.Paint, AddressOf Panel_Paint

    I copy all the code for other people could hsve the same problem

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            SetPanelBorderColor(Me)
        End Sub
        ''' <summary>
        ''' Get the list of all panels and add the PanelinForm.Pant event to Sub Panel_Paint
        ''' </summary>
        ''' <param name="frm"></param>
        Public Sub SetPanelBorderColor(ByRef frm As Form)
            frm.AllPanel.ToList.ForEach(
                Sub(panelinform)    ' Gets list of all panels
                    AddHandler panelinform.Paint, AddressOf Panel_Paint
                End Sub)
        End Sub
        ''' <summary>
        ''' Panel_Paint event ( set size and color you want in the code)
        ''' </summary>
        ''' <param name="sender">
        ''' one panel at a time
        ''' </param>
        ''' <param name="e"></param>
        Private Sub Panel_Paint(sender As Object, e As PaintEventArgs)
            Dim r As New Rectangle(New Point(0, 0), New Size(sender.Width - 3, sender.Height - 3))
            Dim p As New Pen(Brushes.Red, 4)
            e.Graphics.DrawRectangle(p, r)
        End Sub
    
    End Class

    you also need this module that can also get list of all textbox,panel,checkbox etc etc

    Public Module LanguageExtensions
        <Runtime.CompilerServices.Extension()>
        Public Function GetAll(ByVal control As Control, ByVal type As Type) As IEnumerable(Of Control)
            Dim controls = control.Controls.Cast(Of Control)()
            Return controls.SelectMany(Function(ctrl) GetAll(ctrl, type)) _
                    .Concat(controls).Where(Function(c) c.GetType() Is type)
        End Function
        <Runtime.CompilerServices.Extension()>
        Public Function AllTextBoxes(ByVal pSender As Control) As IEnumerable(Of TextBox)
            Return GetAll(pSender, GetType(TextBox)).OfType(Of TextBox)
        End Function
        <Runtime.CompilerServices.Extension()>
        Public Function AllCheckBoxes(ByVal pSender As Control) As IEnumerable(Of CheckBox)
            Return GetAll(pSender, GetType(CheckBox)).OfType(Of CheckBox)
        End Function
        <Runtime.CompilerServices.Extension()>
        Public Function AllPanel(ByVal pSender As Control) As IEnumerable(Of Panel)
            Return GetAll(pSender, GetType(Panel)).OfType(Of Panel)
        End Function
        <Runtime.CompilerServices.Extension()>
        Public Function AllMenuStrip(ByVal pSender As Control) As IEnumerable(Of MenuStrip)
            Return GetAll(pSender, GetType(MenuStrip)).OfType(Of MenuStrip)
        End Function
    End Module
    

    I hope this will help

    Cludio

    Tuesday, December 12, 2017 2:16 PM
  • sorry

    i have a lot of panels and i do not want to use the panel name in your sub

    So I solved the problem with AddHandler panel.Paint, AddressOf Panel_Paint

    I copy all the code for other people could hsve the same problem

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            SetPanelBorderColor(Me)
        End Sub
        ''' <summary>
        ''' Get the list of all panels and add the PanelinForm.Pant event to Sub Panel_Paint
        ''' </summary>
        ''' <param name="frm"></param>
        Public Sub SetPanelBorderColor(ByRef frm As Form)
            frm.AllPanel.ToList.ForEach(
                Sub(panelinform)    ' Gets list of all panels
                    AddHandler panelinform.Paint, AddressOf Panel_Paint
                End Sub)
        End Sub
        ''' <summary>
        ''' Panel_Paint event ( set size and color you want in the code)
        ''' </summary>
        ''' <param name="sender">
        ''' one panel at a time
        ''' </param>
        ''' <param name="e"></param>
        Private Sub Panel_Paint(sender As Object, e As PaintEventArgs)
            Dim r As New Rectangle(New Point(0, 0), New Size(sender.Width - 3, sender.Height - 3))
            Dim p As New Pen(Brushes.Red, 4)
            e.Graphics.DrawRectangle(p, r)
        End Sub
    
    End Class

    you also need this module that can also get list of all textbox,panel,checkbox etc etc

    Public Module LanguageExtensions
        <Runtime.CompilerServices.Extension()>
        Public Function GetAll(ByVal control As Control, ByVal type As Type) As IEnumerable(Of Control)
            Dim controls = control.Controls.Cast(Of Control)()
            Return controls.SelectMany(Function(ctrl) GetAll(ctrl, type)) _
                    .Concat(controls).Where(Function(c) c.GetType() Is type)
        End Function
        <Runtime.CompilerServices.Extension()>
        Public Function AllTextBoxes(ByVal pSender As Control) As IEnumerable(Of TextBox)
            Return GetAll(pSender, GetType(TextBox)).OfType(Of TextBox)
        End Function
        <Runtime.CompilerServices.Extension()>
        Public Function AllCheckBoxes(ByVal pSender As Control) As IEnumerable(Of CheckBox)
            Return GetAll(pSender, GetType(CheckBox)).OfType(Of CheckBox)
        End Function
        <Runtime.CompilerServices.Extension()>
        Public Function AllPanel(ByVal pSender As Control) As IEnumerable(Of Panel)
            Return GetAll(pSender, GetType(Panel)).OfType(Of Panel)
        End Function
        <Runtime.CompilerServices.Extension()>
        Public Function AllMenuStrip(ByVal pSender As Control) As IEnumerable(Of MenuStrip)
            Return GetAll(pSender, GetType(MenuStrip)).OfType(Of MenuStrip)
        End Function
    End Module

    I hope this will help

    Cludio

    Hi

    First of all, you asked 'How can I change the Panel border color  ?' which is singular.

    The code I provided did answer that question. If you want to add to it, fine, but don't try to infer that my answer wasn't suitable for you due to factors not even mentioned in the original question.

    You just threw in a small one line question and then go on to say later on 'i have a lot of panels' - do you expect others to  be mind readers?


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, December 12, 2017 2:48 PM
    Tuesday, December 12, 2017 2:47 PM
  • @Ieshay - That was called for.

    @Claudio111 - He gave you the answer to your question. When others are searching for an answer to this question, they'll stumble across your title and click on it. Marking Ieshay's answer 'As Answer' will help others find their answer.

    Also, "I have a lot of panels and i do not want to use the panel name in your sub" - you can change the panel name to whatever you like...
    • Edited by Lucy Qu Tuesday, December 12, 2017 2:57 PM
    Tuesday, December 12, 2017 2:55 PM
  • Dont worry, your first replay has been very helpfull form me.

    Than i fit it in my code and i send all the code for other people having the same problem.

    But now i got one problem

    I  anchor the panel to the right and if the user resize the form....i get a lot of rectangles

    How can I clear the old rectangle ?

    Tuesday, December 12, 2017 3:00 PM