Results 1 to 11 of 11

Thread: [RESOLVED] Button Arrays - How

  1. #1

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Resolved [RESOLVED] Button Arrays - How

    I know in VB.Net there isn't an Index property on the Button control like it does in VB6. But I am trying to replicate a control array in VB.Net. I have created an array of buttons:

    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, _
    2.                    ByVal e As System.EventArgs) Handles MyBase.Load
    3.         Dim but(10) As Button
    4.         Dim i As Integer
    5.         Dim lngTop As Long = 10
    6.  
    7.  
    8.         For i = 1 To 6
    9.             but(i) = New Button()
    10.             With but(i)
    11.                 Me.Controls.Add(but(i))
    12.                 .BackColor = Color.AliceBlue
    13.                 .FlatStyle = FlatStyle.Standard
    14.                 .Text = "But(" & i & ")"
    15.                 .Name = "Button"
    16.                 .Left = 10
    17.                 .Top = lngTop
    18.                 .Tag = "UP"
    19.                 lngTop = (lngTop + but(i).Height) - 1.5
    20.                 .Visible = True
    21.                 AddHandler but(i).Click, AddressOf ButtonClick
    22.             End With
    23.         Next i
    24.     End Sub

    But my question how do I pass the Button Array index to the following Sub:

    VB Code:
    1. Private Sub ButtonClick(ByVal sender As Object, ByVal e As EventArgs)
    2.  
    3.         'Do something with Button
    4.  
    5.     End Sub

    Thanks,
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: Button Arrays - How

    Since I see you are already using the Tag property for something else, you may want to make your own Button class that inherits from Button, and just add an Index property.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: Button Arrays - How

    VB Code:
    1. Private Sub ButtonClick(ByVal sender As Object, ByVal e As EventArgs)
    2.  
    3.         'sender is a reference to the control that fires the event, it must be cast however.
    4.         Dim b As Button = DirectCast(sender, Button)
    5.         'b is the button that was clicked
    6.         Debug.WriteLine(b.Text)
    7.        
    8.  
    9.     End Sub
    "so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman

  4. #4

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Button Arrays - How

    Thank you for the posts but I guess what I meant to say is that how would I access another button's properties other than the Sender Object (ie. User Clicks Button(2) and then I want to move Button(4)'s position)


    Quote Originally Posted by crptcblade
    Since I see you are already using the Tag property for something else, you may want to make your own Button class that inherits from Button, and just add an Index property.
    Can you point me to any good examples of this?

    Thanks
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  5. #5
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: Button Arrays - How

    make a class level var of Dim but(10) As Button so it can be referenced from your eventhandler.
    "so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman

  6. #6

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Button Arrays - How

    Quote Originally Posted by grilkip
    make a class level var of Dim but(10) As Button so it can be referenced from your eventhandler.

    Thanks, I can't believe I couldn't figure that out myself. Now I am getting a little worried, VB.Net is starting to make sense to me now.


    Thanks for the posts!!
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  7. #7
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: Button Arrays - How

    make a custom class for your buttons
    VB Code:
    1. Public Class ButtonI
    2.  Inherits Forms.Button
    3.  Public Index as Integer
    4. End Class

    or, there is actually a buttonarray object you can add to your project, never used it though

    EDIT NOTE: Oh? you didnt need the index to do + 1 or whatever

  8. #8
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: Button Arrays - How

    As a side note: you should use the keyword Private rather than Dim with Class level vars.
    "so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman

  9. #9

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Button Arrays - How

    Quote Originally Posted by Phill64
    make a custom class for your buttons
    VB Code:
    1. Public Class ButtonI
    2.  Inherits Forms.Button
    3.  Public Index as Integer
    4. End Class
    . . .
    Thanks for you code, I added the class to the Form Class:

    VB Code:
    1. Public Class Form1
    2.     Public Class ButtonI
    3.         Inherits System.Windows.Forms.Button
    4.         Public Index As Integer
    5.     End Class
    6.  
    7.     Private Sub Form1_Load(ByVal sender As System.Object, _
    8.                       ByVal e As System.EventArgs) Handles MyBase.Load
    9.  
    10.   End Sub
    11.  
    12. End Class

    But I am not able to access the index variable.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  10. #10
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: Button Arrays - How

    you will have to declare your buttons as "ButtonI" instead of "Button", as thats the type which has the Index value

  11. #11

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Button Arrays - How

    Quote Originally Posted by Phill64
    you will have to declare your buttons as "ButtonI" instead of "Button", as thats the type which has the Index value

    Thanks

    Here is a copy of the Final code if anyone was interested:

    VB Code:
    1. Public Class Form1
    2.     Public Class ButtonI
    3.         Inherits System.Windows.Forms.Button
    4.         Public Index As Integer
    5.     End Class
    6.  
    7.     Private but(10) As ButtonI
    8.  
    9.     Private Sub Form1_Load(ByVal sender As System.Object, _
    10.                   ByVal e As System.EventArgs) Handles MyBase.Load
    11.  
    12.         Dim i As Integer
    13.         Dim lngTop As Long = 10
    14.  
    15.         For i = 1 To 6
    16.             but(i) = New ButtonI
    17.             With but(i)
    18.                 Me.Controls.Add(but(i))
    19.                 .FlatStyle = FlatStyle.Standard
    20.                 .Index = i
    21.                 .Text = "Button " & i
    22.                 .Name = "Button"
    23.                 .Left = 10
    24.                 .Top = lngTop
    25.                 lngTop = (lngTop + but(i).Height) - 1.5
    26.                 .Visible = True
    27.                 AddHandler but(i).Click, AddressOf ButtonClick
    28.             End With
    29.         Next i
    30.     End Sub
    31.  
    32.     Private Sub ButtonClick(ByVal sender As Object, ByVal e As EventArgs)
    33.         Dim intIndex As Integer
    34.                 Debug.Print(CType(sender, ButtonI).Text)
    35.                 Debug.Print(CType(sender, Button).Name)
    36.                 Debug.Print(CType(sender, ButtonI).Index)
    37.     End Sub
    38. End Class
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


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