切换按钮内容

发布于 2024-12-08 06:14:37 字数 775 浏览 0 评论 0原文

您好,我需要帮助来切换两个按钮的内容,

到目前为止我所做的是检查按钮是否是邻居。

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);
    }

我的 XAML 文件中的按钮是这样的

<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>

,挑战是切换内容(A 和 B)

任何人都可以帮助我吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怪我闹别瞎闹 2024-12-15 06:14:37
  1. 不要使用像 Button_Click 方法这样的 WinForms 事件处理。
  2. 对按钮内容使用绑定
  3. 查看命令
  4. 查看 MVVM 方法
  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
十年不长 2024-12-15 06:14:37

只需使用一个临时变量来保存其中一个字符串即可。

string tmp = Button1.Text;
Button1.Text = Button2.Text;
Button2.Text = tmp;

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

string tmp = Button1.Text;
Button1.Text = Button2.Text;
Button2.Text = tmp;
十级心震 2024-12-15 06:14:37

我会跳过通用事件处理程序名称。您需要将要移动到的按钮存储在临时变量中。

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

潇烟暮雨 2024-12-15 06:14:37

我使用了九个按钮:

        <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>

此方法从网格中获取按钮并更改值:

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;
        }
    }

在我的示例中,指定旁边的两个按钮随之更改其值,希望这就是您的意图。

(通过 X 和 Y 从网格获取项目是从 此处)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文