当我点击下一个按钮时,为什么我的程序看起来有点迟钝? [英] Why my program looks abit laggy when i click the next button?

查看:52
本文介绍了当我点击下一个按钮时,为什么我的程序看起来有点迟钝?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

This is the part of the code of my program. After i added the

SpeechSynthesizer后,我的整个程序看起来非常滞后。

单击下一步按钮会更糟。我想问题

在我的下一个按钮点击代码中,可以有人帮我解决

这个问题?

int correctAns = 0; //正确答案的计数
int wrongAns = 0; //错误答案的计数
int current_tab = 0; //选项卡的计数
int numOftry = 1;
bool正确;
bool nexttab = false;
SpeechSynthesizer ss = new SpeechSynthesizer();

private void startpage()
{
btnStart.Visible = true;
next.Visible = false;
correctAns = 0;
wrongAns = 0;
}

private void getvalue(bool _correct,bool _nexttab)
{
correct = _correct;
nexttab = _nexttab;
}

private void btnStart_Click(object sender,EventArgs e)
{
btnStart.Visible = false;
next.Visible = true;
current_tab ++;
tabControl1.SelectedIndex = current_tab; //当前标签等于tabControl1中所选的标签
if(current_tab == 1)
{
ss.Speak(lblQ1.Text);
}
}

public void Question1()
{
yellow.BackColor = Color.Transparent;
green.BackColor = Color.Transparent;
red.BackColor = Color.Transparent;
blue.BackColor = Color.Transparent;
}

private void next_Click(object sender,EventArgs e)
{

if(nexttab == true)
{
if(correct == true)
{
correctAns ++;
}
else if(correct == false)
{
wrongAns ++;
}
if(current_tab!= 0)
{
current_tab ++;
}
else if(current_tab == 0)
{
startpage();
}

tabControl1.SelectedIndex = current_tab;
nexttab = false;
}
else if(nexttab == false)
{
MessageBox.Show(" Please select an answer !!!");
}

if(current_tab == 2)
{
ss.Speak(lblQ2.Text);
}

else if(current_tab == 3)
{
ss.Speak(lblQ3.Text);
}

else if(current_tab == 4)
{
ss.Speak(lblQ4.Text);
}

else if(current_tab == 5)
{
ss.Speak(lblQ5.Text);
}

else if(current_tab == 6)
{
ss.Speak(lblQ6.Text);
}

else if(current_tab == 7)
{
ss.Speak(lblQ7.Text);
}

else if(current_tab == 8)
{
ss.Speak(lblQ8.Text);
}

else if(current_tab == 9)
{
ss.Speak(lblQ9.Text);
}

else if(current_tab == 10)
{
ss.Speak(lblQ10.Text);
}

else if(current_tab == 11)
{
if(numOftry == 1)
{
this.chart1。系列[" Correct Ans"]。Points.AddXY("First try",correctAns);
this.chart1.Series [" Wrong Ans]]。Points.AddXY(" First try",wrongAns);
}
else if(numOftry == 2)
{
this.chart1.Series [" Correct Ans"]。Points.AddXY(" Second try",correctAns );
this.chart1.Series [" Wrong Ans]]。Points.AddXY(" Second try",wrongAns);
}
else if(numOftry == 3)
{
this.chart1.Series [" Correct Ans"]。Points.AddXY(" Third try",correctAns );
this.chart1.Series [" Wrong Ans]]。Points.AddXY(" Third try",wrongAns);
}
else if(numOftry == 4)
{
this.chart1.Series [" Correct Ans"]。Points.AddXY(" Fourth try",correctAns );
this.chart1.Series [" Wrong Ans]]。Points.AddXY(" Fourth try",wrongAns);
}
else if(numOftry == 5)
{
this.chart1.Series [" Correct Ans"]。Points.AddXY(" Fifth try",correctAns );
this.chart1.Series [" Wrong Ans]]。Points.AddXY(" Fifth try",wrongAns);
}
nexttab = true;
numOftry ++;
current_tab = 0;
问题1();
问题2();
问题3();
问题4();
问题5();
问题6();
问题7();
Question8();
问题9();
问题10();
}

private void red_Click(object sender,EventArgs e)
{
getvalue(true,true);
问题1();
red.BackColor = Color.Black;
this.red.Padding = new System.Windows.Forms.Padding(5);
}
private void green_Click(object sender,EventArgs e)
{
getvalue(false,true);
问题1();
green.BackColor = Color.Black;
this.green.Padding = new System.Windows.Forms.Padding(5);
}

private void blue_Click(object sender,EventArgs e)
{
getvalue(false,true);
问题1();
blue.BackColor = Color.Black;
this.blue.Padding = new System.Windows.Forms.Padding(5);
}

private void yellow_Click(object sender,EventArgs e)
{
getvalue(false,true);
问题1();
yellow.BackColor = Color.Black;
this.yellow.Padding = new System.Windows.Forms.Padding(5);
}

int correctAns = 0; //count of the correct answer int wrongAns = 0; //count of the wrong answer int current_tab = 0; //count of the tab int numOftry = 1; bool correct; bool nexttab = false; SpeechSynthesizer ss = new SpeechSynthesizer(); private void startpage() { btnStart.Visible = true; next.Visible = false; correctAns = 0; wrongAns = 0; } private void getvalue(bool _correct, bool _nexttab) { correct = _correct; nexttab = _nexttab; } private void btnStart_Click(object sender, EventArgs e) { btnStart.Visible = false; next.Visible = true; current_tab++; tabControl1.SelectedIndex = current_tab; //current tab equal to the tab selected in the tabControl1 if (current_tab == 1) { ss.Speak(lblQ1.Text); } } public void Question1() { yellow.BackColor = Color.Transparent; green.BackColor = Color.Transparent; red.BackColor = Color.Transparent; blue.BackColor = Color.Transparent; } private void next_Click(object sender, EventArgs e) { if(nexttab == true) { if (correct == true) { correctAns++; } else if (correct == false) { wrongAns++; } if (current_tab != 0) { current_tab++; } else if(current_tab == 0) { startpage(); } tabControl1.SelectedIndex = current_tab; nexttab = false; } else if (nexttab == false) { MessageBox.Show("Please select an answer!!!"); } if(current_tab == 2) { ss.Speak(lblQ2.Text); } else if (current_tab == 3) { ss.Speak(lblQ3.Text); } else if (current_tab == 4) { ss.Speak(lblQ4.Text); } else if (current_tab == 5) { ss.Speak(lblQ5.Text); } else if (current_tab == 6) { ss.Speak(lblQ6.Text); } else if (current_tab == 7) { ss.Speak(lblQ7.Text); } else if (current_tab == 8) { ss.Speak(lblQ8.Text); } else if (current_tab == 9) { ss.Speak(lblQ9.Text); } else if (current_tab == 10) { ss.Speak(lblQ10.Text); } else if (current_tab == 11) { if(numOftry == 1) { this.chart1.Series["Correct Ans"].Points.AddXY("First try", correctAns); this.chart1.Series["Wrong Ans"].Points.AddXY("First try", wrongAns); } else if(numOftry == 2) { this.chart1.Series["Correct Ans"].Points.AddXY("Second try", correctAns); this.chart1.Series["Wrong Ans"].Points.AddXY("Second try", wrongAns); } else if (numOftry == 3) { this.chart1.Series["Correct Ans"].Points.AddXY("Third try", correctAns); this.chart1.Series["Wrong Ans"].Points.AddXY("Third try", wrongAns); } else if (numOftry == 4) { this.chart1.Series["Correct Ans"].Points.AddXY("Fourth try", correctAns); this.chart1.Series["Wrong Ans"].Points.AddXY("Fourth try", wrongAns); } else if (numOftry == 5) { this.chart1.Series["Correct Ans"].Points.AddXY("Fifth try", correctAns); this.chart1.Series["Wrong Ans"].Points.AddXY("Fifth try", wrongAns); } nexttab = true; numOftry++; current_tab = 0; Question1(); Question2(); Question3(); Question4(); Question5(); Question6(); Question7(); Question8(); Question9(); Question10(); } private void red_Click(object sender, EventArgs e) { getvalue(true, true); Question1(); red.BackColor = Color.Black; this.red.Padding = new System.Windows.Forms.Padding(5); } private void green_Click(object sender, EventArgs e) { getvalue(false, true); Question1(); green.BackColor = Color.Black; this.green.Padding = new System.Windows.Forms.Padding(5); } private void blue_Click(object sender, EventArgs e) { getvalue(false, true); Question1(); blue.BackColor = Color.Black; this.blue.Padding = new System.Windows.Forms.Padding(5); } private void yellow_Click(object sender, EventArgs e) { getvalue(false, true); Question1(); yellow.BackColor = Color.Black; this.yellow.Padding = new System.Windows.Forms.Padding(5); }




推荐答案

您好Weishen0130,

Hi Weishen0130,

一个原因是你使用了大量的if / else条件。因此,当您单击下一个按钮时,程序会检查所有条件,因此需要时间。为了获得更好的性能,我建议使用"switch / case"。而不是"if / else"。

One reason is you are using lots of if/else condition. So when your next button is clicked program checkes all the condition so it takes time. For better performance I would suggest to use "switch/ case" instead of "if/else".


这篇关于当我点击下一个按钮时,为什么我的程序看起来有点迟钝?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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