What is the best way to identify chosen button by x, y Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!What is the difference between String and string in C#?What does the [Flags] Enum Attribute mean in C#?What is the best way to iterate over a dictionary?What are the correct version numbers for C#?What is the difference between an abstract function and a virtual function?Best way to repeat a character in C#What do two question marks together mean in C#?Best way to parse command line arguments in C#?What is a NullReferenceException, and how do I fix it?Route multiple buttons to the same routine

What initially awakened the Balrog?

What order were files/directories output in dir?

"klopfte jemand" or "jemand klopfte"?

Is it possible for an event A to be independent from event B, but not the other way around?

How to write capital alpha?

Resize vertical bars (absolute-value symbols)

The Nth Gryphon Number

Is there hard evidence that the grant peer review system performs significantly better than random?

Flight departed from the gate 5 min before scheduled departure time. Refund options

What does it mean that physics no longer uses mechanical models to describe phenomena?

Why do early math courses focus on the cross sections of a cone and not on other 3D objects?

Why is std::move not [[nodiscard]] in C++20?

What would you call this weird metallic apparatus that allows you to lift people?

What is the origin of 落第?

Co-worker has annoying ringtone

As a dual citizen, my US passport will expire one day after traveling to the US. Will this work?

Did any compiler fully use 80-bit floating point?

In musical terms, what properties are varied by the human voice to produce different words / syllables?

Delete free apps from library

Monty Hall Problem-Probability Paradox

How to ask rejected full-time candidates to apply to teach individual courses?

Central Vacuuming: Is it worth it, and how does it compare to normal vacuuming?

Is there public access to the Meteor Crater in Arizona?

A proverb that is used to imply that you have unexpectedly faced a big problem



What is the best way to identify chosen button by x, y



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!What is the difference between String and string in C#?What does the [Flags] Enum Attribute mean in C#?What is the best way to iterate over a dictionary?What are the correct version numbers for C#?What is the difference between an abstract function and a virtual function?Best way to repeat a character in C#What do two question marks together mean in C#?Best way to parse command line arguments in C#?What is a NullReferenceException, and how do I fix it?Route multiple buttons to the same routine



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am making a chess game in windows form and after I made it so I once a button is pressed I will get his place on the board so I can use math on it (Like if I want to find the square exactly in front of a square I can just find the square with y +1)



void button64_Click(object sender, EventArgs e)

if (partOfTurn == false)

Xa = 8;
Ya = 8;
partOfTurn = true;


if (partOfTurn == true)

Xb = 8;
Yb = 8;
partOfTurn = false;
Click();




I need a way to change the second button's background image into the first one's and to clear the first one but in order to do so with
button1.BackgroundImage = button2.backgroundImage



I need to use the name of the button. How can I do this without doing it in a switch with 4096 cases of any button combination?










share|improve this question
























  • So, is your board 64 square buttons? If so, I'd name the buttons in a way that I could easily discern the coordinates ("x1y1" through "x8y8"). I'd make a single button click handler handle all clicks, and figure out what was happening by looking at (sender as Button)?.Name. I'd track what the last button pressed was and what the coordinate of that button was (so I could figure out if moving that knight is legal). I'd also provide a function that lets me get button coordinates (as an int pair) from a button and get the right button from a pair of coordinates.

    – Flydog57
    Mar 8 at 23:24







  • 1





    How have you created your buttons? How have you wired up the event handlers? How many buttons do you have? Why are there 4096 button combinations?

    – Enigmativity
    Mar 8 at 23:27












  • Just a hint - if you've dragged and dropped the buttons using the designer then you've made your life hard. If you use a loop to create them and save the buttons in a List<Button> then your life is easy.

    – Enigmativity
    Mar 8 at 23:33











  • Oh, and you probably want one or a few enums. For example enum Piece Rook, Knight, Bishop, King, Queen, Pawn and maybe enum Player White, Black. That way, you can write your rules in a much more natural way (for example, a LegalMoves function that returns a list of int pairs given a Piece, a Player and a current position (as an int pair).

    – Flydog57
    Mar 8 at 23:40












  • @Enigmativity: Though List<T> is almost always better than an Array of T, in this case, it might make sense to store the buttons in a two-dimensional Button array: Button[,]

    – Flydog57
    Mar 8 at 23:44

















0















I am making a chess game in windows form and after I made it so I once a button is pressed I will get his place on the board so I can use math on it (Like if I want to find the square exactly in front of a square I can just find the square with y +1)



void button64_Click(object sender, EventArgs e)

if (partOfTurn == false)

Xa = 8;
Ya = 8;
partOfTurn = true;


if (partOfTurn == true)

Xb = 8;
Yb = 8;
partOfTurn = false;
Click();




I need a way to change the second button's background image into the first one's and to clear the first one but in order to do so with
button1.BackgroundImage = button2.backgroundImage



I need to use the name of the button. How can I do this without doing it in a switch with 4096 cases of any button combination?










share|improve this question
























  • So, is your board 64 square buttons? If so, I'd name the buttons in a way that I could easily discern the coordinates ("x1y1" through "x8y8"). I'd make a single button click handler handle all clicks, and figure out what was happening by looking at (sender as Button)?.Name. I'd track what the last button pressed was and what the coordinate of that button was (so I could figure out if moving that knight is legal). I'd also provide a function that lets me get button coordinates (as an int pair) from a button and get the right button from a pair of coordinates.

    – Flydog57
    Mar 8 at 23:24







  • 1





    How have you created your buttons? How have you wired up the event handlers? How many buttons do you have? Why are there 4096 button combinations?

    – Enigmativity
    Mar 8 at 23:27












  • Just a hint - if you've dragged and dropped the buttons using the designer then you've made your life hard. If you use a loop to create them and save the buttons in a List<Button> then your life is easy.

    – Enigmativity
    Mar 8 at 23:33











  • Oh, and you probably want one or a few enums. For example enum Piece Rook, Knight, Bishop, King, Queen, Pawn and maybe enum Player White, Black. That way, you can write your rules in a much more natural way (for example, a LegalMoves function that returns a list of int pairs given a Piece, a Player and a current position (as an int pair).

    – Flydog57
    Mar 8 at 23:40












  • @Enigmativity: Though List<T> is almost always better than an Array of T, in this case, it might make sense to store the buttons in a two-dimensional Button array: Button[,]

    – Flydog57
    Mar 8 at 23:44













0












0








0








I am making a chess game in windows form and after I made it so I once a button is pressed I will get his place on the board so I can use math on it (Like if I want to find the square exactly in front of a square I can just find the square with y +1)



void button64_Click(object sender, EventArgs e)

if (partOfTurn == false)

Xa = 8;
Ya = 8;
partOfTurn = true;


if (partOfTurn == true)

Xb = 8;
Yb = 8;
partOfTurn = false;
Click();




I need a way to change the second button's background image into the first one's and to clear the first one but in order to do so with
button1.BackgroundImage = button2.backgroundImage



I need to use the name of the button. How can I do this without doing it in a switch with 4096 cases of any button combination?










share|improve this question
















I am making a chess game in windows form and after I made it so I once a button is pressed I will get his place on the board so I can use math on it (Like if I want to find the square exactly in front of a square I can just find the square with y +1)



void button64_Click(object sender, EventArgs e)

if (partOfTurn == false)

Xa = 8;
Ya = 8;
partOfTurn = true;


if (partOfTurn == true)

Xb = 8;
Yb = 8;
partOfTurn = false;
Click();




I need a way to change the second button's background image into the first one's and to clear the first one but in order to do so with
button1.BackgroundImage = button2.backgroundImage



I need to use the name of the button. How can I do this without doing it in a switch with 4096 cases of any button combination?







c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 23:19









meJustAndrew

3,22742552




3,22742552










asked Mar 8 at 23:12









Yuval AmirYuval Amir

246




246












  • So, is your board 64 square buttons? If so, I'd name the buttons in a way that I could easily discern the coordinates ("x1y1" through "x8y8"). I'd make a single button click handler handle all clicks, and figure out what was happening by looking at (sender as Button)?.Name. I'd track what the last button pressed was and what the coordinate of that button was (so I could figure out if moving that knight is legal). I'd also provide a function that lets me get button coordinates (as an int pair) from a button and get the right button from a pair of coordinates.

    – Flydog57
    Mar 8 at 23:24







  • 1





    How have you created your buttons? How have you wired up the event handlers? How many buttons do you have? Why are there 4096 button combinations?

    – Enigmativity
    Mar 8 at 23:27












  • Just a hint - if you've dragged and dropped the buttons using the designer then you've made your life hard. If you use a loop to create them and save the buttons in a List<Button> then your life is easy.

    – Enigmativity
    Mar 8 at 23:33











  • Oh, and you probably want one or a few enums. For example enum Piece Rook, Knight, Bishop, King, Queen, Pawn and maybe enum Player White, Black. That way, you can write your rules in a much more natural way (for example, a LegalMoves function that returns a list of int pairs given a Piece, a Player and a current position (as an int pair).

    – Flydog57
    Mar 8 at 23:40












  • @Enigmativity: Though List<T> is almost always better than an Array of T, in this case, it might make sense to store the buttons in a two-dimensional Button array: Button[,]

    – Flydog57
    Mar 8 at 23:44

















  • So, is your board 64 square buttons? If so, I'd name the buttons in a way that I could easily discern the coordinates ("x1y1" through "x8y8"). I'd make a single button click handler handle all clicks, and figure out what was happening by looking at (sender as Button)?.Name. I'd track what the last button pressed was and what the coordinate of that button was (so I could figure out if moving that knight is legal). I'd also provide a function that lets me get button coordinates (as an int pair) from a button and get the right button from a pair of coordinates.

    – Flydog57
    Mar 8 at 23:24







  • 1





    How have you created your buttons? How have you wired up the event handlers? How many buttons do you have? Why are there 4096 button combinations?

    – Enigmativity
    Mar 8 at 23:27












  • Just a hint - if you've dragged and dropped the buttons using the designer then you've made your life hard. If you use a loop to create them and save the buttons in a List<Button> then your life is easy.

    – Enigmativity
    Mar 8 at 23:33











  • Oh, and you probably want one or a few enums. For example enum Piece Rook, Knight, Bishop, King, Queen, Pawn and maybe enum Player White, Black. That way, you can write your rules in a much more natural way (for example, a LegalMoves function that returns a list of int pairs given a Piece, a Player and a current position (as an int pair).

    – Flydog57
    Mar 8 at 23:40












  • @Enigmativity: Though List<T> is almost always better than an Array of T, in this case, it might make sense to store the buttons in a two-dimensional Button array: Button[,]

    – Flydog57
    Mar 8 at 23:44
















So, is your board 64 square buttons? If so, I'd name the buttons in a way that I could easily discern the coordinates ("x1y1" through "x8y8"). I'd make a single button click handler handle all clicks, and figure out what was happening by looking at (sender as Button)?.Name. I'd track what the last button pressed was and what the coordinate of that button was (so I could figure out if moving that knight is legal). I'd also provide a function that lets me get button coordinates (as an int pair) from a button and get the right button from a pair of coordinates.

– Flydog57
Mar 8 at 23:24






So, is your board 64 square buttons? If so, I'd name the buttons in a way that I could easily discern the coordinates ("x1y1" through "x8y8"). I'd make a single button click handler handle all clicks, and figure out what was happening by looking at (sender as Button)?.Name. I'd track what the last button pressed was and what the coordinate of that button was (so I could figure out if moving that knight is legal). I'd also provide a function that lets me get button coordinates (as an int pair) from a button and get the right button from a pair of coordinates.

– Flydog57
Mar 8 at 23:24





1




1





How have you created your buttons? How have you wired up the event handlers? How many buttons do you have? Why are there 4096 button combinations?

– Enigmativity
Mar 8 at 23:27






How have you created your buttons? How have you wired up the event handlers? How many buttons do you have? Why are there 4096 button combinations?

– Enigmativity
Mar 8 at 23:27














Just a hint - if you've dragged and dropped the buttons using the designer then you've made your life hard. If you use a loop to create them and save the buttons in a List<Button> then your life is easy.

– Enigmativity
Mar 8 at 23:33





Just a hint - if you've dragged and dropped the buttons using the designer then you've made your life hard. If you use a loop to create them and save the buttons in a List<Button> then your life is easy.

– Enigmativity
Mar 8 at 23:33













Oh, and you probably want one or a few enums. For example enum Piece Rook, Knight, Bishop, King, Queen, Pawn and maybe enum Player White, Black. That way, you can write your rules in a much more natural way (for example, a LegalMoves function that returns a list of int pairs given a Piece, a Player and a current position (as an int pair).

– Flydog57
Mar 8 at 23:40






Oh, and you probably want one or a few enums. For example enum Piece Rook, Knight, Bishop, King, Queen, Pawn and maybe enum Player White, Black. That way, you can write your rules in a much more natural way (for example, a LegalMoves function that returns a list of int pairs given a Piece, a Player and a current position (as an int pair).

– Flydog57
Mar 8 at 23:40














@Enigmativity: Though List<T> is almost always better than an Array of T, in this case, it might make sense to store the buttons in a two-dimensional Button array: Button[,]

– Flydog57
Mar 8 at 23:44





@Enigmativity: Though List<T> is almost always better than an Array of T, in this case, it might make sense to store the buttons in a two-dimensional Button array: Button[,]

– Flydog57
Mar 8 at 23:44












2 Answers
2






active

oldest

votes


















0














Since this is for learning I'll not solve it but just rather give a hint:



The sender object you get passed as a parameter is basically the object you call the event handler on, in your case the clicked button itself.



private void buttonClicked(object sender, EventArgs e) 
Button clickedButton = (Button) sender;






share|improve this answer























  • Can you please explain more? Again, I am very new to c# and I am having trouble understanding you

    – Yuval Amir
    Mar 9 at 13:57











  • How can I use button sender and still be able to identify what button was used?

    – Yuval Amir
    Mar 12 at 14:14


















0














The Tag property of each Button is an object. You could use a Point or create your own class. I have named mine Coord but it could be Position or Square. You could also use chess notation a1, c2, although using numbers is easier to determine relations between squares.



(Coord could perhaps be extended to include the chess notation as a property, and anything else useful.)



public class Coord

public int X get; set;
public int Y get; set;

public Coord(int x, int y)

X = x;
Y = y;



public partial class Form1 : Form

private Button previousButton = null;

public Form1()

InitializeComponent();

button1.Click += Buttons_Click;
button2.Click += Buttons_Click;

button1.Tag = new Coord(0, 0);
button2.Tag = new Coord(1, 0);


private void Buttons_Click(object sender, EventArgs e)

if (previousButton != null)

// do something with previousButton
Coord prevCoords = (Coord)(previousButton.Tag);

MessageBox.Show($"previous coords prevCoords.X prevCoords.Y");


// code to work with the currently clicked button
// as (Button)sender
Coord currentCoords = (Coord)((Button)sender).Tag;
MessageBox.Show($"current coords currentCoords.X currentCoords.Y");

// remember the current button
previousButton = (Button)sender;




You would still store the buttons in a collection but I haven't included that aspect in my sample code.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072281%2fwhat-is-the-best-way-to-identify-chosen-button-by-x-y%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Since this is for learning I'll not solve it but just rather give a hint:



    The sender object you get passed as a parameter is basically the object you call the event handler on, in your case the clicked button itself.



    private void buttonClicked(object sender, EventArgs e) 
    Button clickedButton = (Button) sender;






    share|improve this answer























    • Can you please explain more? Again, I am very new to c# and I am having trouble understanding you

      – Yuval Amir
      Mar 9 at 13:57











    • How can I use button sender and still be able to identify what button was used?

      – Yuval Amir
      Mar 12 at 14:14















    0














    Since this is for learning I'll not solve it but just rather give a hint:



    The sender object you get passed as a parameter is basically the object you call the event handler on, in your case the clicked button itself.



    private void buttonClicked(object sender, EventArgs e) 
    Button clickedButton = (Button) sender;






    share|improve this answer























    • Can you please explain more? Again, I am very new to c# and I am having trouble understanding you

      – Yuval Amir
      Mar 9 at 13:57











    • How can I use button sender and still be able to identify what button was used?

      – Yuval Amir
      Mar 12 at 14:14













    0












    0








    0







    Since this is for learning I'll not solve it but just rather give a hint:



    The sender object you get passed as a parameter is basically the object you call the event handler on, in your case the clicked button itself.



    private void buttonClicked(object sender, EventArgs e) 
    Button clickedButton = (Button) sender;






    share|improve this answer













    Since this is for learning I'll not solve it but just rather give a hint:



    The sender object you get passed as a parameter is basically the object you call the event handler on, in your case the clicked button itself.



    private void buttonClicked(object sender, EventArgs e) 
    Button clickedButton = (Button) sender;







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 8 at 23:22









    main.cmain.c

    6419




    6419












    • Can you please explain more? Again, I am very new to c# and I am having trouble understanding you

      – Yuval Amir
      Mar 9 at 13:57











    • How can I use button sender and still be able to identify what button was used?

      – Yuval Amir
      Mar 12 at 14:14

















    • Can you please explain more? Again, I am very new to c# and I am having trouble understanding you

      – Yuval Amir
      Mar 9 at 13:57











    • How can I use button sender and still be able to identify what button was used?

      – Yuval Amir
      Mar 12 at 14:14
















    Can you please explain more? Again, I am very new to c# and I am having trouble understanding you

    – Yuval Amir
    Mar 9 at 13:57





    Can you please explain more? Again, I am very new to c# and I am having trouble understanding you

    – Yuval Amir
    Mar 9 at 13:57













    How can I use button sender and still be able to identify what button was used?

    – Yuval Amir
    Mar 12 at 14:14





    How can I use button sender and still be able to identify what button was used?

    – Yuval Amir
    Mar 12 at 14:14













    0














    The Tag property of each Button is an object. You could use a Point or create your own class. I have named mine Coord but it could be Position or Square. You could also use chess notation a1, c2, although using numbers is easier to determine relations between squares.



    (Coord could perhaps be extended to include the chess notation as a property, and anything else useful.)



    public class Coord

    public int X get; set;
    public int Y get; set;

    public Coord(int x, int y)

    X = x;
    Y = y;



    public partial class Form1 : Form

    private Button previousButton = null;

    public Form1()

    InitializeComponent();

    button1.Click += Buttons_Click;
    button2.Click += Buttons_Click;

    button1.Tag = new Coord(0, 0);
    button2.Tag = new Coord(1, 0);


    private void Buttons_Click(object sender, EventArgs e)

    if (previousButton != null)

    // do something with previousButton
    Coord prevCoords = (Coord)(previousButton.Tag);

    MessageBox.Show($"previous coords prevCoords.X prevCoords.Y");


    // code to work with the currently clicked button
    // as (Button)sender
    Coord currentCoords = (Coord)((Button)sender).Tag;
    MessageBox.Show($"current coords currentCoords.X currentCoords.Y");

    // remember the current button
    previousButton = (Button)sender;




    You would still store the buttons in a collection but I haven't included that aspect in my sample code.






    share|improve this answer



























      0














      The Tag property of each Button is an object. You could use a Point or create your own class. I have named mine Coord but it could be Position or Square. You could also use chess notation a1, c2, although using numbers is easier to determine relations between squares.



      (Coord could perhaps be extended to include the chess notation as a property, and anything else useful.)



      public class Coord

      public int X get; set;
      public int Y get; set;

      public Coord(int x, int y)

      X = x;
      Y = y;



      public partial class Form1 : Form

      private Button previousButton = null;

      public Form1()

      InitializeComponent();

      button1.Click += Buttons_Click;
      button2.Click += Buttons_Click;

      button1.Tag = new Coord(0, 0);
      button2.Tag = new Coord(1, 0);


      private void Buttons_Click(object sender, EventArgs e)

      if (previousButton != null)

      // do something with previousButton
      Coord prevCoords = (Coord)(previousButton.Tag);

      MessageBox.Show($"previous coords prevCoords.X prevCoords.Y");


      // code to work with the currently clicked button
      // as (Button)sender
      Coord currentCoords = (Coord)((Button)sender).Tag;
      MessageBox.Show($"current coords currentCoords.X currentCoords.Y");

      // remember the current button
      previousButton = (Button)sender;




      You would still store the buttons in a collection but I haven't included that aspect in my sample code.






      share|improve this answer

























        0












        0








        0







        The Tag property of each Button is an object. You could use a Point or create your own class. I have named mine Coord but it could be Position or Square. You could also use chess notation a1, c2, although using numbers is easier to determine relations between squares.



        (Coord could perhaps be extended to include the chess notation as a property, and anything else useful.)



        public class Coord

        public int X get; set;
        public int Y get; set;

        public Coord(int x, int y)

        X = x;
        Y = y;



        public partial class Form1 : Form

        private Button previousButton = null;

        public Form1()

        InitializeComponent();

        button1.Click += Buttons_Click;
        button2.Click += Buttons_Click;

        button1.Tag = new Coord(0, 0);
        button2.Tag = new Coord(1, 0);


        private void Buttons_Click(object sender, EventArgs e)

        if (previousButton != null)

        // do something with previousButton
        Coord prevCoords = (Coord)(previousButton.Tag);

        MessageBox.Show($"previous coords prevCoords.X prevCoords.Y");


        // code to work with the currently clicked button
        // as (Button)sender
        Coord currentCoords = (Coord)((Button)sender).Tag;
        MessageBox.Show($"current coords currentCoords.X currentCoords.Y");

        // remember the current button
        previousButton = (Button)sender;




        You would still store the buttons in a collection but I haven't included that aspect in my sample code.






        share|improve this answer













        The Tag property of each Button is an object. You could use a Point or create your own class. I have named mine Coord but it could be Position or Square. You could also use chess notation a1, c2, although using numbers is easier to determine relations between squares.



        (Coord could perhaps be extended to include the chess notation as a property, and anything else useful.)



        public class Coord

        public int X get; set;
        public int Y get; set;

        public Coord(int x, int y)

        X = x;
        Y = y;



        public partial class Form1 : Form

        private Button previousButton = null;

        public Form1()

        InitializeComponent();

        button1.Click += Buttons_Click;
        button2.Click += Buttons_Click;

        button1.Tag = new Coord(0, 0);
        button2.Tag = new Coord(1, 0);


        private void Buttons_Click(object sender, EventArgs e)

        if (previousButton != null)

        // do something with previousButton
        Coord prevCoords = (Coord)(previousButton.Tag);

        MessageBox.Show($"previous coords prevCoords.X prevCoords.Y");


        // code to work with the currently clicked button
        // as (Button)sender
        Coord currentCoords = (Coord)((Button)sender).Tag;
        MessageBox.Show($"current coords currentCoords.X currentCoords.Y");

        // remember the current button
        previousButton = (Button)sender;




        You would still store the buttons in a collection but I haven't included that aspect in my sample code.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 13 at 10:38









        Andy GAndy G

        17.3k53757




        17.3k53757



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072281%2fwhat-is-the-best-way-to-identify-chosen-button-by-x-y%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            1928 у кіно

            Захаров Федір Захарович

            Ель Греко