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.
277 questions
{count} votes

6 answers

Sort by: Most helpful
  1. WhTurner 1,611 Reputation points
    2022-06-08T13:29:09.703+00:00

    See this program:

    '' BoxClick    220608 WhT  
    GraphicsWindow.FontSize=15  
    GraphicsWindow.BrushColor="Black"  
    GraphicsWindow.DrawText(450,xt,x+" - "+y)  
    For i=20 To 420 Step 40  
      GraphicsWindow.DrawLine(20,i,420,i)  
      GraphicsWindow.DrawLine(i,20,i,420)  
    EndFor  
    For i=1 To 9  
      GraphicsWindow.DrawText(40*i,1,i)  
    EndFor  
    GraphicsWindow.DrawText(40*i-8,1,10)  
    For i=1 To 10  
      GraphicsWindow.DrawText(425,40*i-10,i)  
    EndFor  
    GraphicsWindow.DrawText(450,20,"Click in boxes")  
    GraphicsWindow.DrawText(470,45,"X")  
    GraphicsWindow.DrawText(510,45,"Y")  
    xt=50  
    GraphicsWindow.MouseDown=MD  
      
    Sub MD  
      x=Math.Ceiling((GraphicsWindow.MouseX-20)/40)  
      y=Math.Ceiling((GraphicsWindow.MouseY-20)/40)  
      If x>0 and x<11 and y>0 and y<11 Then  
        xt=xt+20  
        GraphicsWindow.DrawText(470,xt,x)  
        GraphicsWindow.DrawText(510,xt,y)  
      EndIf  
    EndSub  
    
    1 person found this answer helpful.
    0 comments No comments

  2. WhTurner 1,611 Reputation points
    2022-06-09T12:35:08.17+00:00

    To show that the solution of JR-5192 works:
    put the following lines at the end

     '' this is where I need to change ax or ay depending on which button is clicked  
      Controls.SetButtonCaption(controls.lastclickedbutton ,"     HIT    ")  
      Controls.SetButtonCaption(array[ax+1][ay],"     hit      ")  
      Controls.SetButtonCaption(array[ax-1][ay],"     hit      ")  
      Controls.SetButtonCaption(array[ax][ay+1],"     hit      ")  
      Controls.SetButtonCaption(array[ax][ay-1],"     hit      ")  
    
    1 person found this answer helpful.

  3. WhTurner 1,611 Reputation points
    2022-06-08T11:17:15.917+00:00

    How did you define the buttons? Can you publish the part of the program where you try to do it?


  4. Small Visual Basic 411 Reputation points
    2022-09-17T11:43:43.64+00:00

    You can do it in (Small Visual Basic)[https://github.com/VBAndCs/sVB-Small-Visual-Basic/releases/tag/v1.8] like this:

       Btns = {}  
         
       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.OnClick = Btn_Click  
             Btns[X][Y] = Btn  
             Btn.Visible = False  
          EndFor  
            
          Button1 = Btns[4][3]  
          Button1.Visible = True  
       EndFor  
         
         
       Sub Btn_Click()  
          ForEach row In Btns  
             ForEach button1 In row  
                button1.Visible = False  
             Next  
          Next  
            
          button1 = Event.SenderControl  
          xy = Text.GetSubTextToEnd(button1, 7)  
          i = xy.IndexOf("_")  
          ax = xy.SubText(1, i - 1)  
          ay = xy.SubTextToEnd(i + 1)  
            
          ShowButton(ax, ay)  
          ShowButton(ax + 1, ay)  
          ShowButton(ax - 1, ay)  
          ShowButton(ax, ay + 1)  
          ShowButton(ax, ay - 1)  
       EndSub  
         
         
       Sub ShowButton(ax, ay)  
          Button1 = Btns[ax][ay]  
          Button1.Visible = True  
       EndSub  
    

    I published the sample here:
    https://github.com/VBAndCs/sVB-Small-Visual-Basic/tree/master/Samples/Show%20Buttons

    0 comments No comments

  5. Small Visual Basic 411 Reputation points
    2022-09-17T11:51:46.36+00:00

    you may also change this part

       ShowButton(ax, ay)  
          ShowButton(ax + 1, ay)  
          ShowButton(ax - 1, ay)  
          ShowButton(ax, ay + 1)  
          ShowButton(ax, ay - 1)  
    

    to:

       ShowButtons(  
             {  
                {ax, ay},  
                {ax + 1, ay},  
                {ax - 1, ay},  
                {ax, ay + 1},  
                {ax, ay - 1}  
             }  
          )  
    

    with ShowButtons sub defined as this:

       Sub ShowButtons(xyArr)  
          ForEach xy In xyArr  
             Button1 = Btns[xy[1]][xy[2]]  
             Button1.Visible = True  
          Next  
       EndSub  
    
    0 comments No comments