'Custom Combobox with values
I have a windows application with multiple Forms containing the same ComboBoxes.
These Comboboxes have fixed items. On each Form I load the Comboboxes with these values.
For example:
Public Sub New()
InitializeComponent()
With Combobox1.Items
.Insert(0, "Value1")
.Insert(1, "Value2")
.Insert(2, "Value3")
End With
With Combobox2.Items
.Insert(0, "Value4")
.Insert(1, "Value5")
.Insert(2, "Value6")
End With
End Sub
I don't want to repeat this code on every Form so, is there a way to create a custom Combobox that is pre-filled with these values, and then inherit the Comboboxes on each Form? Or is there another proper solution?
UPDATE 1 Based on jmcilhinney's comment, I have created this class
Public Class ComboClass
Inherits System.Windows.Forms.ComboBox
Public Sub New()
End Sub
Sub New(ByVal sender As String)
If sender = "R" Then
With Items
.Insert(0, "Value1")
.Insert(1, "Value2")
.Insert(2, "Value3")
End With
Else
With Items
.Insert(0, "Value4")
.Insert(1, "Value5")
.Insert(2, "Value6")
End With
End If
End Sub
End Class
And then I added the new custom control to my Form.
In the Private Sub InitializeComponent() method designer code, I have the following:
Me.Combobox1 = New MyApplication.ComboClass("R")
Me.Combobox2 = New MyApplication.ComboClass("A")
I get the following designer error:
The variable 'Combobox1' is either undeclared or was never assigned.
The variable 'Combobox2' is either undeclared or was never assigned.
I'm missing something but I don't know what it is.
Thanks for your help.
Solution 1:[1]
Instead of using the class Constructor (which isn't much useful if you want to create your controls in the Designer), you could add a Public Property that accepts an Enumerator (or even a String value, as you're doing here) that lets you define, at Design Time, pre-defined data sets when the property value is changed.
As a note, avoid tampering with the Designer.vb code. It's handled by the Form Designer, you don't usually write anything here: it will be delete as soon as the Form design is modified.
The custom Property is called CustomDataSet, here.
The code here also drops down the ComboBox List, so you can see what has been set.
This is how it works:
Imports System.ComponentModel
<DesignerCategory("Code")>
Public Class ComboClass
Inherits ComboBox
Private Selector As DataSelector = DataSelector.None
Public Enum DataSelector
None = 0
Set1
Set2
End Enum
Public Sub New()
' Initialization code, if needed
End Sub
<DefaultValue(DataSelector.None)>
<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
Public Property CustomDataSet As DataSelector
Get
Return Selector
End Get
Set(ByVal value As DataSelector)
Selector = value
Me.SetData()
End Set
End Property
Private Sub SetData()
Me.Items.Clear()
Select Case Me.Selector
Case DataSelector.None
Case DataSelector.Set1
Me.Items.AddRange({"Value1", "Value2", "Value3"})
Case DataSelector.Set2
Me.Items.AddRange({"Value6", "Value7", "Value8"})
Case Else
'NOP
End Select
If DesignMode Then Me.DroppedDown = True
End Sub
End Class
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

