forms control in a class

  • Thread starter Thread starter kpg
  • Start date Start date
K

kpg

Hi all,

I have a vb.net class with this in it:

Public Sub SetValues(ByVal obj As Object)
DirectCast(obj, ComboBox).Items.Add("Value1")
DirectCast(obj, ComboBox).Items.Add("Value2")
End Sub

Problem: ComboBox is unrecognized becuase this is a class library, not a
form. So I add:

Imports System.Windows.Forms

and I get an error -

Warning 1 Namespace or type specified in the Imports
'System.Windows.Forms' doesn't contain any public member or cannot be
found. Make sure the namespace or the type is defined and contains at least
one public member. Make sure the imported element name doesn't use any
aliases.

Also adding this does not help (of course):

DirectCast(obj, System.Windows.Forms.ComboBox).Items.Add("Value1")

So how can I have a method that accepts a ComboBox object and manipulates
it?

Thanks,

kpg
 
Am 02.04.2010 21:41, schrieb kpg:
Hi all,

I have a vb.net class with this in it:

Public Sub SetValues(ByVal obj As Object)
DirectCast(obj, ComboBox).Items.Add("Value1")
DirectCast(obj, ComboBox).Items.Add("Value2")
End Sub


Is there a reason why obj is not declared As Combobox?

Problem: ComboBox is unrecognized becuase this is a class library, not a
form. So I add:

Imports System.Windows.Forms

and I get an error -

Warning 1 Namespace or type specified in the Imports
'System.Windows.Forms' doesn't contain any public member or cannot be
found. Make sure the namespace or the type is defined and contains at least
one public member. Make sure the imported element name doesn't use any
aliases.

Also adding this does not help (of course):

DirectCast(obj, System.Windows.Forms.ComboBox).Items.Add("Value1")

So how can I have a method that accepts a ComboBox object and manipulates
it?


Have you added a reference to a library containing that namespace and type?
You see the containing assembly at the top of each class' documentation
(System.Windows.Forms.dll in this case).
 
Have you added a reference to a library containing that namespace and
type? You see the containing assembly at the top of each class'
documentation (System.Windows.Forms.dll in this case).

Well, I have now...and that fixed it.

...and there is no reason the obj is not declared as a ComboBox, except I
suppose this would work for any control that supports a list, but then it
gets cast as a combobox, so no, there is no good reason.

thx
kpg
 
Back
Top