gpt4 book ai didi

c# - 如何显示来自另一种形式的数据

转载 作者:行者123 更新时间:2023-11-30 18:08:52 26 4
gpt4 key购买 nike

这基本上是一个井字游戏,我有另一个名为 Winner.cs 的表单,当玩家获胜时我希望它调用该表单(这部分有效)然后我希望它说 xWinner.label =b1。 text""+ 赢得了比赛!。我无法开始工作的部分是在获胜者表单标签中显示文本。有一个消息框的例子,注释掉供引用而不是 b1.text

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MyGame
{
public class Result1
{


static private int[,] Winners = new int[,]
{
{0,1,2},
{3,4,5},
{6,7,8},
{0,3,6},
{1,4,7},
{2,5,8},
{0,4,8},
{2,4,6},
};
static public bool CheckWinner(Button[] myControls)
{
bool gameOver = false;
for (int i = 0; i < 8; i++)
{
int a = Winners[i, 0], b = Winners[i, 1], c = Winners[i, 2];
Button b1 = myControls[a], b2 = myControls[b], b3 = myControls[c];
if (b1.Text == "" || b2.Text == "" || b3.Text == "")
continue;
if (b1.Text == b2.Text && b2.Text == b3.Text)
{
b1.BackColor = b2.BackColor = b3.BackColor = System.Drawing.Color.LightCoral;
b1.Font = b2.Font = b3.Font = new System.Drawing.Font("Microsoft Sans Serif", 32F, System.Drawing.FontStyle.Italic & System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
gameOver = true;
Form xWinnerForm = new xWinnerForm();
xWinnerForm.Show();

//MessageBox.Show(b1.Text + " .... Wins the game!", "Game End", MessageBoxButtons.OK);
//break;
}
}
return gameOver;
}
}
}

最佳答案

您可以做的一件事是在您的 cWinnerForm 类中创建您自己的 Show 方法:

public void Show(string text)
{
this.myLabel.Text = text;
this.Show();
}

那么您将不得不更改代码块中的两行代码:

来自这里:

Form xWinnerForm = new xWinnerForm();
xWinnerForm.Show();

为此:

xWinnerForm xWinnerForm = new xWinnerForm();
xWinnerForm.Show(b1.Text);

另一种选择是将文本传递到 xWinnerForm 的构造函数中。

关于c# - 如何显示来自另一种形式的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2888901/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com