Home All Groups Group Topic Archive Search About

forms control in a class

Author
2 Apr 2010 7:41 PM
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

Author
2 Apr 2010 8:07 PM
Armin Zingler
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?


Show quoteHide quote
> 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).


--
Armin
Author
2 Apr 2010 8:37 PM
kpg
> 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