dlaksmi
dlaksmi

Reputation: 369

How to make automatically use the Custom panel in Custom ComboBox into one in vb.net

I'm Trying to make automatically use the Custom panel PanelGlow in Custom ComboBox FLATCOMBOBOX into one in vb.net

Please Guide me

Thanks

Custom FLATCOMBOBOX

Imports System.ComponentModel
Imports System.Drawing.Drawing2D

Public Class FLATCOMBOBOX
    Inherits ComboBox
    ' Fields
    Private _disabledIndicatorColor As Color = Color.DarkGray
    Private _disabledColor As Color = Color.FromArgb(240, 240, 240)
    Private _disabledForeColor As Color = Color.FromArgb(&H6D, &H6D, &H6D)
    Private _disabledBorderColor As Color = Color.FromArgb(&HCC, &HCC, &HCC)
    Private _itemTopMargin As Integer = 1
    Private _textLeftMargin As Integer = 5
    Private _borderColor As Color = Color.Red
    Private _backColor As Color = Color.White
    Private _radius As Integer = 1
    Private _borderthickness As BorderThickness
    Private _indicatorColor As Color = Color.Red
    ' Methods
    Public Sub New()
        Me.InitializeComponent()
        MyBase.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        MyBase.SetStyle(ControlStyles.ContainerControl, True)
        MyBase.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        MyBase.SetStyle(ControlStyles.ResizeRedraw, True)
        MyBase.SetStyle(ControlStyles.Selectable, True)
        MyBase.SetStyle(ControlStyles.UserPaint, True)
        MyBase.SetStyle(ControlStyles.Selectable, True)
        MyBase.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        Me.FlatStyle = FlatStyle.Flat
        Me.DrawMode = DrawMode.Normal
        Me.DropDownStyle = ComboBoxStyle.DropDown
        Me.Width = 260
        Me.Height = &H20
        Me.ItemHeight = &H1A
        Me.DrawMode = DrawMode.OwnerDrawFixed
        Me.IndicatorColor = Color.Red
        Me.DropdownBorderThickness = BorderThickness.Thin
    End Sub
    Private Sub DrawIndicator(ByVal graphics As Graphics, ByVal lineColor As Color, ByVal indicatorColor As Color, ByVal foreColor As Color)
        Dim num As Integer
        Dim pen As Pen
        graphics.SmoothingMode = SmoothingMode.AntiAlias
        graphics.SmoothingMode = SmoothingMode.HighQuality
        graphics.InterpolationMode = InterpolationMode.High
        Dim brush As New SolidBrush(Me._borderColor)
        Dim brush2 As New SolidBrush(Me._backColor)
        If Not Me.Enabled Then
            indicatorColor = Me._disabledIndicatorColor
            brush.Color = Me._disabledBorderColor
            brush2.Color = Me._disabledColor
        End If
        If Me.DropdownBorderThickness = BorderThickness.Thick Then
            pen = New Pen(brush, 2.0F)
            num = 1
        Else
            pen = New Pen(brush, 1.0F)
            num = 0
        End If
        Using p = New Pen(Me.BorderColor, 2)
            graphics.DrawRectangle(p, MyBase.ClientRectangle.X, MyBase.ClientRectangle.Y, MyBase.ClientRectangle.Width - 1, MyBase.ClientRectangle.Height - 1)
        End Using
        brush.Dispose()
        brush2.Dispose()
        pen.Dispose()
    End Sub
    <Category("Tools"), Description("Sets dropdown's border color.")>
    Public Overridable Property BorderColor As Color
        Get
            Return Me._borderColor
        End Get
        Set(ByVal value As Color)
            Me._borderColor = value
            Me.Refresh()
        End Set
    End Property
    <DisplayName("BackColor"), Category("Tools"), Description("Sets the dropdown's background color.")>
    Public Overridable Property BackgroundColor As Color
        Get
            Return Me._backColor
        End Get
        Set(ByVal value As Color)
            Me._backColor = value
            Me.Refresh()
        End Set
    End Property
    <Category("Tools"), Description("Sets the dropdown's border thickness.")>
    Public Overridable Property DropdownBorderThickness As BorderThickness
        Get
            Return Me._borderthickness
        End Get
        Set(ByVal value As BorderThickness)
            Me._borderthickness = value
            Me.Refresh()
        End Set
    End Property
    <Category("Tools"), Description("Sets the indicator color.")>
    Public Overridable Property IndicatorColor As Color
        Get
            Return Me._indicatorColor
        End Get
        Set(ByVal value As Color)
            Me._indicatorColor = value
            Me.Refresh()
        End Set
    End Property
    <Browsable(False)>
    Public Overridable Property Color As Color
        Get
            Return Me._borderColor
        End Get
        Set(ByVal value As Color)
            Me._borderColor = value
            Me.Refresh()
        End Set
    End Property

    Protected Overrides Sub OnPaint(ByVal pe As PaintEventArgs)
        If Me.Enabled Then
            Me.DrawIndicator(pe.Graphics, Me.Color, Me.IndicatorColor, Me.ForeColor)
        Else
            Me.DrawIndicator(pe.Graphics, Me._disabledBorderColor, Me._disabledBorderColor, Me._disabledForeColor)
        End If
        MyBase.OnPaint(pe)
    End Sub
    Public Enum BorderThickness
        Thick
        Thin
    End Enum
End Class

Custom PanelGlow

#Region "Imports"
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.ComponentModel
Imports System.Windows.Forms
#End Region



''' <summary>
''' PanelGlow is a Panel control to add glow effect to a focused child control
''' </summary>
''' <remarks>
''' v1.0.0
''' </remarks>
<ToolboxItem(True), ToolboxBitmap(GetType(PanelGlow), "PanelGlow.bmp")>
<System.Diagnostics.DebuggerStepThrough()>
    Public Class PanelGlow
        Inherits Panel

#Region "Initialize"

        Public Sub New()

            ' This call is required by the designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.
            SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
            SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        End Sub
#End Region

#Region "Fields"
    Private _glowColor As Color = Color.Magenta
    Private _glowOn As Boolean
        Private _glowPointColor As Integer = 15

#End Region

#Region "Properties"

        ''' <summary>
        ''' Get or Set the color of the Glow
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Category("PanelGlow")>
        <Description("Get or Set the color of the Glow")>
    <DefaultValue(GetType(Color), "Magenta")>
    Public Property GlowColor As Color
            Get
                Return _glowColor
            End Get
            Set(ByVal Value As Color)
                _glowColor = Value
                Invalidate()
            End Set
        End Property
        ''' <summary>
        ''' Get or Set the point color of the Glow
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Category("PanelGlow")>
        <Description("Get or Set the integer color of the Glow")>
        <DefaultValue(GetType(Integer), "15")>
        Public Property GlowPointColor As Integer
            Get
                Return _glowPointColor
            End Get
            Set(ByVal Value As Integer)
                _glowPointColor = Value
                Invalidate()
            End Set
        End Property

        ''' <summary>
        ''' Turn the Glow effect on or off
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Category("PanelGlow")>
        <Description("Turn the Glow effect on or off")>
        <DefaultValue(False)>
        Public Property GlowOn As Boolean
            Get
                Return _glowOn
            End Get
            Set(ByVal Value As Boolean)
                _glowOn = Value
                Invalidate()
            End Set
        End Property
#End Region

#Region "Paint"

        Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
            MyBase.OnPaintBackground(e)

            If DesignMode = True AndAlso Controls.Count = 0 Then
                TextRenderer.DrawText(e.Graphics,
                                  String.Format("Drop a control{0}on the gGlowBox", vbNewLine),
                                  New Font("Arial", 8, FontStyle.Bold),
                                  New Point(20, 20),
                                  Color.DarkBlue)
                'TextRenderer.DrawText(e.Graphics,
                '                     "SSDiver2112",
                '                     New Font("Arial", 7, FontStyle.Bold),
                '                     New Point(Width - 75, Height - 17),
                '                     Color.LightGray)
            ElseIf _glowOn Then

                Using gp As New GraphicsPath
                    Dim _Glow = _glowPointColor
                    Dim _Feather = 50
                    'Get a Rectangle a little smaller than the Panel's
                    'and make a GraphicsPath with it
                    Dim rect As Rectangle = DisplayRectangle
                    rect.Inflate(-5, -5)
                    gp.AddRectangle(rect)

                    'Draw multiple rectangles with increasing thickness and transparency
                    For i As Integer = 1 To _Glow Step 2
                        Dim aGlow As Integer = CInt(_Feather -
                      ((_Feather / _Glow) * i))
                        Using pen As Pen =
                        New Pen(Color.FromArgb(aGlow, _glowColor), i) With
                        {.LineJoin = LineJoin.Round}

                            e.Graphics.DrawPath(pen, gp)

                        End Using

                    Next i

                End Using
            End If

        End Sub
#End Region

#Region "Sizing"

        Private Sub gGlowBox_Layout(ByVal sender As Object, ByVal e As LayoutEventArgs) Handles Me.Layout

            'Resize the gGlowBox to fit in the Child Control size
            If Controls.Count > 0 Then
                If e.AffectedControl Is Controls(0) Then
                    Size = New Size(Controls(0).Width + 7, Controls(0).Height + 7)
                    Controls(0).Location = New Point(4, 4)
                    Invalidate()
                End If

            End If

        End Sub

        Private Sub PanelGlow_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize

            'This is needed to avoid resizing an Anchored gGlowBox when the parent Form is Minimized 
            If IsNothing(FindForm) OrElse FindForm.WindowState = FormWindowState.Minimized Then Exit Sub

            'Resize the Child Control to fit the size of the gGlowBox
            If Controls.Count > 0 Then
                Controls(0).Size = New Size(Width - 7, Height - 7)
            End If

        End Sub
#End Region

#Region "Control Focus Event"

        Private Sub PanelGlow_ControlAdded(ByVal sender As Object, ByVal e As ControlEventArgs) Handles Me.ControlAdded
            ' Add handlers to let the gGlowBox know when the child control gets Focus 
            AddHandler e.Control.GotFocus, AddressOf ChildGotFocus
            AddHandler e.Control.LostFocus, AddressOf ChildLostFocus
        End Sub

        Private Sub ChildGotFocus()

            If Controls.Count > 0 Then
                'Check if the control has the ReadOnly property and if so, its value.
                If Not IsNothing(Controls(0).GetType().GetProperty("ReadOnly")) Then
                    GlowOn = Not CallByName(Controls(0), "ReadOnly", CallType.Get)
                Else
                    GlowOn = True
                End If

            End If

        End Sub

        Private Sub ChildLostFocus()
            GlowOn = False
        End Sub
#End Region

    End Class


If I use the manual drag toolbox in the form then the code is as below :

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Me.Flatcombobox1 = New WindowsApplication1.FLATCOMBOBOX(Me.components)
        Me.PanelGlow1 = New panelGlow.PanelGlow()
        Me.PanelGlow1.SuspendLayout()
        Me.SuspendLayout()
        '
        'Flatcombobox1
        '
        Me.Flatcombobox1.BackgroundColor = System.Drawing.Color.White
        Me.Flatcombobox1.BorderColor = System.Drawing.Color.Red
        Me.Flatcombobox1.Color = System.Drawing.Color.Red
        Me.Flatcombobox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed
        Me.Flatcombobox1.DropdownBorderThickness = WindowsApplication1.FLATCOMBOBOX.BorderThickness.Thin
        Me.Flatcombobox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.Flatcombobox1.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.Flatcombobox1.FormattingEnabled = True
        Me.Flatcombobox1.IndicatorColor = System.Drawing.Color.Red
        Me.Flatcombobox1.ItemHeight = 26
        Me.Flatcombobox1.Items.AddRange(New Object() {"A", "A1", "A2", "A3"})
        Me.Flatcombobox1.Location = New System.Drawing.Point(4, 4)
        Me.Flatcombobox1.Name = "Flatcombobox1"
        Me.Flatcombobox1.Size = New System.Drawing.Size(260, 32)
        Me.Flatcombobox1.TabIndex = 1
        '
        'PanelGlow1
        '
        Me.PanelGlow1.Controls.Add(Me.Flatcombobox1)
        Me.PanelGlow1.Location = New System.Drawing.Point(23, 12)
        Me.PanelGlow1.Name = "PanelGlow1"
        Me.PanelGlow1.Size = New System.Drawing.Size(267, 39)
        Me.PanelGlow1.TabIndex = 2
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(315, 261)
        Me.Controls.Add(Me.PanelGlow1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.PanelGlow1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub
    Friend WithEvents Flatcombobox1 As FLATCOMBOBOX
    Friend WithEvents PanelGlow1 As panelGlow.PanelGlow
End Class

desired result

so I want to directly drag from the toolbox to something like this

desired result

Upvotes: 0

Views: 65

Answers (0)

Related Questions