Results 1 to 7 of 7

Thread: [RESOLVED] control array of my button?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    57

    Resolved [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..

  2. #2
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    57

    Re: control array of my button?

    Quote 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

  4. #4
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    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

  5. #5
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589

    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.

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2008
    Posts
    57

    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....

  7. #7
    Addicted Member
    Join Date
    Dec 2007
    Posts
    186

    Re: [RESOLVED] control array of my button?

    I always do them like this

    vb Code:
    1. Dim arrTB() As TextBox          'Textbox array
    2.  
    3.     Public Sub New()
    4.         InitializeComponent()
    5.  
    6.         'Textbox Array
    7.         arrTB = New TextBox() {TextBox1, TextBox2}
    8.  
    9.     End Sub

    then use it like

    vb Code:
    1. arrTB(1).Text = "Hi"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width