开发者

How can show data from another form

This is basically a tic tac toe game, and I have another form called Winner.cs when a player wins I want it to call the form (this part works) and then I want it to say xWinner.label =b1.text"" + has won the game!. the part I cant get to work is displaying the text in the winners form label. There's an example of a message box that commented out for reference instead of b1.text

usin开发者_如何转开发g 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;
        }
    }
}


One thing you can do is create your own Show method in your cWinnerForm class:

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

then you would have to change two lines of code in your code block:

from this:

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

to this:

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

Another option is to pass the text into the xWinnerForm's constructor.


Sounds like b1's textbox is private (or protected). Making it public should do the trick.

If the only thing that you want from b1 is the winner's name, each form in winforms has a Tag, which is public. You can set the Tag after someone wins to the winner's name and then in the other form do b1.Tag.ToString() to get it there.

Also, as an aside; In a "real life" application you'd probably want to encapsulate some of these components into different classes rather than having forms look at each others' controls.

EDIT

I don't have Visual Studio in front of me now, but I believe that in the properties window you can set that particular textbox to "public."

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜