Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can show data from another form

Tags:

c#

winforms

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

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;
        }
    }
}
like image 916
Michael Quiles Avatar asked Jan 26 '26 15:01

Michael Quiles


1 Answers

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.

like image 116
Joel Avatar answered Jan 29 '26 04:01

Joel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!