切换按钮内容
您好,我需要帮助来切换两个按钮的内容,
到目前为止我所做的是检查按钮是否是邻居。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Button_Click
方法这样的 WinForms 事件处理。Button_Click
method.只需使用一个临时变量来保存其中一个字符串即可。
Just use a temporary variable to hold one of the strings.
我会跳过通用事件处理程序名称。您需要将要移动到的按钮存储在临时变量中。
I'd skip the generic eventhandler name. You need to store the button you want to move to in a temporary variable.
我使用了九个按钮:
此方法从网格中获取按钮并更改值:
在我的示例中,指定旁边的两个按钮随之更改其值,希望这就是您的意图。
(通过 X 和 Y 从网格获取项目是从 此处)
I used nine Buttons:
and This Method to get the Button from the Grid and Change Values:
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)