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..
Printable View
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..
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..Quote:
Originally Posted by VBDT
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
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
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.
Thanks to all of u Guys..
@ VBDT: thank u so much for ur code.. i got an idea now....
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
vb Code:
arrTB(1).Text = "Hi"