Inheritance problem with custom control

  • Thread starter Fred Flintstone
  • Start date
F

Fred Flintstone

This is driving me crazy. I've put together a perfect, easy example of
the problem. What I'm doing is creating a custom control, prepopulated
with 4 items when dropped on a form:

- Create a new Control Library project.
- Inherit System.Windows.Forms.Combobox
- In New(), add this:

Me.Items.Add("Test1")
Me.Items.Add("Test2")
Me.Items.Add("Test3")
Me.Items.Add("Test4")

- Build.
- Start a sample project.
- Drop the new control on the form
- Run.
- Pull down combo:

The combo containts Test1 - Test4, TWICE.

The problem is, when the new control is dropped on a form, it adds
this to the "Windows Form Designer Generated Code":

'
'TestCombo1
'
Me.TestCombo1.Items.AddRange(New Object() {"Test1", "Test2", "Test3",
"Test4"})
Me.TestCombo1.Location = New System.Drawing.Point(64, 80)
Me.Morecode...

I can't write any pre-initialized controls because the initialization
is always copied to the application it's being used in, duplicating
the code as demonstrated here. How do I prevent inheritance of the
iniitialization code? (or any code for that matter)

Thanks!!!
 
G

Guest

Use following code can avoid your problem:

Dim myArray As String() = {"Test1", "Test2", "Test3", "Test4"}
Me.DataSource = myArray

Do not need Me.Items.Add("Test1")
Me.Items.Add("Test2")
Me.Items.Add("Test3")
Me.Items.Add("Test4")
any more
 
F

Fred Flintstone

Actually, it doesn't. The code below is copied to the application
code and executed again at runtime. Here's the solution:

if not (Me.DesignMode) then
' Add Items....
End if.

Thanks tho...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top