How to show data from another form

This is basically a game with tic tac toe, and I have another form called Winner.cs, when the player wins, I want him to call the form (this part works), and then I want it to say xWinner.label = b1. the text "" + won the game !. the part that I can’t work with displays the text in the winners form label. Here is an example message box that is commented out for reference instead of 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;
        }
    }
}
+3
source share
2 answers

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

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

then you will need to change two lines of code in your code block:

from this:

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

:

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

- xWinnerForm.

+1

, b1 ( ). .

, b1, - , winforms , . , - , b1.Tag.ToString(), .

, ; " " , , , , .

Visual Studio , , "public".

0

Source: https://habr.com/ru/post/1746685/


All Articles