How to customer usercontrol

isawa

Active member
Joined
Oct 3, 2005
Messages
25
Programming Experience
Beginner
I'm created my usercontrol that have one control Label and then i'm use my control in my form but i'm con't to use property about my control how i can. vb.net 2005

thanks.
 
I mean my usercontrol don't have some property in my windowform when i use my usercontrol. so, u have some e-book about create usercontrol?
 
isawa, if my interpretation of your question is successful, the answer is to make it Public.
 
Like any class, if you want your UserControl to have a particular property then it's up to you to implement it. You have to provide all the properties and methods yourself, other than the ones that are inherited.
 
I changed my UserControl to PUBLIC, when i build my windowform some any changed in my userControl don't changed too. but, whan i code in editor code like a UserControl11.Label1.Text = "aaaaa" that's OK. I like to change properties TEXT in window properties. How?

Thanks,
 
Set the BrowsableAttribute attribute to True:

VB.NET:
[COLOR=black][FONT=Courier New]<Browsable([COLOR=blue]True[/COLOR])> _
[COLOR=blue]Public[/COLOR] [COLOR=blue]Overrides[/COLOR] [COLOR=blue]Property[/COLOR] Text() [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR]
    [COLOR=blue]Get[/COLOR]
        [COLOR=blue]Return[/COLOR] [COLOR=blue]MyBase[/COLOR].Text
    [COLOR=blue]End[/COLOR] [COLOR=blue]Get[/COLOR]
    [COLOR=blue]Set[/COLOR]([COLOR=blue]ByVal[/COLOR] value [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR])
        [COLOR=blue]MyBase[/COLOR].Text = value
        LabelText.Text = Text
    [COLOR=blue]End[/COLOR] [COLOR=blue]Set[/COLOR]
[COLOR=blue]End[/COLOR] [COLOR=blue]Property[/COLOR]
[/FONT][/COLOR]
 
you don't make property to add, but method, for instance
VB.NET:
public function AddToCombo(item as string) as integer
  return me.combobox.items.add(item)
end function
 
I think isawa means: how to add a property to a usercontrol to set the items of an 'embedded' combobox in design time.

One possibility I can think of is to expose the items as an array of string (you could store them internally as an ArrayList or List(of String) in a private variable and use that as the DataSource for the embedded Combobox) :
VB.NET:
[COLOR=black][FONT=Courier New]<Description([COLOR=maroon]"The items of the Combobox in this User Control as String()"[/COLOR]), _
Category([COLOR=maroon]"Data"[/COLOR])> _
[COLOR=blue]Public[/COLOR] [COLOR=blue]Property[/COLOR] Items() [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR]()
    [COLOR=blue]Get[/COLOR]
        [COLOR=blue]Dim[/COLOR] result(ComboBoxItems.Items.Count - 1) [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR]
        ComboBoxItems.Items.CopyTo(result, 0)
        [COLOR=blue]Return[/COLOR] result
    [COLOR=blue]End[/COLOR] [COLOR=blue]Get[/COLOR]
    [COLOR=blue]Set[/COLOR]([COLOR=blue]ByVal[/COLOR] value [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR]())
        ComboBoxItems.Items.Clear()
        ComboBoxItems.Items.AddRange(value)
    [COLOR=blue]End[/COLOR] [COLOR=blue]Set[/COLOR]
[COLOR=blue]End[/COLOR] [COLOR=blue]Property[/COLOR]
[/FONT][/COLOR]
 
I think it would be better to just use the property of the UserControl as a pass-through for the ComboBox property:
VB.NET:
    Public ReadOnly Property items() As ComboBox.ObjectCollection
        Get
            Return Me.ComboBox1.Items
        End Get
    End Property
That way you get exactly the same behaviour as if you were dealing with the ComboBox directly. The previously suggested implementation restricts you to Strings and it also does not allow you to add or remove individual items. It would probably be appropriate to do the same thing for the DataSource, etc. properties so that you can bind to the ComboBox as well. By exposing the members of the ComboBox in this way you allow the UserControl to be used as though it was a ComboBox in many ways, without actually exposing the ComboBox itself as a public member, thus allowing unfettered access to it.
 
I means, in my UserControl had 2 Control about Button1 and Button2, when i used my Control in my window form. How can i use event Click of Button2 at property window?
 
Back
Top