none
border style of groupbox RRS feed

  • Question

  • How can I make the borderstyle of a Groupbox in vb Express like the cellborderstyle of a TablelayoutPanel. e.g. cellborderstyle(inset,inset double, outset)?
    Saturday, July 11, 2009 1:05 PM

Answers

  •  

    Private Sub GroupBox1_Paint(ByVal Sender As Object,ByVal e As PaintEventArgs)Handles GroupBox1.Paint

    Dim Rect As Rectangle = GroupBox1.ClientRectangle()

    ControlPaint.DrawBorder3D(e.Graphics,Rect,Border3DStyle.Raised)

    End Sub

     


    Asgar
    • Proposed as answer by CubePS Saturday, July 11, 2009 1:43 PM
    • Marked as answer by Yichun_Feng Friday, July 17, 2009 12:42 AM
    Saturday, July 11, 2009 1:38 PM
  • Most of the controls, if they offer a border style property, only offer none, flat, and 3D as choices. You don't get all the fancy ones like sunken, raised, double, etc.

    You could probably subclass the GroupBox and implement your own drawing routine but it's pretty easy to fake it with a Label and Panel so it's probably not worth it. (Unless you just want the practice.)

    And Asghar did have good code for drawing the border piece.

    (Barely related aside: WPF can add bitmap effects to just about anything so it can, for example, add a beveled edge, drop shadow, or glow to an image, label, or whatever. A little like ControlPaint.DrawBorder3D with more flexibility.)

    Rod

    Rod Stephens, Visual Basic MVP

    Beginning Database Design Solutions
    http://www.amazon.com/exec/obidos/ASIN/0470385499/vbhelper/

    Visual Basic 2008 Programmer's Reference
    http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
    • Marked as answer by Yichun_Feng Friday, July 17, 2009 12:41 AM
    Sunday, July 12, 2009 12:00 AM

All replies

  • use overloaded ControlPaint.DrawBorder3D() method
    Asgar
    • Edited by _asgar Saturday, July 11, 2009 1:10 PM addl
    Saturday, July 11, 2009 1:10 PM
  • Hi Asghar
    Thanks for your reply.
    Here is my code for Groupbox:

    Dim

     

    newgroup As New GroupBox

    newgroup.Left = 20

    newgroup.Top = 20

    newgroup.Width = 40

    newgroup.Height = 40

    Controls.Add(newgroup)

    and how i have to add the code line you suggested?

    ControlPaint.DrawBorder3D()

     

     

    Saturday, July 11, 2009 1:24 PM
  •  

    Private Sub GroupBox1_Paint(ByVal Sender As Object,ByVal e As PaintEventArgs)Handles GroupBox1.Paint

    Dim Rect As Rectangle = GroupBox1.ClientRectangle()

    ControlPaint.DrawBorder3D(e.Graphics,Rect,Border3DStyle.Raised)

    End Sub

     


    Asgar
    • Proposed as answer by CubePS Saturday, July 11, 2009 1:43 PM
    • Marked as answer by Yichun_Feng Friday, July 17, 2009 12:42 AM
    Saturday, July 11, 2009 1:38 PM
  • That doesn't quite work. It draws a nice box around the GroupBox but it's around the outside of the control rather than where the control normally draws its border. So you still see the top line and the new one doesn't line up with the caption.

    You might want to build it from scratch. You can use a Panel or PictureBox to hold the controls that would be inside the GroupBox. Use code similar to _asgar's to draw the border:

        Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
            Dim Rect As Rectangle = sender.ClientRectangle()
            ControlPaint.DrawBorder3D(e.Graphics, Rect, Border3DStyle.RaisedInner)
        End Sub

    Now place a Label overlapping the upper left corner to display the caption. The Label's background will cover that part of the border to make it look like a GroupBox.

    Rod

    Rod Stephens, Visual Basic MVP

    Beginning Database Design Solutions
    http://www.amazon.com/exec/obidos/ASIN/0470385499/vbhelper/

    Visual Basic 2008 Programmer's Reference
    http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
    Saturday, July 11, 2009 2:24 PM
  • Hi Stephens
    Yes you are right . the code Asghar provided draws a rectangle outside the Groupbox.
    So there are no borderstyles for Groupbox as we can use in TablelayoutPanel. e.g. cellborderstyle(inset,inset double, outset)?
    Saturday, July 11, 2009 7:01 PM
  • Most of the controls, if they offer a border style property, only offer none, flat, and 3D as choices. You don't get all the fancy ones like sunken, raised, double, etc.

    You could probably subclass the GroupBox and implement your own drawing routine but it's pretty easy to fake it with a Label and Panel so it's probably not worth it. (Unless you just want the practice.)

    And Asghar did have good code for drawing the border piece.

    (Barely related aside: WPF can add bitmap effects to just about anything so it can, for example, add a beveled edge, drop shadow, or glow to an image, label, or whatever. A little like ControlPaint.DrawBorder3D with more flexibility.)

    Rod

    Rod Stephens, Visual Basic MVP

    Beginning Database Design Solutions
    http://www.amazon.com/exec/obidos/ASIN/0470385499/vbhelper/

    Visual Basic 2008 Programmer's Reference
    http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
    • Marked as answer by Yichun_Feng Friday, July 17, 2009 12:41 AM
    Sunday, July 12, 2009 12:00 AM
  • If you want you can have my code for a custom groupbox



    Imports System.Drawing.Drawing2D
    
    Public Class Form1
    
    End Class
    
    Public Class BDGroupBox : Inherits GroupBox
    
        Private _GbrushColor1 As Color = Color.Blue
        Public Property GbrushColor1() As Color
            Get
                Return _GbrushColor1
            End Get
            Set(ByVal value As Color)
                _GbrushColor1 = value
                Me.Invalidate()
            End Set
        End Property
    
        Private _GbrushColor2 As Color = Color.Cyan
        Public Property GbrushColor2() As Color
            Get
                Return _GbrushColor2
            End Get
            Set(ByVal value As Color)
                _GbrushColor2 = value
                Me.Invalidate()
            End Set
        End Property
    
        Private _BorderColor As Color = Color.Red
        Public Property BorderColor() As Color
            Get
                Return _BorderColor
            End Get
            Set(ByVal value As Color)
                _BorderColor = value
                Me.Invalidate()
            End Set
    
        End Property
    
        Private _BorderMode As LinearGradientMode = LinearGradientMode.Vertical
        Public Property BorderMode() As LinearGradientMode
            Get
                Return _BorderMode
            End Get
            Set(ByVal value As LinearGradientMode)
                _BorderMode = value
                Me.Invalidate()
            End Set
        End Property
    
        Private _InnerColor1 As Color = Color.White
        Public Property InnerColor1() As Color
            Get
                Return _InnerColor1
            End Get
            Set(ByVal value As Color)
                _InnerColor1 = value
                Me.Invalidate()
            End Set
        End Property
    
        Private _InnerColor2 As Color = Color.Black
        Public Property InnerColor2() As Color
            Get
                Return _InnerColor2
            End Get
            Set(ByVal value As Color)
                _InnerColor2 = value
                Me.Invalidate()
            End Set
        End Property
    
        Private _InnerMode As LinearGradientMode = LinearGradientMode.Vertical
        Public Property InnerBorderMode() As LinearGradientMode
            Get
                Return _InnerMode
            End Get
            Set(ByVal value As LinearGradientMode)
                _InnerMode = value
                Me.Invalidate()
            End Set
        End Property
    
        Private _InnerBorder As Color = Color.Black
        Public Property InnerBorder() As Color
            Get
                Return _InnerBorder
            End Get
            Set(ByVal value As Color)
                _InnerBorder = value
                Me.Invalidate()
            End Set
        End Property
    
        Private _InnerBorderWidth As Integer = 2
        Public Property InnerBorderWidth() As Integer
            Get
                Return _InnerBorderWidth
            End Get
            Set(ByVal value As Integer)
                _InnerBorderWidth = value
                Me.Invalidate()
            End Set
        End Property
    
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    
            Dim NewPath As GraphicsPath = CreatePath(Me.ClientRectangle)
    
            Me.Region = New Region(NewPath)
    
            Dim brsh As New LinearGradientBrush(Me.ClientRectangle, _GbrushColor1, _GbrushColor2, _BorderMode)
            e.Graphics.FillPath(brsh, NewPath)
    
            Dim Border As New Pen(Color.Transparent, 2)
            NewPath.Widen(Border)
            e.Graphics.FillPath(brsh, NewPath)
            e.Graphics.DrawPath(New Pen(_BorderColor, 2), NewPath)
    
            Dim R As Rectangle = Me.ClientRectangle
    
            Dim InnerBrush As New LinearGradientBrush(R, _InnerColor1, _InnerColor2, _InnerMode)
    
            R.Inflate(-8, -20)
    
            Dim M As New Matrix
            M.Translate(8, 30)
    
            Dim Innerpath As GraphicsPath = CreatePath(R, M)
            e.Graphics.FillPath(InnerBrush, Innerpath)
            e.Graphics.DrawPath(New Pen(_InnerBorder, _InnerBorderWidth), Innerpath)
    
            Dim SFormat As New StringFormat
            SFormat.Alignment = StringAlignment.Near
            SFormat.LineAlignment = StringAlignment.Center
            SFormat.FormatFlags = StringFormatFlags.NoWrap
            e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), New Rectangle(10, 10, Me.Width, 16), SFormat)
    
        End Sub
    
        Private Function CreatePath(ByVal GRectangle As Rectangle, Optional ByVal M As Matrix = Nothing) As GraphicsPath
    
            Dim NewPath As New GraphicsPath
    
            NewPath.AddArc(New Rectangle(2, 2, 20, 20), 180, 90)
    
            NewPath.AddLine(New Point(22, 2), New Point(GRectangle.Width - 24, 2))
    
            NewPath.AddArc(New Rectangle(GRectangle.Width - 24, 2, 20, 20), 270, 90)
    
            NewPath.AddLine(New Point(GRectangle.Width - 4, 22), New Point(GRectangle.Width - 4, GRectangle.Height - 22))
    
            NewPath.AddArc(New Rectangle(GRectangle.Width - 24, GRectangle.Height - 22, 20, 20), 0, 90)
    
            NewPath.AddLine(New Point(GRectangle.Width - 24, GRectangle.Height - 2), New Point(24, GRectangle.Height - 2))
    
            NewPath.AddArc(New Rectangle(2, GRectangle.Height - 22, 20, 20), 90, 90)
    
            NewPath.CloseFigure()
    
            If M IsNot Nothing Then
                NewPath.Transform(M)
            End If
    
            Return NewPath
    
        End Function
    
    End Class
    

    Coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Sunday, July 12, 2009 1:09 AM
  • You can turn off VisualStyles for just the groupbox via interop.

    make this call at Form_Load (assuming the GroupBox is named GroupBox1):

        SetWindowTheme(GroupBox1.Handle, "", "")

    ...where SetWindowTheme has been defined as follows:

        <DllImport("uxtheme.dll", CharSet:=CharSet.Unicode)> _
        Private Shared Function SetWindowTheme(ByVal hWnd As IntPtr, ByVal sz1 As String, ByVal sz2 As String) As Int32
        End Function
    Note that the GroupBox's FlatStyle property must be set to System as VisualStyles has not been correctly implemented and will be painted by the control whether it is wanted or not with other FlatStyle settings.

    Mick Doherty
    http://dotnetrix.co.uk
    Sunday, July 12, 2009 2:08 PM