Array of Controls (Different Question)

I know that someone has already asked about an array of controls, but my question is somewhat different. I would like to know if it is possible to create an array of about 20 buttons on the screen, so that when an event takes place, the event listener just goes through the array to find out which button it came from (using IFs) and do the appropriate action. It is so cumbersome to keep repeating this over and over again without an array. Since the only difference in each button's action would be the number that it manipulates, it is wasteful to create a whole event listener for every button. In Java, all you have to do is create an array of buttons, then run loops to get them onscreen, set text, etc. If I wanted each button to have a value, I either made a corrosponding array of integers or doubles, and in the loops, it would automatically use the correct number for the button pressed (or I would create a class that included the button and its corrosponding nubmer, then put that in an array).

Comments

  • seancampbellseancampbell Pennsylvania, USA
    Well. In VB 6.0 (as you may already know) there is a way to do that. But when they made Vb.Net they changed the way the objects are and took out that handy little feature. There may be a few different ways to solve this problem that I don't know, but I think I have a way for you to do it.

    If you create buttons with the same name like "Frank1", "Frank2", "Frank3" something that no other button will have in their name also... you can write a script to go through all the buttons on the form and Add Event Handler SubName() to them. I think you can parse the name for the object while inside of that subroutine so you can know what number the button is by the Object name.

    It's not the nicest, and I don't have .Net on this computer, but when I am settled into work tomorrow afternoon (I have to travel to IL :x tomorrow) I will post some code to do it, and you can see what I am talking about.

    -Sean C


    If you want to try writing it on your own:

    Write code for a button, then remove the last stuff (handles part) on the Subroutine line it will say:
    [code]
    Private Sub Button1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    [/code]
    Ok. I think sender is the button object so you can say Sender.Name or something to get the object name
    On your form load you would want something like this:

    (I dont have vs.net on this puter so I am writing this without checking it. you may need to modify this to work/fit your project as I am not sure that the code I am writing is 100% correct)

    Note: I use naming structure for my objects so I can use the Name string to figure out what kind of object it is. That way I can say If SubString(Obj.Name, "pnl") = -1 Then 'Not a panel.

    [code]
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Do your loading code here

    'Now we need to add an event Handler
    'to each button w/ btnDrtyHrry in their name
    For each Ctl in Me
    If SubString(Ctl.Name, "btnDrtyHrry") <> -1 Then
    Add Event Handler btnDrtyHrry, ButtonCode
    End If
    Next

    End Sub

    Sub ButtonCode(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim Name as string = sender.Name
    Dim CtlNum as Integer = CInt(Name.Split("btnDrtyHrry")(1))
    End Sub
    [/code]
  • seancampbellseancampbell Pennsylvania, USA
    As I suspected my code was wrong

    [code]
    AddHandler Button1, AddressOf ButtonCode
    [/code]

    Substitute that in whereever I say "Add Event Handler"
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion