Discussion:
Two Different Types
(too old to reply)
Derek Hart
2009-09-21 22:01:48 UTC
Permalink
If I have code like the following, and I want to send in two different types
for uc, and they both have the exact same named controls insie them, how
would I do this? I cannot use uc.MyCombobox1 because uc is just a generic
user control.

Private Sub VaultFillComboBoxes(ByVal uc As UserControl)
With uc.MyCombobox1
.Items.Add("Personal")
.Items.Add("Business")
.Items.Add("Tax")
End With

I need to be able to do the following:
If TypeOf uc is uc1 then...
If TypeOf uc is uc2 then...

So then I can just use one line of code and not have to duplicate the entire
procedure twice.
With uc.MyCombobox1
Eddie
2009-09-21 23:50:21 UTC
Permalink
Post by Derek Hart
If I have code like the following, and I want to send in two different types
for uc, and they both have the exact same named controls insie them, how
would I do this? I cannot use uc.MyCombobox1 because uc is just a generic
user control.
< snip >

The easiest way might be to make your UserControl-derived classes
implement an interface which has a property that exposes the ComboBox.

Then your VaultFillComboBoxes() method can cast the UserControl
parameter to that interface, and use the property to access the ComboBox.
--
Eddie ***@deguello.org
John Bundy
2009-09-23 15:42:01 UTC
Permalink
I don't have a clue what the heck Eddie is trying to say... I will give you
the easy to use version.
This will populate every combobox on your form, if you want specific ones or
to exclude some, use Control.Name.
For Each Control As Control In Me.Controls
If Control.GetType.ToString = "System.Windows.Forms.ComboBox" Then

Dim cBox As New ComboBox
cBox = Control

With cBox
.Items.Add("Personal")
.Items.Add("Business")
.Items.Add("Tax")
End With
End If
Next
--
-John http://www.jmbundy.blogspot.com/
Please rate when your question is answered to help us and others know what
is helpful.
Post by Derek Hart
If I have code like the following, and I want to send in two different types
for uc, and they both have the exact same named controls insie them, how
would I do this? I cannot use uc.MyCombobox1 because uc is just a generic
user control.
Private Sub VaultFillComboBoxes(ByVal uc As UserControl)
With uc.MyCombobox1
.Items.Add("Personal")
.Items.Add("Business")
.Items.Add("Tax")
End With
If TypeOf uc is uc1 then...
If TypeOf uc is uc2 then...
So then I can just use one line of code and not have to duplicate the entire
procedure twice.
With uc.MyCombobox1
Loading...