0

Hello I am in need of help to switch the content of two buttons

what I have done so far is to check if the buttons are neighbours.

private int row = 4;
private int col = 4;

public MainWindow()
{

}

private void Button_Click(object sender, RoutedEventArgs e)
{
    Button cmd = (Button)sender;
    MessageBox.Show(cmd.Tag.ToString());
    string txt = cmd.Tag.ToString();
    int r = int.Parse("" + txt[0]);
    int c = int.Parse("" + txt[1]);
    if (Math.Abs(r - row) + Math.Abs(c - col) == 1)
    {
        MessageBox.Show(r + "  " + c);
    }

And my buttons in my XAML file is like this

<Button Tag="00" Grid.Row="0" Grid.Column="0" Click="Button_Click">A</Button>
<Button Tag="01" Grid.Row="0" Grid.Column="1" Click="Button_Click">B</Button>

and the Challenge is to switch the content (A and B)

Can anyone help me with that?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Dallerius
  • 1
  • 1
  • 1
    Somewhere a little WPF kitten died. Please check out Karel's answer. Switching the Button Models should then be a breeze. – flq Oct 05 '11 at 10:03

4 Answers4

2
  1. do not use WinForms event handling like Button_Click method.
  2. use binding for button content
  3. look at the commands
  4. look at the MVVM approach
Karel Frajták
  • 4,389
  • 1
  • 23
  • 34
0

Just use a temporary variable to hold one of the strings.

string tmp = Button1.Text;
Button1.Text = Button2.Text;
Button2.Text = tmp;
Polynomial
  • 27,674
  • 12
  • 80
  • 107
0

I'd skip the generic eventhandler name. You need to store the button you want to move to in a temporary variable.

Kammersgaard
  • 74
  • 1
  • 2
0

I used nine Buttons:

        <Button Tag="00" Grid.Row="0" Grid.Column="0" Margin="15,21,13,23" Name="btn1" Click="btn1_Click">A</Button>
        <Button Tag="01" Grid.Row="0" Grid.Column="1" Margin="15,21,13,23" Name="btn2" Click="btn1_Click">B</Button>
        <Button Tag="02" Grid.Row="0" Grid.Column="2" Margin="15,21,13,23" Name="btn3" Click="btn1_Click">C</Button>
        <Button Tag="10" Grid.Row="1" Grid.Column="0" Margin="15,21,13,23" Name="btn4" Click="btn1_Click">D</Button>
        <Button Tag="11" Grid.Row="1" Grid.Column="1" Margin="15,21,13,23" Name="btn5" Click="btn1_Click">E</Button>
        <Button Tag="12" Grid.Row="1" Grid.Column="2" Margin="15,21,13,23" Name="btn6" Click="btn1_Click">F</Button>
        <Button Tag="20" Grid.Row="2" Grid.Column="0" Margin="15,21,13,23" Name="btn7" Click="btn1_Click">G</Button>
        <Button Tag="21" Grid.Row="2" Grid.Column="1" Margin="15,21,13,23" Name="btn8" Click="btn1_Click">H</Button>
        <Button Tag="22" Grid.Row="2" Grid.Column="2" Margin="15,21,13,23" Name="btn9" Click="btn1_Click">I</Button>

and This Method to get the Button from the Grid and Change Values:

private int row = 2;
    private int col = 2;

    private void btn1_Click(object sender, RoutedEventArgs args)
    {
        Button cmd = (Button)sender;
        string txt = cmd.Tag.ToString();
        int r = int.Parse("" + txt[0]);
        int c = int.Parse("" + txt[1]);
        if (Math.Abs(r - row) + Math.Abs(c - col) == 1)
        {
            MessageBox.Show(r + "  " + c);
            Button nearButton = grd1.Children.Cast<Button>().First(e => Grid.GetRow(e) == row && Grid.GetColumn(e) == col);
            Object tmp = nearButton.Content;
            nearButton.Content = cmd.Content;
            cmd.Content = tmp;
        }
    }

In my Example the two Buttons next to the specified change their value with it, hope that is what you intended.

(Getting an Item from a Grid via the X and Y is stolen from here)

Community
  • 1
  • 1
Salazaar
  • 384
  • 1
  • 9