如何激活下一个表单 [英] How do I activate a next form

查看:77
本文介绍了如何激活下一个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,



开始学习C#,我试图做一个quizz游戏,我的目标是有3个表格。

第一个是开始的地方,第二个是实际游戏,第三个是分数。



但我坚持到这里,我可以交换表格,但我的表格2不是我不能做任何事情......



我尝试过:



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:

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

       }

推荐答案

使用Form.ShowDialog()方法。

Form.Show()确实显示一个表单,但是用于显示表单然后完成的方法 - 将执行Form.Show()之后的任何代码。

Form.ShowDialog()显示一个表格&停止处理该方法,直到表单关闭。

请参阅以下关于Form.ShowDialog()的MSDN文章;

Form.ShowDialog Method(System.Windows.Forms) [ ^ ]



这将允许您使用线性处理模式 - 下面的伪代码;

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;
// 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



使用ShowDialog的另一个好处是该方法可以返回结果 - Form.Show不会返回结果&需要有公共属性来检索值以确定用户是否关闭了表单或完成了测验。



希望这有助于



亲切的问候


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


GameControl // winform项目主表单

Button btnPlay;

Button btnGetScore ;

TextBox tbxScoreReport;

CheckBox checkBox1;

CheckBox checkBox2;



1.控制流程:应用程序启动



a。 'GameControl实例在应用程序启动时加载



a.1。创建了一个'GamePlay'游戏的实例。



a.2 FormClosing的事件处理程序'游戏安装完成'



a.2.a这测试游戏是否正在关闭,因为应用程序正在关闭或者是否因为用户关闭此表单而关闭:如果应用程序正在关闭,则不执行任何操作;如果没有,那么如果有任何游戏数据,游戏统计数据的报告将写入主表格中的TextBox。



b。单击播放按钮时



b.1调用游戏的ConfigureGame方法设置初始游戏参数



b.2播放按钮被禁用



b.3游戏表格显示



2.流量控制:显示的游戏形式



a。创建了一个新的ScoreData实例,'数据



b。 Shown事件处理程序设置游戏开始时间



c。一个KeyDown处理程序安装将关闭如果alt和q键都被按下



c.1当游戏表格关闭时这将触发在此定义的FormClosing事件GameControl实例



d。 btnAddPoints和btnAddPoints按钮增加和减少分数



e。 GetGameStatus方法只由GameControl实例调用



代码示例:
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拥有'GameControl

TextBox tbxCurrentScore;

Button btnAddPoints;

Button btnSubPoints;

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}:得分{Data.Score} \\\\ n开始:{数据.GameStart}:结束{Data.GameEnd} \\\
Duration: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();
}
}
}

公共类ScoreData
{
public Sc​​oreData(int id,string plname =)
{
Id = id;
PlayerName = plname;

得分= 0;
GameStart = DateTime.Now;
}

public int Id {set;得到; }
public string PlayerName {set;得到; }

public int Score {set;得到; }

public DateTime GameStart {set;得到; }

public DateTime GameEnd {set;得到; }

public void ChangeScore(int adjust)
{
Score + = adjust;
}
}
"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; } }

这里没有实现:暂停游戏,重新启动,跟踪实际游戏时间的方法......应该很容易。< br $> b $ b

...结束编辑...



首先,你需要做出一些战略选择:



1.您希望用户在应用程序启动时看到什么表单?



a。闪屏?

b。配置屏幕?

c。登录屏幕

d。其他主屏幕?



e。有没有首先出现游戏窗口的情况?



2.您的表格需要交互,还是需要发送或接收数据?在你的游戏中,显然,得分表格必须包含游戏中的数据。



3.使用表格模式?



a。你想要游戏屏幕锁定你的应用程序直到它关闭?



4.每张表格你想要什么视觉设置?



a。 FormBorderStyle,ShowinTaskBar,ShowIcon,StartPosition等。



b。固定的或可变的大小?



告诉我你的选择,而且我会找一个代码示例。

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.


这篇关于如何激活下一个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆