如何使用if else构造 [英] How to use if else construct
问题描述
你好,我想在表单/按钮上使用if else构造.在这种情况下,在我有一个文本框,保存按钮和开始按钮的表单上,保存按钮保存了通过文本框提供的用户输入,现在当用户单击开始按钮时,该按钮将打开另一个表单.我希望使用开始"按钮来确保用户单击了保存"按钮.
代码示例会有所帮助....
在此先感谢
hello, i want to use the if else construct on forms/buttons. here is the situation, on a form i have a textbox,save button and start button, the save button saves user input that has been provided through the textbox, now when the user clicks the start button,the button opens another form. i want the start button to make sure that the user has clicked the save button.
code samples would help....
thanks in advance
推荐答案
让我们使用两个按钮cmdSave和cmdStart(用于保存"和开始"按钮).
我会有一个类级别的布尔标志(让我们称之为标志).
在cmdSave click方法上,我将此标志设置为true
.
Lets take two buttons cmdSave and cmdStart (for the Save and Start buttons).
I would have a class level boolean flag (lets call it flag).
On the cmdSave click method, I would set this flag totrue
.
public void OncmdSaveClick(object sender, EventArgs e)
{
flag = true;
}
每当单击cmdStart按钮时,我都会检查该标志.只有设置了此标志(之前已单击保存"按钮,我才能运行该表单).如果没有,我将退出该方法.
Whenever the cmdStart button is clicked I would check for this flag. Only if this flag is set (the Save button had been clicked earlier, I would run the form). If not, I would just step out of the method.
public void OncmdStartClick(object sender, EventArgs e)
{
//Call the new form if the flag is set
if (flag)
{
CallNewForm(); //Calling the new form
}
}
这样,您将确保至少单击一次保存"按钮.
This way, you will be sure the Save button has been clicked at least once.
从我收集的信息中:
-在表单加载中,将表单级别变量isSaved
设置为false
:
From what I gathered:
- in your form load, set a form level variableisSaved
tofalse
:
private bool isSaved = false;
-在保存"按钮中单击处理程序,更改变量的状态:
- in the save button click handler, change the state of the variable:
isSaved = true;
-在开始"按钮单击处理程序中,检查isSaved
是true
:
- in the start button click handler, check that isSaved
is true
:
if (!isSaved) [
System.Windows.MessageBox.Show("Save first or something");
return;
}
// do the actual start code...
这篇关于如何使用if else构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!