gpt4 book ai didi

c# - 如何防止第一次在 C# 中运行的事件

转载 作者:行者123 更新时间:2023-12-03 21:52:10 25 4
gpt4 key购买 nike

我有一个 Combobox 有这样的事件:

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 = "گوساله";
}
}

}

但是我在加载表单中为这个组合框设置了一个默认索引,

现在的问题是,当我在程序打开之前运行程序时,消息框先显示,如果不在第一次运行,是否有任何方法可以阻止逻辑?

//-----

正如你所说,我把代码改成了这样:

    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 = "گاو شیری";
}

我该怎么办?

最佳答案

您要么在加载组合框后注册事件处理程序,要么在第一次运行时构建检查,如下所示:

private bool firstRun = true;

在你的方法中:

if (firstRun)
{
firstRun = false;
return;
}

关于c# - 如何防止第一次在 C# 中运行的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36176779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com