Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8596937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:47:58+00:00 2026-06-12T00:47:58+00:00

I am trying to make a tic-tac-toe program, and want to know how I

  • 0

I am trying to make a tic-tac-toe program, and want to know how I figure out if any of the buttons was clicked and which button it was. I am currently repeating the code inside every button:

private void button1_Click(object sender, EventArgs e)
{
  button1.Visible = false;
  label1.Text = "X";
  Application.DoEvents();

}
private void button2_Click(object sender, EventArgs e)
{
  button2.Visible = false;
  label2.Text = "X";
  Application.DoEvents();
}

private void button3_Click(object sender, EventArgs e)
{
   button3.Visible = false;
   label3.Text = "X"
   Application.DoEvents();
}

Is there a more elegant way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:47:59+00:00Added an answer on June 12, 2026 at 12:47 am

    There’s two ways to accomplish what you’re after (assuming WinForms)..

    First, you could wire up all buttons to point to the same event handler.. and check its name:

    // In the form designer code:
    this.buttonNW.Click += new System.EventHandler(this.button_Click);
    this.buttonN.Click += new System.EventHandler(this.button_Click);
    this.buttonNE.Click += new System.EventHandler(this.button_Click);
    
    
    // in the form code:
    private void button_Click(object sender, EventArgs e) {
        Button thisButton = (Button)sender;
    
        switch (thisButton.Name) {
            case "buttonNW":
                MessageBox.Show("North West button clicked");
                break;
            case "buttonN":
                MessageBox.Show("Northern button clicked");
                break;
            case "buttonNE":
                MessageBox.Show("North East button clicked");
                break;
        }
    }
    

    Secondly, you could just put the logic into it’s own function and call that function for each individual event handler:

    private void buttonNW_Click(object sender, EventArgs e) {
        handleButtonClick("buttonNW");
    }
    
    private void buttonN_Click(object sender, EventArgs e) {
        handleButtonClick("buttonN");
    }
    
    private void buttonNE_Click(object sender, EventArgs e) {
        handleButtonClick("buttonNE");
    }
    
    private void handleButtonClick(string buttonName) {
        switch (buttonName) {
            case "buttonNW":
                MessageBox.Show("North West button clicked");
                break;
            case "buttonN":
                MessageBox.Show("Northern button clicked");
                break;
            case "buttonNE":
                MessageBox.Show("North East button clicked");
                break;
        }
    }
    

    I would personally go with option 2 so that everything is clear.

    It’s a trivial exercise though, considering you’ll only ever have a maximum of 9 buttons in a tic-tac-toe game.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a Tic Tac Toe program with java swing, and
I was trying to make Tic Tac Toe game for the monthly tutorial and
I have read a book and am trying to make a game of tic-tac-toe
I am trying to make tic tac toe using websockets running on glassfish. I've
I am trying to make a Tic Tac Toe game in Silverlight that can
Basically i'm trying to make a tic-tac-toe game using C++ and SFML2 API. I
I am trying to make a really simple Tic-Tac-Toe game. I have stored values
I'm writing a Tic Tac Toe Game and I would like to know how
I've posted here earlier about how to make a tic tac toe for two
I am fairly new to iOS development and trying make a simple app which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.