Question Make Hatch Drop Down

wara06

New member
Joined
Jun 19, 2015
Messages
4
Programming Experience
Beginner
hi
can help me?
I find projects in How to write a HatchStyle Dropdown - CodeProject
I modify it to be like this:
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Public Class ClassCustomDropBox
    Inherits ComboBox

    Public Sub New()
        MyBase.New()
        '
        ' TODO: Add constructor logic here
        '
        Me.DrawMode = DrawMode.OwnerDrawVariable
        ' Me.SetStyle(ControlStyles.DoubleBuffer, True)
        Me.InitializeDropDown()
    End Sub

    Protected Overrides Sub Finalize()
        Try
            MyBase.Dispose()
            Me.Dispose(True)
        Finally
            MyBase.Finalize()
        End Try
    End Sub

    Public Enum hatchType
        Horizontal
        Vertical
        ForwardDiagonal
        BackwardDiagonal
        Cross
        DiagonalCross
        Percent05
        Percent10
        Percent20
        Percent25
        Percent30
        Percent40
        Percent50
        Percent60
        Percent70
        Percent75
        Percent80
        Percent90
        LightDownwardDiagonal
        LightUpwardDiagonal
        DarkDownwardDiagonal
        DarkUpwardDiagonal
        WideDownwardDiagonal
        WideUpwardDiagonal
        LightVertical
        LightHorizontal
        NarrowVertical
        NarrowHorizontal
        DarkHorizontal
        DarkVertical
        DashedDownwardDiagonal
        DashedUpwardDiagonal
        DashedVertical
        DashedHorizontal
        SmallConfetti
        LargeConfetti
        Zigzag
        Wave
        DiagonalBrick
        HorizontalBrick
        Weave
        Plaid
        Divot
        DottedGrid
        DottedDiamond
        Shingle
        Trellis
        Sphere
        SmallGrid
        SmallCheckerBoard
        LargeCheckerBoard
        OutlinedDiamond
        SolidDiamond
    End Enum

    Protected Sub InitializeDropDown()
        ' Me.MaximumSize = 52
        Dim hatch As Type = GetType(hatchType)
        For Each styleName As String In [Enum].GetNames(hatch)
            Me.Items.Add(styleName)
            'If Not Me.Items.Contains(styleName) Then
            '    Me.Items.Add(styleName)
            'End If
        Next
    End Sub

    Private hatch As hatchType

    Public Property ddHatch() As hatchType
        Get
            Return Me.hatch
        End Get
        Set(value As hatchType)
            Me.hatch = value
            Me.Refresh()
        End Set
    End Property

    Protected Overrides Sub OnDrawItem(e As System.Windows.Forms.DrawItemEventArgs)
        ' The following method should generally be called before drawing.
        ' It is actually superfluous here, since the subsequent drawing
        ' will completely cover the area of interest.
        e.DrawBackground()
        'The system provides the context
        'into which the owner custom-draws the required graphics.
        'The context into which to draw is e.graphics.
        'The index of the item to be painted is e.Index.
        'The painting should be done into the area described by e.Bounds.
        'Dim displayText As String = Me.Items(e.Index).ToString()
        'Dim hs As HatchStyle = CType([Enum].Parse(GetType(HatchStyle), displayText, ignoreCase:=True), HatchStyle)

            If e.Index <> -1 Then
                Dim g As Graphics = e.Graphics
                Dim r As Rectangle = e.Bounds

                Dim rd As Rectangle = r
                rd.Width = rd.Left + 25
                Dim rt As Rectangle = r
                r.X = rd.Right
                Dim displayText As String = Me.Items(e.Index).ToString()

                ' TODO add user selected foreground and background colors here
                'Try

                Dim hs As HatchStyle = CType([Enum].Parse(GetType(HatchStyle), displayText, ignoreCase:=True), HatchStyle)

                Dim b As New HatchBrush(hs, Color.Black, e.BackColor)
                g.DrawRectangle(New Pen(Color.Black, 2), rd.X + 3, rd.Y + 2, rd.Width - 4, rd.Height - 4)
                g.FillRectangle(b, New Rectangle(rd.X + 3, rd.Y + 2, rd.Width - 4, rd.Height - 4))

                'Catch ex As Exception
                '    Dim hs As HatchStyle = CType([Enum].Parse(GetType(HatchStyle), displayText, ignoreCase:=True), HatchStyle)
                'End Try

                Dim sf As New StringFormat()
                sf.Alignment = StringAlignment.Near

                'If the current item has focus.

                If (e.State And DrawItemState.Focus) = 0 Then
                    e.Graphics.FillRectangle(New SolidBrush(SystemColors.Window), r)
                    e.Graphics.DrawString(displayText, Me.Font, New SolidBrush(SystemColors.WindowText), r, sf)
                Else
                    e.Graphics.FillRectangle(New SolidBrush(SystemColors.Highlight), r)
                    e.Graphics.DrawString(displayText, Me.Font, New SolidBrush(SystemColors.HighlightText), r, sf)
                End If
            End If

            'Draws a focus rectangle on the specified graphics surface and within the specified bounds.
            e.DrawFocusRectangle()   
    End Sub

    Protected Overrides Sub OnMeasureItem(e As System.Windows.Forms.MeasureItemEventArgs)
        'Work out what the text will be
        Dim displayText As String = Me.Items(e.Index).ToString()

        'Get width & height of string
        Dim stringSize As SizeF = e.Graphics.MeasureString(displayText, Me.Font)

        'Account for top margin
        stringSize.Height += 5

        ' set hight to text height
        e.ItemHeight = CInt(stringSize.Height)

        ' set width to text width
        e.ItemWidth = CInt(stringSize.Width)
    End Sub
End Class

I modify because I wanted to throw 3 hatchstyle (max, min, largegrid)
but after the program is run, the items that appear out 2 times. I want to only appear once. is something wrong with the source code?
actual project of How to write a HatchStyle Dropdown - CodeProject if executed also appeared two times ( hatchstyle) in dropdwon, if it can only appear once?


please help me due to my college thesis. thanks
 
Last edited by a moderator:
It happens in your test project, your control constructor adds items and this happens even in designer, then designer adds code to add these items as well at runtime (as you can see in form.designer.vb file). At runtime the items is first added when control is created, and again when test form initializes. One way to avoid this is to shadow Items property and tell designer not to serialize code for it:
    <System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)> _
    <System.ComponentModel.Browsable(False)> _
    Public Overloads ReadOnly Property Items As ComboBox.ObjectCollection
        Get
            Return MyBase.Items
        End Get
    End Property

As alternative this could be used: Defining Default Values with the ShouldSerialize and Reset Methods
 
hi John,
thanks for your suggestion but I have overcome my problem with remove "Me.InitializeDropDown()"..
Not a good idea at all. What do you think will happen when you now add this control to a form? It will have no items.
 
hi paul,
i'm a newbie.
but when i remove "Me.InitializeDropDown()" n i run the program, the combobox is show item like i want n appear just once.
 
hi paul,
i'm a newbie.
but when i remove "Me.InitializeDropDown()" n i run the program, the combobox is show item like i want n appear just once.
That is because of the previous problem the form designer code has already generated code to add these items (which is wrong). When you add a new control, with the changed control code, it will no longer add the items at all.
 
Back
Top