Search code examples
c#buttonwindows-phone

How to check and change name button in C# Windows Phone


I have a problem, I'm making an easy game noughts and crosses on Windows Phone with Buttons:

<Button x:Name="ba0" Content=" " HorizontalAlignment="Left" Height="126" Margin="37,146,0,0" VerticalAlignment="Top" Width="126" Click="a0"/>

I would like to change the content of the button if on the button there's " " and not to change if there's "X" or "O". I wrote something like that:

public void a0(object sender, System.EventArgs e)
{
    if (ba0.Content == " ")
        ba0.Content = "X";
    else
         return;
}

But it doesn't work properly. Any idea?


Solution

  • Try the following

    Convert the content into string type and also trim the value.

    public void a0(object sender, System.EventArgs e)
            {
                if (string.IsNullOrEmpty(Convert.ToString(ba0.Content).Trim()))
                    ba0.Content = "X";
                else
                    return;
            }
    

    or check for white spaces

    public void a0(object sender, System.EventArgs e)
        {
            var content = Convert.ToString(ba0.Content);
            if (string.IsNullOrEmpty(content) || string.IsNullOrWhiteSpace(content))
                ba0.Content = "X";
            else
                return;
        }