Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Array Index Problem

Array Index Problem

Scheduled Pinned Locked Moved C#
questiondatabasedata-structureshelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    ytubis
    wrote on last edited by
    #1

    I have a problem! i have an Array of buttons, and all of the buttons are connected to one event, let us say OnClick Event, now when i get in to the Event function i Cust the sender to a temp button in the function but the problem is that i want or better say need the buttons indexes becaue i need to check the buttons around the selected one. How can i solve this problem? Thanks you all!:)

    T 1 Reply Last reply
    0
    • Y ytubis

      I have a problem! i have an Array of buttons, and all of the buttons are connected to one event, let us say OnClick Event, now when i get in to the Event function i Cust the sender to a temp button in the function but the problem is that i want or better say need the buttons indexes becaue i need to check the buttons around the selected one. How can i solve this problem? Thanks you all!:)

      T Offline
      T Offline
      the last free name
      wrote on last edited by
      #2

      Store the button index in the button's Tag property

      Y 1 Reply Last reply
      0
      • T the last free name

        Store the button index in the button's Tag property

        Y Offline
        Y Offline
        ytubis
        wrote on last edited by
        #3

        What do you mean? please give e.g. Thanks :)

        C 1 Reply Last reply
        0
        • Y ytubis

          What do you mean? please give e.g. Thanks :)

          C Offline
          C Offline
          Curtis Schlak
          wrote on last edited by
          #4

          What the last free name means is the following:

          Button[] buttons = new Button[ numberOfButtons ];
          for( int i = 0; i < numberOfButtons; i++ )
          {
          buttons[ i ] = new Button();
          buttons[ i ].Tag = i;
          buttons[ i ].Text = String.Format( "Button {0}", i );
          }

          And, in your button click event handler

          Button b = ( Button ) sender;
          int buttonIndex = ( int ) b.Tag;
          // Now you have the index of the button!

          "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

          Y 1 Reply Last reply
          0
          • C Curtis Schlak

            What the last free name means is the following:

            Button[] buttons = new Button[ numberOfButtons ];
            for( int i = 0; i < numberOfButtons; i++ )
            {
            buttons[ i ] = new Button();
            buttons[ i ].Tag = i;
            buttons[ i ].Text = String.Format( "Button {0}", i );
            }

            And, in your button click event handler

            Button b = ( Button ) sender;
            int buttonIndex = ( int ) b.Tag;
            // Now you have the index of the button!

            "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

            Y Offline
            Y Offline
            ytubis
            wrote on last edited by
            #5

            what about a Matrix? [,]?

            C 1 Reply Last reply
            0
            • Y ytubis

              what about a Matrix? [,]?

              C Offline
              C Offline
              Curtis Schlak
              wrote on last edited by
              #6

              Okay, I don't know the specific class Matrix to which you have referred. However, let's assume that you have a Matrix class that acts like an n x m array of objects. Then, you could have the following code:

              Matrix m = new Matrix( n, m );
              for( int i = 0; i < n; i++ )
              {
              for( int j = 0; j < m; j++ )
              {
              m[ i, j ] = new Button();
              m[ i, j ].Tag = new int[] { i, j };
              }
              }

              And you could then acess the button in your event handler like so:

              Button b = ( Button ) sender;
              int[] index = ( int[] ) b.Tag;

              That would then contain your index where the row index is in the first entry of index and the column entry in the second. "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups