|
-
Feb 4th, 2009, 11:56 PM
#1
Thread Starter
Member
[RESOLVED] control array of my button?
hello sirs,,
i am a beginner of VBnet
may i know how to make a conrol array of my control?
ex:
Button1(0)
Button1(1)
Button1(2)
...........
in vb6 i have no problem on doing this.. but in vbnet i dont know..hehehe
thnx in advance..
-
Feb 5th, 2009, 12:12 AM
#2
Re: control array of my button?
Here is how to do that:
Code:
Dim myButtons(2) As Button
For i As Integer = 0 To myButtons.Length - 1
Dim btn As New Button
' Set some properties of the button control.
btn.Name = "button" & CStr(i)
'...
'...
myButtons(i) = btn
Next
-
Feb 5th, 2009, 12:48 AM
#3
Thread Starter
Member
Re: control array of my button?
 Originally Posted by VBDT
Here is how to do that:
Code:
Dim myButtons(2) As Button
For i As Integer = 0 To myButtons.Length - 1
Dim btn As New Button
' Set some properties of the button control.
btn.Name = "button" & CStr(i)
'...
'...
myButtons(i) = btn
Next
thank you sir for ur fast reply..
actually i want to see those 3 buttons on my form
here is my VB6 code:
Code:
Private Sub Button_Click(Index as interger)
Select Case Index
Case Is = 0
Msgbox "Button(0) is Clicked"
Case Is = 1
Msgbox "Button(1) is Clicked"
Case Is = 2
Msgbox "Button(2) is Clicked"
End Select
End Sub
-
Feb 5th, 2009, 01:04 AM
#4
Re: control array of my button?
To show the buttons on a form you should specify the buttons location on the form and add them to its Controls property. The code also adds click event for the buttons. Here is my approach to it:
Code:
Dim myButtons(2) As Button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For i As Integer = 0 To myButtons.Length - 1
Dim btn As New Button
' Set some properties of the button control.
btn.Name = "button" & CStr(i)
btn.Text = btn.Name
' Set the button location on the form.
If i > 0 Then
btn.Location = New Point(myButtons(i - 1).Bounds.Right + 5, 5)
Else
btn.Location = New Point(5, 5)
End If
'...
'...
myButtons(i) = btn
' Add the button to the form.
Me.Controls.Add(btn)
' Add buton click evant handler for the button so that it can call Buttons_Click sub.
AddHandler btn.Click, AddressOf Buttons_Click
Next
End Sub
Private Sub Buttons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim btn As Button = DirectCast(sender, Button)
MessageBox.Show(btn.Name & " was clicked!")
End Sub
Last edited by VBDT; Feb 5th, 2009 at 01:25 AM.
-
Feb 5th, 2009, 01:15 AM
#5
Fanatic Member
Re: control array of my button?
Creating control arrays in .NEt is a tricky job for new comers. However you can create multiple buttons in the code with different names. But add only one Event handler to all of them. The parameter "sender" can be caught for the right button which sends the event, later.
-
Feb 5th, 2009, 02:17 AM
#6
Thread Starter
Member
Re: control array of my button?
Thanks to all of u Guys..
@ VBDT: thank u so much for ur code.. i got an idea now....
-
Feb 5th, 2009, 02:57 AM
#7
Addicted Member
Re: [RESOLVED] control array of my button?
I always do them like this
vb Code:
Dim arrTB() As TextBox 'Textbox array Public Sub New() InitializeComponent() 'Textbox Array arrTB = New TextBox() {TextBox1, TextBox2} End Sub
then use it like
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|