VB.NET multiple buttons implementation?

  • Thread starter Thread starter Dan Dautrich
  • Start date Start date
D

Dan Dautrich

I'm trying to create a custom user control using VB.NET 2002 and ran across
a stumbling block. I need an indefinite number of buttons to display across
the control, based on its size. I'm not sure how to do this because I
cannot make an array of buttons without losing Events ability and I've
failed trying to use a collection of buttons because they don't show up on
the form. Any suggestions?

Thanks,
Dan ([email protected])
 
Dan:

I guess resize is what's triggering your code to add the new button(s). You
can add an Event handler dynamically at runtime after you create your
control and add it to the form's controls collection
http://www.informit.com/isapi/produ...-4F5D-8316-0AFDB0EA806C}/content/articlex.asp

You can also add an existing event handler so if you have a button called
Button1 that's with the control originally, and you now dynamically create
SomeNewButton, you can use the handler from Button1 if you wanted (assuming
that was the functionality you are looking for).
 
* "Dan Dautrich said:
I'm trying to create a custom user control using VB.NET 2002 and ran across
a stumbling block. I need an indefinite number of buttons to display across
the control, based on its size. I'm not sure how to do this because I
cannot make an array of buttons without losing Events ability and I've
failed trying to use a collection of buttons because they don't show up on
the form. Any suggestions?

Creating Control Arrays in Visual Basic .NET and Visual C# .NET
<http://msdn.microsoft.com/library/?...ngControlArraysInVisualBasicNETVisualCNET.asp>
 
Dan Dautrich said:
I'm trying to create a custom user control using VB.NET 2002 and ran
across a stumbling block. I need an indefinite number of buttons to
display across the control, based on its size. I'm not sure how to
do this because I cannot make an array of buttons without losing
Events ability and I've failed trying to use a collection of buttons
because they don't show up on the form. Any suggestions?

I don't know what you've tried, but this should work:

private buttons as button()
'...

redim buttons(3)
for i=0 to 3
buttons(i) = new button
me.controls.add(buttons(i))
addhandler buttons(i).click, addressof OnButtonClick
next i

OnButtonClick is the procedure handling the click event of the button. You
can also use an arraylist instead of an array to store the buttons.
 
Hi Dan,

The event is the same as with Armin, but I have some examples,

I hope this helps

Cor

\\\Winform
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim start As Integer = 4
Dim top As Integer = 25
Dim i As Integer
Dim nowdate As DateTime = DateTime.Now
Dim mybutton(nowdate.Month) As Button
For i = 0 To System.DateTime.DaysInMonth _
(nowdate.Year, nowdate.Month) - 1
mybutton(i) = New Button
mybutton(i).TextAlign = ContentAlignment.MiddleCenter
mybutton(i).Width = 40
mybutton(i).Height = 20
mybutton(i).FlatStyle = FlatStyle.Flat
mybutton(i).BackColor = Drawing.Color.AntiqueWhite
mybutton(i).Location = New System.Drawing.Point(start, top)
mybutton(i).Text = (i + 1).ToString
mybutton(i).Cursor = Cursors.Hand
Me.Controls.Add(mybutton(i))
AddHandler mybutton(i).Click, AddressOf mybutton_Click
AddHandler mybutton(i).MouseHover, AddressOf mybutton_Hoover
AddHandler mybutton(i).MouseLeave, AddressOf mybutton_Leave
start = start + 40
If (i + 1) Mod 5 = 0 Then
top = top + 20
start = 4
End If
Next
End Sub
Private Sub mybutton_Click _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim thisbutton As Button = DirectCast(sender, Button)
MessageBox.Show("The day is: " & thisbutton.Text)
End Sub
Private Sub mybutton_Hoover _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim thisbutton As Button = DirectCast(sender, Button)
thisbutton.BackColor = Drawing.Color.AliceBlue
End Sub
Private Sub mybutton_Leave _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim thisbutton As Button = DirectCast(sender, Button)
thisbutton.BackColor = Drawing.Color.AntiqueWhite
End Sub
///
\\\Webform
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim mybutton(31) As Button
Dim i As Integer
For i = 0 To New Date().DaysInMonth _
(New Date().Year, New Date().Month) - 1
mybutton(i) = New Button
mybutton(i).BackColor = Drawing.Color.White
mybutton(i).Text = (i + 1).ToString
mybutton(i).Width = New Unit(30)
Me.Panel1.Controls.Add(mybutton(i))
AddHandler mybutton(i).Click, AddressOf mybutton_Click
If (i + 1) Mod 5 = 0 Then
Me.Panel1.Controls.Add(New LiteralControl("<BR>"))
End If
Next
End Sub
Private Sub mybutton_Click _
(ByVal sender As Object, ByVal e As System.EventArgs)
Dim mylabel As New Label
Me.Panel1.Controls.Add(New LiteralControl("<BR><BR>"))
Me.Panel1.Controls.Add(mylabel)
mylabel.Text = "The day is: " & DirectCast(sender, Button).Text
End Sub
///
 
Back
Top