We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,503 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

how i can come to know which button is selected (or how i can pass i,j as an argument

hi guys please help me


i want to know how i can pass an i ,j argumaents to the function abc so that i can do oprations on i, j in function abc

Button[,] b = new Button[8, 8];

        void drawchessboard()
        {
            int x=60, y=10,z=0;
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    b[i, j] = new Button();
                    b[i, j].Name = "b" + Convert.ToString(i) + Convert.ToString(j);
                    b[i, j].Text = " ";
                    b[i, j].Width = 80;
                    b[i, j].Height = 80;
                    b[i, j].FlatStyle = FlatStyle.Flat;
                    b[i, j].Location = new Point(x,y);
                    if (z == 0)
                    {   b[i, j].BackColor = Color.White;
                        z = 1;  }
                    else
                    {   b[i, j].BackColor = Color.DarkGray;
                        z = 0;  }
                    
                    b[i, j].Click += new EventHandler(abc);
                    Button button = new Button();
                    this.Controls.Add(b[i,j]);
                    x = x + 81;
                }
                //MessageBox.Show(""+n);
                y = y + 81;
                x = 60;
                if(z==1) 
                { z = 0;}
                else
                { z = 1; }
            }
        }
        void drawpieces()
        {
            for (int j = 0; j < 8; j++)
            {   
                b[1, j].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory+"/black_pawn.png"); 
                b[6, j].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory+"/white_pawn.png");
            }
            b[0, 0].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/black_rook.png");
            b[0, 7].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/black_rook.png");
            b[0, 1].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/black_knight.png");
            b[0, 6].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/black_knight.png");
            b[0, 2].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/black_bishop.png");
            b[0, 5].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/black_bishop.png");
            b[0, 3].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/black_queen.png");
            b[0, 4].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/black_king.png");
            b[7, 0].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/white_rook.png");
            b[7, 7].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/white_rook.png");
            b[7, 1].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/white_knight.png");
            b[7, 6].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/white_knight.png");
            b[7, 2].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/white_bishop.png");
            b[7, 5].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/white_bishop.png");
            b[7, 3].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/white_queen.png");
            b[7, 4].Image = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/white_king.png");

           
        }

        void abc(object sender, EventArgs e)
        {
            MessageBox.Show("gfd");
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            drawchessboard();
            drawpieces();
        }
4
Contributors
6
Replies
3 Weeks
Discussion Span
1 Year Ago
Last Updated
7
Views
sat21091
Newbie Poster
8 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I see that your function abc locks like a event , you add a event to a control this way
<control name>.event += new new EventHandler(<event function>).
In your case it would be:

b[i,j].Click += new EventHandler(abc);
predator5047
Newbie Poster
14 posts since Dec 2011
Reputation Points: 10
Solved Threads: 5
Skill Endorsements: 0

hello sir

actually i m trying to make a chess engine

thanks for help but it didnt work out for me bcoz i just want to know which button is selected from 8x8 grid which is possible by knowing the values of i, j in grid and after that i can apply oprations on i,j for further actions


so i want to know how i will get a value of i,j in function abc.
please read the code carefully.

thanks in advance

sat21091
Newbie Poster
8 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Your abc click handler would loo something like

private void abc_Click(object sender, EventArgs e)
        {
//code here
}

Now sender.Name will give you something like "bij"(so extract the i and j from there), you could also use the Location property in this case or the Tag property, to do what you want to do. Success! :)

ddanbe
Industrious Poster
4,308 posts since Oct 2008
Reputation Points: 2,126
Solved Threads: 725
Skill Endorsements: 26

You could use the .Tag property of each button to give it a coordinate. Then subscribe all the buttons' events to one event, and use this coordinate to identify the button.

in drawchessboard:

b[i,j].Tag = new Point(i, j);
private void abc(object sender, EventArgs e)
{
   Point ButtonCoordinate = (Point)(((Button)sender).Tag);
}

A bit ugly parenthesis wise, but that's just to get it all on one line. It could be written like this:

private void abc(object sender, EventArgs e)
{
   Button CallingButton = (Button)sender;
   Point ButtonCoordinate = (Point)(CallingButton.Tag);
}

A side note, it looks like line 25 is totally useless and can be removed.

skatamatic
Posting Shark
986 posts since Nov 2007
Reputation Points: 399
Solved Threads: 132
Skill Endorsements: 1

hi sir thanks for help

i wanna know how i can return the name of image of the button

sat21091
Newbie Poster
8 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

hi sir thanks for help

i wanna know how i can return the name of image of the button

The .Image property doesn't contain a name, but it does have a .Tag property. You can use this property when assigning the image to the button to hold a string, which you can then use to get this data, just like you did with the coordinates on the button itself. You could also keep an internal list of images with an assosiated name (a custom image class of sorts). When you assign the image to the buttons, use a reference to the image in this list rather than a 'new' image. Then you can search this list for an identical reference to the .Image property of the button, and get the associated name that way. The first method is propably easier though...

skatamatic
Posting Shark
986 posts since Nov 2007
Reputation Points: 399
Solved Threads: 132
Skill Endorsements: 1

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0829 seconds using 2.75MB