How to get the index of a clicked button in a multidimensional array?

Schmurr 1 Reputation point
2022-06-07T20:29:14.23+00:00

Trying to change the value of two variables (one for each dimension of my array) based on the index of a button in that array.

Example:
I click button array[2][6], and it should set my x variable to 2 and y to 6.

How do I get this to work? Can I?

Small BASIC
Small BASIC
A programming language created by Microsoft that serves a stepping stone for beginners from block-based coding languages to more complex text-based languages.
280 questions
{count} votes

6 answers

Sort by: Most helpful
  1. Small Visual Basic 411 Reputation points
    2022-09-17T13:10:07.417+00:00

    I updated the code to store x and y in the button.Tag property instead of parsing button name. Also, no need for an array to store button names, since we can generate the button name for a given x and y value ob the form form1.buttonX_Y, as sVB references control names to their forms. This is the new code:

       For X = 1 To 8  
          For Y = 1 To 7  
             Btn = Me.AddButton(  
                "Button" + X + "_" + Y,  
                10 + 80 * X,  
                10 + 30 * Y,  
                80,  
                30  
             )  
             Btn.Text = X + "," + Y  
             Btn.Tag = {X, Y}  
             Btn.OnClick = Btn_Click  
             Btn.Visible = False  
          EndFor  
            
          Button1 = GetButton(4, 3)  
          Button1.Visible = True  
       EndFor  
         
         
       Sub Btn_Click()  
          For x = 1 To 8  
             For y = 1 To 7  
                Button1 = GetButton(x, y)  
                Button1.Visible = False  
             Next  
          Next  
            
          Button1 = Event.SenderControl  
          xy = Button1.Tag  
          ax = xy[1]  
          ay = xy[2]  
            
          ShowButtons(  
             {  
                {ax, ay},  
                {ax + 1, ay},  
                {ax - 1, ay},  
                {ax, ay + 1},  
                {ax, ay - 1}  
             }  
          )  
       EndSub  
         
         
       Sub ShowButtons(xyArr)  
          ForEach xy In xyArr  
             Button1 = GetButton(xy[1], xy[2])  
             Button1.Visible = True  
          Next  
       EndSub  
         
         
       Function GetButton(x, y)  
          Return "form1.button" + x + "_" + y  
       EndFunction  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.