【问题标题】:How can show data from another form如何显示来自另一个表单的数据
【发布时间】:2010-05-22 16:47:47
【问题描述】:

这基本上是一个井字游戏,当玩家获胜时,我有另一个名为 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;
        }
    }
}

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    您可以做的一件事是在您的 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 的构造函数。

    【讨论】:

    • 或者只是添加一个方法 "SetWinnerText(string text) {this.myLabel.Text = text; } 然后用 xWinnerForm form = new xWinnerForm(); form.SetWinnerText(b1.Text) 调用它; form.Show();
    • 我试过这个方法,但它给出了一个错误 xWinnerForm.Show(b1.Text);无法从字符串转换为 system.windows.forms.string iwin32window
    • @Michael Quiles:介意发布引发该错误的代码行吗?
    【解决方案2】:

    听起来 b1 的文本框是私有的(或受保护的)。将其公开应该可以解决问题。

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

    另外,顺便说一句;在“现实生活”应用程序中,您可能希望将其中一些组件封装到不同的类中,而不是让表单查看彼此的控件。

    编辑

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 2020-01-30
      • 2019-11-01
      • 1970-01-01
      • 2014-04-01
      • 1970-01-01
      相关资源
      最近更新 更多