C# 如何显示来自其他窗体的数据

C# 如何显示来自其他窗体的数据,c#,winforms,C#,Winforms,这基本上是一个tic-tac-toe游戏,我有另一个表单叫做Winner.cs当一个玩家赢了,我希望它调用表单(这部分工作),然后我希望它说xWinner.label=b1.text”“+赢得了游戏!。我无法工作的部分是在winners表单标签中显示文本。这里有一个消息框的示例,注释掉以供参考 而不是b1.text using System; using System.Collections.Generic; using System.Linq; using System.Text; using

这基本上是一个tic-tac-toe游戏,我有另一个表单叫做Winner.cs当一个玩家赢了,我希望它调用表单(这部分工作),然后我希望它说xWinner.label=b1.text”“+赢得了游戏!。我无法工作的部分是在winners表单标签中显示文本。这里有一个消息框的示例,注释掉以供参考 而不是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;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
名称空间MyGame
{
公开课成绩1
{
静态私有整数[,]Winners=新整数[,]
{
{0,1,2},
{3,4,5},
{6,7,8},
{0,3,6},
{1,4,7},
{2,5,8},
{0,4,8},
{2,4,6},
};
静态公共bool CheckWinner(按钮[]myControls)
{
bool gameOver=false;
对于(int i=0;i<8;i++)
{
INTA=优胜者[i,0],b=优胜者[i,1],c=优胜者[i,2];
按钮b1=myControls[a],b2=myControls[b],b3=myControls[c];
如果(b1.Text==“”| | b2.Text==“”| | b3.Text==“”)
继续;
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无衬线”,32F,System.Drawing.FontStyle.Italic和System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,((System.Byte)(0));
gameOver=true;
表单xWinnerPerform=新的xWinnerPerform();
xWinPerform.Show();
//MessageBox.Show(b1.Text+“…赢得游戏!”,“游戏结束”,MessageBox按钮。确定);
//中断;
}
}
返回游戏结束;
}
}
}

听起来b1的文本框是私有的(或受保护的)。把它公之于众应该可以奏效

如果您只想从b1中获得获胜者的姓名,那么winforms中的每个表单都有一个标记,该标记是公共的。您可以在某人获胜后将标记设置为获胜者的姓名,然后以另一种形式执行b1.Tag.ToString()将其设置为获胜者的姓名

另外,作为旁白;在“现实生活”的应用程序中,您可能希望将这些组件中的一些封装到不同的类中,而不是让窗体查看彼此的控件

编辑


现在我面前没有Visual Studio,但我相信在“属性”窗口中,您可以将特定的文本框设置为“public”。

您可以做的一件事是在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);

另一个选项是将文本传递到XWinnerText的构造函数。

或者只添加一个方法“SetWinnerText(string text){this.myLabel.text=text;},然后用xWinnerForm=new XWinnerPerform();form.SetWinnerText(b1.text);form.Show()调用它;我尝试了这个方法,但它给出了一个错误XWinnerPerform.Show(b1.text);无法从字符串转换为system.windows.forms.stringiwin32window@Michael奎尔斯:介意发布引发该错误的代码行吗?