Click here to Skip to main content
12,395,143 members (69,448 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# WPF buttons Sockets
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!


//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 12-Dec-12 8:39am
Updated 12-Dec-12 8:44am
RyanDev213.8K
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Your best bet is to store it somewhere when you create it, so you can look there.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hello,
In vb.net i'm using
Private WithEvents Tray As NotifyIcon
To dynamically create a Notifyicon.
To catch the events i use:
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
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 12 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100