question on User Control...

dec21st

Active member
Joined
May 14, 2005
Messages
43
Programming Experience
3-5
i have a situation here...

i want to dynamically add buttons on my form.... say if i retrieve a number from my db (e.g. 5) then i wanna have 5 buttons in my form.

what i've plan to do is to......
...created a user control that has a button. so what i want to do is to dynamically call the user control.

i'll declare an int variable to retrieve a number for my db... say if the number is 5, i want to create 5 of the user control in my form.

and another problem is that when i insert 5 of those controls in my form, i want to pass a parameter to it.

so my question is that can this be possible?

is this possible?

my objecttive is just to create buttons accordingly (to the number retrieved) and crreate the button dynamically to have diff text, location, and also image
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,321
Location
Sydney, Australia
Programming Experience
10+
You can create as many Buttons or UserControls as you like, but you'll need to assign each one to a variable and keep track of which is which. I would suggest that you add the buttons to a Collection as you create them. You would probably use an ArrayList if you just want to access them via an index, or a SortedList or HashTable if you want to access them via a name or some other identifier. You would create your control:

Dim myButton As New MyButtonType(parameter)

then, once you've set the necessary properties you would add it to the Form:

Me.Controls.Add(myButton)

and finally add it to your Collection:

Me.myButtonList.Add(myButton)
 

dec21st

Active member
Joined
May 14, 2005
Messages
43
Programming Experience
3-5
thanks for the fast reply...

but how do i add the 'Me.myButtonList.Add(myButton)' in the collection?

saying that i'm doing all these in a while loop

for eg,

function addTable()
Dim myButton As New button
counter1 = 0
counter2 = 5
while counter1 < counter2
Me.Controls.Add(myButton)
button.name = "btnOne"
button.text = "One"
Me.Controls.Add(myButton)
end while
end function

how do i add the 'Me.myButtonList.Add(myButton)'???
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,321
Location
Sydney, Australia
Programming Experience
10+
VB.NET:
[color=Blue]Public Class[/color] MyForm
[color=Blue]     Inherits[/color] Form

[color=Green]     'Create a list for the dynamically created buttons.[/color]
[color=Blue]     Private[/color] buttonList [color=Blue]As New[/color] ArrayList

[color=Blue]     Private Sub[/color] AddButtons()
[color=Blue]         Dim[/color] buttonCount [color=Blue]As Integer[/color] [color=Green]'The number of buttons to add.[/color]
[color=Blue]         Dim[/color] myButton [color=Blue]As[/color] Button

[color=Green]         'Get the the number of buttons to add from database or elsewhere and assign to buttonCount.
         '...[/color]

[color=Green]         'Remove previous buttons if necessary.[/color]
[color=Blue]         For[/color] i [color=Blue]As Integer[/color] = [color=Blue]Me[/color].buttonList.Count - 1 [color=Blue]To[/color] 0 [color=Blue]Step[/color] -1
[color=Blue]             Me[/color].buttonList(i).Dispose()
[color=Blue]         Next[/color]
[color=Blue]         Me[/color].buttonList.Clear()

[color=Green]         'Create the new buttons.[/color]
[color=Blue]         For[/color] i [color=Blue]As Integer[/color] = 0 [color=Blue]To[/color] buttonCount - 1 [color=Blue]Step[/color] 1
            myButton = [color=Blue]New[/color] Button

[color=Green]             'Each button must have a different name.[/color]
            myButton.Name = "btn" & (i + 1).ToString()

[color=Green]             'Set other button properties, e.g. Text, Location, Size, etc. and add event handlers.
             '...[/color]

[color=Green]             'Add the button to the Form.[/color]
[color=Blue]             Me[/color].Controls.Add(myButton)

[color=Green]             'Add the button to the dynamic list.[/color]
[color=Blue]             Me[/color].buttonList.Add(myButton)
[color=Blue]         Next[/color]
[color=Blue]     End Sub[/color]
[color=Blue] End Class[/color]
Note that the Collection (in this case an ArrayList) is not strictly necessary because each Button will have a unique Name, but it allows you to keep all the dynamically created buttons grouped together for easy access, like when removing them. I have a project of my own that does something very similar, where each button represents a class on a timetable and buttons are added and removed as the week displayed changes.
 
Last edited:

dec21st

Active member
Joined
May 14, 2005
Messages
43
Programming Experience
3-5
cool.. i got it working

but just another question... sorry coz i'm a noob here. hope u don't mind

when i create the buttons, how do i code a sub when the button is clicked?

so is it better if i put the button in a user control and adds it ?

but i'm not sure whether can i pass a parameter to a button in a user conttrol when i add it....

what do i write in the user control / button to receive the parameter?
for user control can i put it on the formload?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,321
Location
Sydney, Australia
Programming Experience
10+
Just write a procedure with the same signature as a Button.Click event handler, like so:
VB.NET:
[color=Blue]Private Sub[/color] dynamicButton_Click([color=Blue]ByVal[/color] sender [color=Blue]As[/color] System.Object, [color=Blue]ByVal[/color] e [color=Blue]As[/color] System.EventArgs)
	[color=Green]'Perform desired actions.[/color]
[color=Blue] End Sub[/color]
When you create your button, add the event handler like this:
VB.NET:
[color=Blue]Dim[/color] dynamicButton [color=Blue]As New[/color] Button

[color=Blue]AddHandler[/color] dynamicButton.Click, [color=Blue]AddressOf[/color] dynamicButton_Click
When the event handler is called, you can access the actual button that raised the event via the sender parameter, which you can cast as a Button like this:
VB.NET:
[color=Blue]Dim[/color] clickedButton [color=Blue]As[/color] Button = [color=Blue]CType[/color](sender, Button)
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,321
Location
Sydney, Australia
Programming Experience
10+
kulrom said:
jmcilhinney i hope you don't mind cuz of my post
What you post is up to you, but if you create a project for everyone who asks a question it doesn't really encourage people to think for themselves.
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
jmcilhinney said:
What you post is up to you, but if you create a project for everyone who asks a question it doesn't really encourage people to think for themselves.
I agree with you. But science is telling us we are wrong ...

[font=Palatino, Book Antiqua]The kind of training and coaching people are given makes a big difference in how much they will retain. In general people in a learning situation retain:[/font]

[font=Palatino, Book Antiqua]
10% of what they read
[/font][font=Palatino, Book Antiqua]
20% of what they hear
30% of what they see
50% of what they see and hear
70% of what they talk over with others
80% of what they use and do in real life
95% of what they teach someone else to do[/font]


another article about this: www.humsci.auburn.edu/abell/beeprogram/training/pasttraining/adultlearners.pdf

Cheers ;)
 
Last edited:

dec21st

Active member
Joined
May 14, 2005
Messages
43
Programming Experience
3-5
hey, thanks in advance for the codes

i really appreciate the posting by the 2 of u

i'll check it out later then see how it goes... will tell u about it
 
Top Bottom