Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys,

started to learn C#, im trying to do a quizz game and my goal is to have 3 forms.
First one is where the begin is, the second the actual game and the third the score.

But im stuck here, i can swap forms but my form2 is not ative i cant do anything on it..

What I have tried:

C#
private void btn_play_Click(object sender, EventArgs e)
       {
           QUIZ jogo = new QUIZ();
           this.Hide();
           jogo.StartPosition = FormStartPosition.CenterScreen;
           jogo.Show();

       }
Posted
Updated 27-Jan-18 19:41pm
Comments
cvogt61457 23-Jan-18 15:36pm    
We going to need to see some more of you code - pretty much all of it.
I don't think there is a way to zip your solution and attach it though.
Reduce the code for each form and the main display to the minimum that shows the problem and put that in the question.

Use the Form.ShowDialog() method.
Form.Show() does display a form but the method you use to show the form then completes - any code after Form.Show() will be executed.
Form.ShowDialog() displays a form & stops processing the method until the form is closed.
Refer following MSDN article on Form.ShowDialog();
Form.ShowDialog Method (System.Windows.Forms)[^]

This will allow you to use a linear processing mode - pseudo code below;
C#
// code in your btn_play_click event handler
this.Hide();
formQuiz QuizForm = new QuizForm();
formQuiz.StartupPosition = FormStartPosition.CenterScreen;
formQuiz.ShowDialog();
// this method will then wait until the formQuiz is closed

The other advantage of using ShowDialog is that the method can return a result - Form.Show does not return a result & would need to have public properties for you to retrieve values to determine if the user closed the form or completed the quiz.

Hope this helps

Kind Regards
 
Share this answer
 
GameControl // winform project main form
Button btnPlay;
Button btnGetScore;
TextBox tbxScoreReport;
CheckBox checkBox1;
CheckBox checkBox2;

1. flow-of control: app launch

a. the 'GameControl instance is loaded as the app starts

a.1. an instance of 'GamePlay, 'Game, is created.

a.2 an event handler for the FormClosing Enent of 'Game is installed

a.2.a this tests whether the Game is closing because the application is closing or whether it's closing because the user closed this form: if the app is closing then nothing is done; if not, then if there is any game data, a report of game stats is written to the TextBox in the main form.

b. when the Play button is clicked

b.1 the ConfigureGame method of 'Game is called to set initial game parameters

b.2 the Play button is disabled

b.3 the Game form is shown

2. flow-of control: Game form shown

a. a new instance of ScoreData is created, 'Data

b. the Shown event handler sets the game start-time

c. a KeyDown handler is installed that will close if alt and q keys are both held down

c.1 when the Game form closes this will trigger the FormClosing Event defined in the GameControl instance

d. the btnAddPoints and btnAddPoints buttons increment and decrement the score

e. the GetGameStatus method is only called by the GameControl instance

code example:
public partial class GameControl : Form
{
    private static int gameCounter = 1;

    private GamePlay Game;

    public GameControl()
    {
        InitializeComponent();
    }

    private void GameProtoType_Load(object sender, EventArgs e)
    {
        Game = new GamePlay();

        Game.Owner = this;

        Game.FormClosing += GameOnFormClosing;
    }

    private void GameOnFormClosing(object sender, FormClosingEventArgs e)
    {
        if (Game.Data != null && e.CloseReason != CloseReason.FormOwnerClosing)
        {
            Game.Data.GameEnd = DateTime.Now;
            tbxScoreReport.Text = Game.GetGameStatus();

            btnPlay.BackColor = SystemColors.Control;
            btnPlay.Enabled = true;
        }
    }

    private void btnPlay_Click(object sender, EventArgs e)
    {
        tbxScoreReport.Clear();

        Game.ConfigureGame(gameCounter++, "The Player", checkBox1.CheckState, checkBox2.CheckState);

        btnPlay.BackColor = Color.Firebrick;
        btnPlay.Enabled = false;

        Game.Show();
    }

    private void btnGetScore_Click(object sender, EventArgs e)
    {
        if (Game.Data == null) return;

        tbxScoreReport.Text = Game.GetGameStatus();
    }
}
GamePlay // winform owned by 'GameControl
TextBox tbxCurrentScore;
Button btnAddPoints;
Button btnSubPoints;
public partial class GamePlay : Form
{
    public GamePlay()
    {
        InitializeComponent();
    }

    public ScoreData Data { set; get; }

    public void ConfigureGame(int id, string plname = "", CheckState chk1 = CheckState.Unchecked,
        CheckState chk2 = CheckState.Unchecked)
    {
        Data = new ScoreData(id, plname);

        if (chk1 == CheckState.Checked)
        {
            // configure
        }

        if (chk2 == CheckState.Checked)
        {
            // configure
        }
    }

    private void GamePlay_Shown(object sender, EventArgs e)
    {
        Data.GameStart = DateTime.Now;
    }

    private void btnAddPoints_Click(object sender, EventArgs e)
    {
        Data.ChangeScore(100);
        tbxCurrentScore.Text = Data.Score.ToString();
    }

    private void btnSubPoints_Click(object sender, EventArgs e)
    {
        Data.ChangeScore(-100);
        tbxCurrentScore.Text = Data.Score.ToString();
    }

    public string GetGameStatus()
    {
        var duration = Data.GameEnd - Data.GameStart;

        return
            $"Player {Data.PlayerName} : Id {Data.Id} : Score {Data.Score}\r\nStart : {Data.GameStart} : End {Data.GameEnd}\r\nDuration : hours {duration.Hours} | minutes: {duration.Minutes} | seconds: {duration.Seconds} | ms: {duration.Milliseconds}";
    }

    private void GamePlay_KeyDown(object sender, KeyEventArgs e)
    {
        if (ModifierKeys == Keys.Alt && e.KeyCode == Keys.Q)
        {
            this.Close();
        }
    }
}

public class ScoreData
{
    public ScoreData(int id, string plname = "")
    {
        Id = id;
        PlayerName = plname;

        Score = 0;
        GameStart = DateTime.Now;
    }

    public int Id { set; get; }
    public string PlayerName { set; get; }

    public int Score { set; get; }

    public DateTime GameStart { set; get; }

    public DateTime GameEnd { set; get; }

    public void ChangeScore(int adjust)
    {
        Score += adjust;
    }
}
Not implemented here: a way to pause the game, and re-start it, keeping track of actual game-play time ... should be easy.

... end edit ...

First, you need to make some strategic choices:

1. what Form do you want the user to see when the application launches ?

a. splash screen ?
b. configuration screen ?
c. log-in screen
d. other main screen ?

e. are there any circumstances under which the game-play window appears first ?

2. do your Forms require interaction, or need to send, or receive, data ? In your game, clearly, the score form must have the data from game-play.

3. use of Forms shown modal ?

a. do you want the game-screen to lock your app until it is closed ?

4. for each Form what visual settings do you want ?

a. FormBorderStyle, ShowinTaskBar, ShowIcon, StartPosition, etc.

b. fixed or variable size ?

Tell me your choices, and I;ll be haooy to ost a code example.
 
Share this answer
 
v5
Comments
Member 13639648 29-Jan-18 4:49am    
Hello :)

The form1 its just for the user start the game itself via button. The 2nd form is the game itself, the only input by the user are the checkboxes. The 3rd form i was thinking about displaying the score and the option of repeat and exit via buttons.

The size and the style have already been taken care of
BillWoodruff 29-Jan-18 6:21am    
Hi, the second Form, where the game is "played:" tnhis is a Windows Form, also ?

In the next 24 hours I'll post a code example that I think will give you some ideas on how to manage communication between Forms.

cheers, Bill
Member 13639648 29-Jan-18 6:39am    
yes it is.

Thanks for ur help bill

cheers
BillWoodruff 30-Jan-18 8:05am    
Code example added. Let me know if you have questions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900