Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8179839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:05:13+00:00 2026-06-07T00:05:13+00:00

Below is code to an inherited ComboBox. The issue is that the ComboBox is

  • 0

Below is code to an inherited ComboBox. The issue is that the ComboBox is being populated (PopulateComboBox()) multiple times.

Edit: I’ve taken the advice of Amit Mittal (find his answer below) and implemented ISupportInitialize. Now PopulateComboBox() is only called at run-time, like it should be.

By this implementation, the items should be populated during run-time, and destroyed on exit. However, the designer itself is storing these values when they’re created run-time, and not destroying after run-time.

Is there an elegant solution to the implementation of this code?

Public Class ComboBoxExColors
    Inherits ComboBox
    Implements ISupportInitialize

    Public Sub New()
        MyBase.New()
        Me.Size = New Size(146, 23)
        Me.DropDownStyle = ComboBoxStyle.DropDownList
        Me.MaxDropDownItems = 16
    End Sub

    Public Sub BeginInit() Implements System.ComponentModel.ISupportInitialize.BeginInit
        ' Do nothing?
    End Sub

    Public Sub EndInit() Implements System.ComponentModel.ISupportInitialize.EndInit
        Me.DrawMode = DrawMode.OwnerDrawVariable ' fixed or variable?
        Me.PopulateComboBox()
    End Sub

    Public Sub PopulateComboBox()
        'Me.Items.Clear() ' rather than forcing items to be cleared, is there a more elegant solution to the implementation of this code, rather than forcing an item clear that shouldn't exist to begin with?
        Me.Items.Add("Default")
        Me.Items.Add("Custom")
        Dim KnownColors() As String = System.Enum.GetNames(GetType(System.Drawing.KnownColor)) ' get all colors
        For Each c As String In KnownColors ' add non system colors
            If Not Color.FromName(c).IsSystemColor Then
                Me.Items.Add(c)
            End If
        Next c
    End Sub

    Protected Overrides Sub OnDrawItem(ByVal e As DrawItemEventArgs)
        ' this draws each item onto the control
        If e.Index > -1 Then
            Dim item As String = Me.Items(e.Index).ToString

            e.DrawBackground()
            e.Graphics.DrawString(item, e.Font, SystemBrushes.WindowText, e.Bounds.X, e.Bounds.Y)
            e.DrawFocusRectangle()
        End If
    End Sub

End Class
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:05:14+00:00Added an answer on June 7, 2026 at 12:05 am

    The problem is PopulateComboBox is being invoked from the constructor.

    When you drop your combobox on designer surface designer internally invokes its constructor to get default values of its properties (including Item property). And then, the code behind calls the constructor and after that explicitly specifies the design time properties as well (which in your case seems to be the ‘default’ values only). Hence duplicating of Items.

    Proper way to initialize a custom control is by implementing ISupportInitialize interface.

    EndInit will be called exactly once after designer’s code has applied all the design time properties (if you are not using a designer then it will be your responsibility to call it).

    So rule of thumb is

    1. For default values, specify in constructor
    2. To ‘initialize’ use ISupportInitialize.

    For instance in addition to calling PopulateComboBox from it, you can also use it to force DrawMode property to OwnerDrawFixed as this is crucial for your inherited control and you would not want to let the user of the control to specify anything else at design time (though this will not exactly prevent design time ‘specification’ of a different value but regardless of that at run time you will have always forced the correct value.)

    Note:

    1. After implementing this interface You may need to re-build the project and then again do a ‘drag-drop’ of the inherited control on the designer surface. You can inspect the automagically created designer code to verify that EndInit is indeed being invoked.
    2. As mentioned earlier as well, ISupportInitialize is used by the designer. If you do not use designer, it is your responsibility to call its methods.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

below code is my databasehandler class i got it from a tutorial. Beside that
I have some ASP code that I've inherited from my predecessor (no, it's not
I'm working with some JS code that I inherited on a project, and in
I have a weird issue that I am being asked to fix but alas
I inherited some code that has two non-UI threads that update various WinForm controls.
I've inherited a site that uses Google's GSNewsBar . The wizard creates code that
I inherited sites where previous developer used the spaghetti code below to handle relative
Below, I inserted a code written by Ray Konopka (part of the Coderage presentation).
Below code saying error incorreect syntax near Main INSERT INTO tbl ( 'Week', Main,
Below code : URL oracle = new URL(http://www.oracle.com/); URLConnection inputStream =oracle.openConnection(); InputStream in =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.