C#表单在未被告知的情况下消失

C#表单在未被告知的情况下消失,c#,winforms,.net-3.0,C#,Winforms,.net 3.0,因此,我有一个表单,它创建另一个表单并使用form.ShowDialog()调用它,但是在运行过程中的某个时刻,新表单消失了。我可以告诉它仍然在运行,因为我设置了一个计时器,当完成时,会显示一个消息框。消息框在分配的时间之后仍然显示,但表单不再可见 要调用我使用的表单,请使用以下代码: Player currentUser = new Player(); //Player is a class I've created private void Game(int PlayerCount) {

因此,我有一个表单,它创建另一个表单并使用
form.ShowDialog()
调用它,但是在运行过程中的某个时刻,新表单消失了。我可以告诉它仍然在运行,因为我设置了一个计时器,当完成时,会显示一个消息框。消息框在分配的时间之后仍然显示,但表单不再可见

要调用我使用的表单,请使用以下代码:

Player currentUser = new Player(); //Player is a class I've created

private void Game(int PlayerCount)
{
    Current.Hide();
    for (int i = 0; i < PlayerCount; i++)
    {
        currentUser = GloVar.CurrentPlayers[i];
        CashBuilder cb = new CashBuilder(currentUser);
        cb.ShowDialog();
    }
 }
如果答案在正确和不正确之间交替出现,表格似乎(我无法100%澄清)保持可见。 但是,如果我回答正确,那么正确(或不正确和不正确)表格就会消失

我有一个关闭表单的方法,但在计时器启动之前,不会触动它

我已经搜索了大约一个星期来寻找解决方案,尝试了多个线程(让线程工作,但仍然遇到这个问题),我无法思考其他任何事情


编辑

这是CashBuilder的全部源代码

public partial class CashBuilder : Form
{
    public static CashBuilder current;
    private static int QuestionID; //Identifies the loctaion of the question
    private static int CorrectAnswer; //holds the place of the correct answer
    private static int CountDown = 60; //hold a count of 60, one foe earch second. This adds to a minute.
    private static int QuestionsAnswered = 0; //how many questions have been answered
    private static int AnsweredCorrectly = 0; //out of those how many correctly
    private static int CashWon = 0; //chash won, £1000 per correct answer
    private static int Wait = 10; //holds ten ticks for the second timer
    private static Timer Timer1; //One minute, used to time the Cash Builder round
    private static Timer Timer2; //One fifth of a second, used for adding £1000 to cash wont every correct answer. Looks like a gameshow
    private static Player User;



    public CashBuilder(Player user)
    {
        InitializeComponent();
        current = this;
        User = user;
        pctAvatar.Image = Image.FromFile(GloVar.Avatar(User.ID));
        Generate_Question();
        btnCash.Text = "£0000"; //sets initial Cash Won lable to nothing
        Timer1 = new Timer(); //Creates a new timer
        Timer1.Tick += new EventHandler(timer_Tick);
        Timer1.Interval = 1000; //1 second
        Timer1.Start(); //Starts timer, this is the begining of the round
        btnTime.Text = "Time Left: " + CountDown.ToString();
        Timer2 = new Timer();
        Timer2.Tick += new EventHandler(Timer2_Tick);
        Timer2.Interval = 20; //0.02 seconds
    }

    void timer_Tick(object sender, EventArgs e)
    {
        CountDown--; //takes one away from time left
        btnTime.Text = "Time Left: " + CountDown.ToString(); //shows the user
        if (CountDown == 0) //if theres no time left
        {
            Timer1.Stop(); //stop timer
            Enough();
        }
    }

    private void Timer2_Tick(object sender, EventArgs e)
    {
        Wait--;
        CashWon += 100; //adds 100 to the cash won. Ticks ten times therefore end will be +1000
        btnCash.Text = "£" + CashWon.ToString(); //shows user
        if (Wait == 0)
        {
            Timer2.Stop();
            Wait = 10; //resets the tick count for a later question
        }
    }

    private void Generate_Question()
    {
        Random rnd = new Random();  //used to generate a random value (only integers in this case) within parameters given

        do
        {
            QuestionID = rnd.Next(82); //there are only 82 questions, references as a zero-index. Cannot generate 82.
        } while (GloVar.QuestionsAsked.Contains(QuestionID)); //continue loop if question has already been asked
        GloVar.QuestionsAsked.Add(QuestionID); //if question hasnt been asked, add it to the list of asked questions
        btnQuestion.Text = GloVar.Questions[(QuestionID * 6) + 1]; //show the question on the form


        List<int> AnswerPlacement = new List<int>(); //used to see what answers have already been placed
        int[] Answers = new int[4]; //holds placevalues for the answers
        for (int i = 0; i < 4; i++)
        {
            do
            {
                Answers[i] = rnd.Next(4); //generates random placevalue
            } while (AnswerPlacement.Contains(Answers[i])); //while the placevalue entered is already used
            AnswerPlacement.Add(Answers[i]); //show the placevalue has been used
            if (Answers[i] == 0)
            {
                CorrectAnswer = i;
            }
        }

        btnAns1.Text = GloVar.Questions[(QuestionID * 6) + Answers[0] + 2]; //giving buttons their answers
        btnAns2.Text = GloVar.Questions[(QuestionID * 6) + Answers[1] + 2];
        btnAns3.Text = GloVar.Questions[(QuestionID * 6) + Answers[2] + 2];
        btnAns4.Text = GloVar.Questions[(QuestionID * 6) + Answers[3] + 2];

        AnswerPlacement.Clear();
        lblFeedback.Text = "";
    }

    private void btnAns1_Click(object sender, EventArgs e)
    {
        IsCorrect(0);
    }

    private void btnAns2_Click(object sender, EventArgs e)
    {
        IsCorrect(1);
    }

    private void btnAns3_Click(object sender, EventArgs e)
    {
        IsCorrect(2);
    }

    private void btnAns4_Click(object sender, EventArgs e)
    {
        IsCorrect(3);
    }

    private void btnPass_Click(object sender, EventArgs e)
    {
        Generate_Question();
        QuestionsAnswered++;
    }

    private void IsCorrect(int i)
    {
        if (CorrectAnswer == i) //if the place of the button value given and the correct answer are the same
        {
            lblFeedback.Text = "Correct."; //show the user their awesomeness
            AnsweredCorrectly++; //add to the answered correctly list
            Timer2.Start(); //this timer adds the newly gained £1000
        }
        else
        {
            lblFeedback.Text = Convert.ToString(SSDTheChase.GloVar.Questions[(QuestionID * 6) + 2]); //show their failure
        }
        QuestionsAnswered++; //add to questions answered
        Generate_Question(); //bring a new question
    }

    private void Enough()
    {
        btnAns1.Text = ""; //set all buttons and lables used for a question equal to zero
        btnAns2.Text = "";
        btnAns3.Text = "";
        btnAns4.Text = "";
        btnQuestion.Text = "";
        CountDown = 60; //resetting time
        MessageBox.Show("Time's up." + Environment.NewLine + QuestionsAnswered + " Questions Answered." + Environment.NewLine + AnsweredCorrectly + " Answered Correctly.");
        User.CashGame = CashWon; //setting the users cash won this game.

        current.Close(); //close the form.
    }
}
公共部分类CashBuilder:表单
{
公共静态现金流;
private static int QuestionID;//标识问题的位置
private static int CorrectAnswer;//保留正确答案的位置
private static int CountDown=60;//保持计数60,每秒钟一次。这会增加一分钟。
private static int questionsansweed=0;//回答了多少个问题
private static int answeredcorrect=0;//其中有多少是正确的
私有静态int CashWon=0;//chash won,每个正确答案1000英镑
private static int Wait=10;//为第二个计时器保留十个刻度
私有静态计时器Timer1;//一分钟,用于为现金生成器循环计时
私人静态计时器计时器2;//五分之一秒,用于向现金中添加1000英镑,但不是每个答案都正确。看起来像一场游戏秀
私有静态播放器用户;
公共现金生成器(玩家用户)
{
初始化组件();
电流=这个;
用户=用户;
pctAvatar.Image=Image.FromFile(GloVar.Avatar(User.ID));
生成_问题();
btnCash.Text=“£0000”//将初始现金赢得标签设置为空
Timer1=new Timer();//创建一个新计时器
Timer1.Tick+=新事件处理程序(timer\u Tick);
Timer1.Interval=1000;//1秒
Timer1.Start();//启动计时器,这是一轮的开始
btnTime.Text=“剩余时间:”+CountDown.ToString();
Timer2=新定时器();
Timer2.Tick+=新事件处理程序(Timer2\u Tick);
Timer2.Interval=20;//0.02秒
}
无效计时器勾号(对象发送方,事件参数e)
{
倒计时--;//从剩余时间中减去一个
btnTime.Text=“剩余时间:”+CountDown.ToString();//显示用户
if(倒计时==0)//如果没有时间了
{
Timer1.Stop();//停止计时器
够了();
}
}
私有无效计时器2_刻度(对象发送方,事件参数e)
{
等等——;
CashWon+=100;//将100添加到cash won中。因此,十次勾号结束将为+1000
btnCash.Text=“£”+CashWon.ToString();//显示用户
如果(等待==0)
{
Timer2.Stop();
Wait=10;//为以后的问题重置勾号计数
}
}
私有void生成_问题()
{
Random rnd=new Random();//用于在给定参数内生成随机值(本例中仅为整数)
做
{
QuestionID=rnd.Next(82);//只有82个问题,引用为零索引。无法生成82。
}while(GloVar.QuestionsAsked.Contains(QuestionID));//如果已经询问了问题,则继续循环
GloVar.QuestionsAsked.Add(QuestionID);//如果问题尚未被提出,请将其添加到问题列表中
btnQuestion.Text=GloVar.Questions[(QuestionID*6)+1];//在表单上显示问题
List AnswerPlacement=new List();//用于查看已放置的答案
int[]Answers=new int[4];//保存答案的placevalue
对于(int i=0;i<4;i++)
{
做
{
Answers[i]=rnd.Next(4);//生成随机值
}while(AnswerPlacement.Contains(Answers[i]);//而输入的placevalue已被使用
AnswerPlacement.Add(Answers[i]);//显示已使用placevalue
如果(回答[i]==0)
{
正确答案=i;
}
}
btnAns1.Text=GloVar.Questions[(问题ID*6)+答案[0]+2];//给出按钮的答案
btnAns2.Text=GloVar.Questions[(问题ID*6)+答案[1]+2];
btnAns3.Text=GloVar.Questions[(问题ID*6)+答案[2]+2];
btnAns4.Text=GloVar.Questions[(问题ID*6)+答案[3]+2];
回答位置。清除();
lblFeedback.Text=“”;
}
私有void btnAns1_单击(对象发送方,事件参数e)
{
不正确(0);
}
私有void btnAns2_单击(对象发送方,事件参数e)
{
不正确(1);
}
私有无效btnAns3_单击(对象发送者,事件参数e)
{
不正确(2);
}
私有无效btnAns4_单击(对象发送者,事件参数e)
{
不正确(3);
}
私有void btnpasss\u单击(对象发送方,事件参数e)
{
生成_问题();
问题回答++;
}
私人无效信息是正确的(int i)
{
if(CorrectAnswer==i)//如果给定的按钮值和正确答案的位置相同
{
lblFeedback.Text=“Correct.”;//向用户展示他们的魅力
AnsweredCorrect++;//添加到正确回答列表
Timer2.Start();//此计时器添加新获得的1000英镑
}
其他的
{
lblFeedback.Text=Convert.ToString(SSDTheChase.GloVar.Questions[(QuestionID*6)+2]);//显示它们的失败
}
public partial class CashBuilder : Form
{
    public static CashBuilder current;
    private static int QuestionID; //Identifies the loctaion of the question
    private static int CorrectAnswer; //holds the place of the correct answer
    private static int CountDown = 60; //hold a count of 60, one foe earch second. This adds to a minute.
    private static int QuestionsAnswered = 0; //how many questions have been answered
    private static int AnsweredCorrectly = 0; //out of those how many correctly
    private static int CashWon = 0; //chash won, £1000 per correct answer
    private static int Wait = 10; //holds ten ticks for the second timer
    private static Timer Timer1; //One minute, used to time the Cash Builder round
    private static Timer Timer2; //One fifth of a second, used for adding £1000 to cash wont every correct answer. Looks like a gameshow
    private static Player User;



    public CashBuilder(Player user)
    {
        InitializeComponent();
        current = this;
        User = user;
        pctAvatar.Image = Image.FromFile(GloVar.Avatar(User.ID));
        Generate_Question();
        btnCash.Text = "£0000"; //sets initial Cash Won lable to nothing
        Timer1 = new Timer(); //Creates a new timer
        Timer1.Tick += new EventHandler(timer_Tick);
        Timer1.Interval = 1000; //1 second
        Timer1.Start(); //Starts timer, this is the begining of the round
        btnTime.Text = "Time Left: " + CountDown.ToString();
        Timer2 = new Timer();
        Timer2.Tick += new EventHandler(Timer2_Tick);
        Timer2.Interval = 20; //0.02 seconds
    }

    void timer_Tick(object sender, EventArgs e)
    {
        CountDown--; //takes one away from time left
        btnTime.Text = "Time Left: " + CountDown.ToString(); //shows the user
        if (CountDown == 0) //if theres no time left
        {
            Timer1.Stop(); //stop timer
            Enough();
        }
    }

    private void Timer2_Tick(object sender, EventArgs e)
    {
        Wait--;
        CashWon += 100; //adds 100 to the cash won. Ticks ten times therefore end will be +1000
        btnCash.Text = "£" + CashWon.ToString(); //shows user
        if (Wait == 0)
        {
            Timer2.Stop();
            Wait = 10; //resets the tick count for a later question
        }
    }

    private void Generate_Question()
    {
        Random rnd = new Random();  //used to generate a random value (only integers in this case) within parameters given

        do
        {
            QuestionID = rnd.Next(82); //there are only 82 questions, references as a zero-index. Cannot generate 82.
        } while (GloVar.QuestionsAsked.Contains(QuestionID)); //continue loop if question has already been asked
        GloVar.QuestionsAsked.Add(QuestionID); //if question hasnt been asked, add it to the list of asked questions
        btnQuestion.Text = GloVar.Questions[(QuestionID * 6) + 1]; //show the question on the form


        List<int> AnswerPlacement = new List<int>(); //used to see what answers have already been placed
        int[] Answers = new int[4]; //holds placevalues for the answers
        for (int i = 0; i < 4; i++)
        {
            do
            {
                Answers[i] = rnd.Next(4); //generates random placevalue
            } while (AnswerPlacement.Contains(Answers[i])); //while the placevalue entered is already used
            AnswerPlacement.Add(Answers[i]); //show the placevalue has been used
            if (Answers[i] == 0)
            {
                CorrectAnswer = i;
            }
        }

        btnAns1.Text = GloVar.Questions[(QuestionID * 6) + Answers[0] + 2]; //giving buttons their answers
        btnAns2.Text = GloVar.Questions[(QuestionID * 6) + Answers[1] + 2];
        btnAns3.Text = GloVar.Questions[(QuestionID * 6) + Answers[2] + 2];
        btnAns4.Text = GloVar.Questions[(QuestionID * 6) + Answers[3] + 2];

        AnswerPlacement.Clear();
        lblFeedback.Text = "";
    }

    private void btnAns1_Click(object sender, EventArgs e)
    {
        IsCorrect(0);
    }

    private void btnAns2_Click(object sender, EventArgs e)
    {
        IsCorrect(1);
    }

    private void btnAns3_Click(object sender, EventArgs e)
    {
        IsCorrect(2);
    }

    private void btnAns4_Click(object sender, EventArgs e)
    {
        IsCorrect(3);
    }

    private void btnPass_Click(object sender, EventArgs e)
    {
        Generate_Question();
        QuestionsAnswered++;
    }

    private void IsCorrect(int i)
    {
        if (CorrectAnswer == i) //if the place of the button value given and the correct answer are the same
        {
            lblFeedback.Text = "Correct."; //show the user their awesomeness
            AnsweredCorrectly++; //add to the answered correctly list
            Timer2.Start(); //this timer adds the newly gained £1000
        }
        else
        {
            lblFeedback.Text = Convert.ToString(SSDTheChase.GloVar.Questions[(QuestionID * 6) + 2]); //show their failure
        }
        QuestionsAnswered++; //add to questions answered
        Generate_Question(); //bring a new question
    }

    private void Enough()
    {
        btnAns1.Text = ""; //set all buttons and lables used for a question equal to zero
        btnAns2.Text = "";
        btnAns3.Text = "";
        btnAns4.Text = "";
        btnQuestion.Text = "";
        CountDown = 60; //resetting time
        MessageBox.Show("Time's up." + Environment.NewLine + QuestionsAnswered + " Questions Answered." + Environment.NewLine + AnsweredCorrectly + " Answered Correctly.");
        User.CashGame = CashWon; //setting the users cash won this game.

        current.Close(); //close the form.
    }
}