我有一个组合框,发生了这样的事件:
private void CowTypeSelect_SelectedIndexChanged(object sender, EventArgs e)
{
if (MessageBox.Show(" آیا مطمئن هستید","",
MessageBoxButtons.OKCancel,MessageBoxIcon.Warning) == DialogResult.OK)
{
NotGrazingradioButton.Checked = true;
if (CowTypeSelect.SelectedIndex == 0)
{
CowTypeDefaults.LactatingCow(this);
CowTypeVarlbl.Text = "گاو شیری";
}
else if (CowTypeSelect.SelectedIndex == 1)
{
CowTypeDefaults.DryCow(this);
CowTypeVarlbl.Text = "گاو خشک";
}
else if (CowTypeSelect.SelectedIndex == 2)
{
CowTypeDefaults.ReplacementHeifer(this);
CowTypeVarlbl.Text = "تلیسه جایگزین";
}
else
{
CowTypeDefaults.YoungCalf(this);
CowTypeVarlbl.Text = "گوساله";
}
}
}
但是我以加载形式为此Combox设置了默认索引,
现在的问题是,当我在甚至未打开messageBox的情况下运行该程序时,是否有任何方法可以防止第一次不运行该逻辑?
// -----
如您所说,我更改了代码,如下所示:
bool FirstRun = true;
private void CowTypeSelect_SelectedIndexChanged(object sender, EventArgs e)
{
if (FirstRun == true)
{
FirstRun = false;
return;
}
if (MessageBox.Show("آیا مطمئن هستید؟", "",
MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
NotGrazingradioButton.Checked = true;
if (CowTypeSelect.SelectedIndex == 0)
{
CowTypeDefaults.LactatingCow(this);
CowTypeVarlbl.Text = "گاو شیری";
}
else if (CowTypeSelect.SelectedIndex == 1)
{
CowTypeDefaults.DryCow(this);
CowTypeVarlbl.Text = "گاو خشک";
}
else if (CowTypeSelect.SelectedIndex == 2)
{
CowTypeDefaults.ReplacementHeifer(this);
CowTypeVarlbl.Text = "تلیسه جایگزین";
}
else
{
CowTypeDefaults.YoungCalf(this);
CowTypeVarlbl.Text = "گوساله";
}
}
但是现在的问题是这些代码不是第一次运行,我需要它们运行:
NotGrazingradioButton.Checked = true;
if (CowTypeSelect.SelectedIndex == 0)
{
CowTypeDefaults.LactatingCow(this);
CowTypeVarlbl.Text = "گاو شیری";
}
我应该怎么办 ?
似乎您不了解@Patrick Hofman的代码。
该firstRun
变量指示该功能是否在第一次执行。
下面的行表示如果是第一次执行,它将更改firstRun
为false
并在该return;
行之后停止执行代码。(我添加了一些评论以使您更清楚。)
// Put codes here if you want it to execute every time.
if (firstRun)
{
// Codes here execute at the first time only.
firstRun = false;
return;
}
//Codes here execute except the first time.
固定码
第一次运行以下行
NotGrazingradioButton.Checked = true;
if (CowTypeSelect.SelectedIndex == 0)
{
CowTypeDefaults.LactatingCow(this);
CowTypeVarlbl.Text = "گاو شیری";
}
和if (MessageBox.Show(...))
运行后的线条(第一次除外)
完整代码如下:
bool FirstRun = true;
private void CowTypeSelect_SelectedIndexChanged(object sender, EventArgs e)
{
if (FirstRun == true)
{
// Codes here execute at the first time only.
NotGrazingradioButton.Checked = true;
if (CowTypeSelect.SelectedIndex == 0)
{
CowTypeDefaults.LactatingCow(this);
CowTypeVarlbl.Text = "گاو شیری";
}
FirstRun = false;
return;
}
//Codes below execute except the first time.
if (MessageBox.Show("آیا مطمئن هستید؟", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
NotGrazingradioButton.Checked = true;
if (CowTypeSelect.SelectedIndex == 0)
{
CowTypeDefaults.LactatingCow(this);
CowTypeVarlbl.Text = "گاو شیری";
}
else if (CowTypeSelect.SelectedIndex == 1)
{
CowTypeDefaults.DryCow(this);
CowTypeVarlbl.Text = "گاو خشک";
}
else if (CowTypeSelect.SelectedIndex == 2)
{
CowTypeDefaults.ReplacementHeifer(this);
CowTypeVarlbl.Text = "تلیسه جایگزین";
}
else
{
CowTypeDefaults.YoungCalf(this);
CowTypeVarlbl.Text = "گوساله";
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句