Changed properties of control causes undesirable code

G

Guest

I've created a component that inherits from ComboBox. Wiithin its
constructor, I've hardcoded some initial items that I'd like added. These
items are of a custom class.

When I add my control to a Form, the code-behind file contains code for each
item. This seems unecessary and causes items to be added more than once
(first, because of my ComboBox constructor; again, because of the code-behind
items).

I suspect that there may be a way, within my ComboBox constructor, that I
may reset a flag indicating the Items property has changed. Is that true?
If so, how might I do that?
 
R

rowe_newsgroups

I've created a component that inherits from ComboBox. Wiithin its
constructor, I've hardcoded some initial items that I'd like added. These
items are of a custom class.

When I add my control to a Form, the code-behind file contains code for each
item. This seems unecessary and causes items to be added more than once
(first, because of my ComboBox constructor; again, because of the code-behind
items).

I suspect that there may be a way, within my ComboBox constructor, that I
may reset a flag indicating the Items property has changed. Is that true?
If so, how might I do that?

Alright, I normally shy away from these questions as I don't work with
designer representation of custom controls very often. With that said
you might double check my code for any errors or initialization
problems.

Basically, my approach is to create a ControlDesigner that will
prevent the Visual Studio designer from listing the items added in the
constructor in the Form's code-behind. I don't know if this is the
correct approach, but it should get you started. Below is the sample
code for a custom control that inherits from ComboBox and adds some
list items in it's constructor, and the designer to go with it.

///////////////
Imports System.ComponentModel

'// Assumes a reference to System.Design

<DesignerAttribute(GetType(MyComboBoxDesigner))> _
Public Class MyComboBox
Inherits ComboBox

Public Sub New()
MyBase.New()

Items.Add("My Item 1")
Items.Add("My Item 2")
Items.Add("My Item 3")
End Sub

End Class

Public Class MyComboBoxDesigner
Inherits System.Windows.Forms.Design.ControlDesigner

Public Overrides Sub InitializeNewComponent(ByVal defaultValues As
System.Collections.IDictionary)
MyBase.InitializeNewComponent(defaultValues)

'// Reset the text to a blank string.
'// Using a custom designer stops this action from
'// happening by default.
Control.Text = ""

'// Try to convert the Control into a ComboBox
Dim comboBox As ComboBox = TryCast(Control, ComboBox)

'// Check to make sure the conversion worked and then
'// clear out the designer's Items collection
If comboBox IsNot Nothing Then comboBox.Items.Clear()
End Sub

End Class
///////////////

Let me know how it works!

Thanks,

Seth Rowe
 
G

Guest

That seems to have worked. Thanks.

rowe_newsgroups said:
Alright, I normally shy away from these questions as I don't work with
designer representation of custom controls very often. With that said
you might double check my code for any errors or initialization
problems.

Basically, my approach is to create a ControlDesigner that will
prevent the Visual Studio designer from listing the items added in the
constructor in the Form's code-behind. I don't know if this is the
correct approach, but it should get you started. Below is the sample
code for a custom control that inherits from ComboBox and adds some
list items in it's constructor, and the designer to go with it.

///////////////
Imports System.ComponentModel

'// Assumes a reference to System.Design

<DesignerAttribute(GetType(MyComboBoxDesigner))> _
Public Class MyComboBox
Inherits ComboBox

Public Sub New()
MyBase.New()

Items.Add("My Item 1")
Items.Add("My Item 2")
Items.Add("My Item 3")
End Sub

End Class

Public Class MyComboBoxDesigner
Inherits System.Windows.Forms.Design.ControlDesigner

Public Overrides Sub InitializeNewComponent(ByVal defaultValues As
System.Collections.IDictionary)
MyBase.InitializeNewComponent(defaultValues)

'// Reset the text to a blank string.
'// Using a custom designer stops this action from
'// happening by default.
Control.Text = ""

'// Try to convert the Control into a ComboBox
Dim comboBox As ComboBox = TryCast(Control, ComboBox)

'// Check to make sure the conversion worked and then
'// clear out the designer's Items collection
If comboBox IsNot Nothing Then comboBox.Items.Clear()
End Sub

End Class
///////////////

Let me know how it works!

Thanks,

Seth Rowe
 

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