Click here to Skip to main content
15,884,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am making a client/server application that contains a Tic Tac Toe game where two connected clients can play the game. The 3 x 3 grid is made up of 9 dynamically created buttons. When the first client clicks a button on the grid, the button is disabled and the content shows a 'X'. A value is sent to the server and then to the other connected client. Based on the value received by the client, I want the same button to be disabled and the content changed to 'X'.

The problem I have is finding the button that was dynamically created on the client side. Any help appreciated!


C#
//Dynamically created 9 buttons on the client
 private void initBoard(int rank)
    {

        board = new tttBoard(rank);
        boardGrid.Children.Clear();
        boardGrid.Rows = rank;
        for (int i = 0; i < rank; i++)
        {
            for (int j = 0; j < rank; j++)
            {
                newButton = new Button();
                newButton.Tag = new Point(i, j);
                newButton.Name = "b" + i.ToString() + j.ToString();
                newButton.Content = newButton.Tag;
                boardGrid.Children.Add(newButton);
            }
        }
    }

//Method that receives data - CheckButton called method within this
public void OnDataReceived(IAsyncResult ar)
    {
        try
        {
            SocketPacket sckID = (SocketPacket)ar.AsyncState;
            int iRx = sckID.thisSocket.EndReceive(ar);
            char[] chars = new char[iRx];
            Decoder d = Encoding.UTF8.GetDecoder();
            int charLen = d.GetChars(sckID.dataBuffer, 0, iRx, chars, 0);
            szData = new String(chars);
            this.Dispatcher.Invoke((Action)(() =>
            {
                if(szData.Contains("Clicked button : "))
                {
                    return;
                }
                else
                    lbxMessages.Items.Add(txtMessage.Text + szData);
            }));

            ClickButton();

            WaitForData();
        }
        catch (ObjectDisposedException)
        {
            Debugger.Log(0, "1", "\n OnDataRecieved: Socket has been closed\n");
        }
        catch(SocketException se)
        {
            MessageBox.Show(se.Message);
        }
    }

//based on the message received from the server, I check to see if 
//it contains "Clicked button: " and a value that I use to locate the correct 
//button to disable and change content to 'x' to represent the move made by the 
//other client

public void ClickButton()
    {
        if (szData.Contains("Clicked button : "))
        {
            value = szData.Substring(17, 1);
        }
        this.Dispatcher.Invoke((Action)(() =>
        {
            btnName = "b0" + value;
            object item = grdClient.FindName(btnName);//boardGrid.FindName(btnName);
            if (item is Button)
            {
                Button btn = (Button)item;
                btn.IsEnabled = false;
            }
        }));
    }
Posted
Updated 12-Dec-12 8:44am
v2

Your best bet is to store it somewhere when you create it, so you can look there.
 
Share this answer
 
Hello,
In vb.net i'm using
VB
Private WithEvents Tray As NotifyIcon

To dynamically create a Notifyicon.
To catch the events i use:
VB
Private Sub Tray_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tray.DoubleClick
    frmStart.Show()
End Sub

So i tried translating it into C#, this is what a got
private NotifyIcon withEventsField_Tray;
private NotifyIcon Tray {
	get { return withEventsField_Tray; }
	set {
		if (withEventsField_Tray != null) {
			withEventsField_Tray.DoubleClick -= Tray_DoubleClick;
		}
		withEventsField_Tray = value;
		if (withEventsField_Tray != null) {
			withEventsField_Tray.DoubleClick += Tray_DoubleClick;
		}
	}
}
private void Tray_DoubleClick(object sender, System.EventArgs e)
{
	frmStart.Show();
}


So here i used the Doubleclick event wich is possible for a button too,
i hope you got a bit wiser?

Bert
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900