Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正在验证用户';在继续执行程序之前,输入_C#_Winforms - Fatal编程技术网

C# 正在验证用户';在继续执行程序之前,输入

C# 正在验证用户';在继续执行程序之前,输入,c#,winforms,C#,Winforms,我创建了一个包含(目前)2个表单的程序 在第一种形式中,我要求用户提供一个文件。用户选择文件后,调用另一个表单,然后关闭当前表单 if语句指示用户在按下打开文件按钮时是否插入了文件,如果没有,则不会加载第二个表单 问题是,如果用户单击第一个表单(当前表单)上的关闭按钮,表单将关闭,并调用下一个表单 下一个表单的选项基于用户在第一个表单中的输入(其中要求用户选择一个文件),因此如果用户取消第一个表单时调用第二个表单,则会给第二个表单中的方法带来问题 关于如何处理“关闭”按钮有什么想法吗?如果要阻止

我创建了一个包含(目前)2个表单的程序

在第一种形式中,我要求用户提供一个文件。用户选择文件后,调用另一个表单,然后关闭当前表单

if
语句指示用户在按下打开文件按钮时是否插入了文件,如果没有,则不会加载第二个表单

问题是,如果用户单击第一个表单(当前表单)上的关闭按钮,表单将关闭,并调用下一个表单

下一个表单的选项基于用户在第一个表单中的输入(其中要求用户选择一个文件),因此如果用户取消第一个表单时调用第二个表单,则会给第二个表单中的方法带来问题


关于如何处理“关闭”按钮有什么想法吗?

如果要阻止关闭表单,可以处理该事件

bool cancel = true;
protected override void OnFormClosing(FormClosingEventArgs e)
{
    e.Cancel = cancel;
    base.OnFormClosing(e);
}
当关闭表单时,请记住将
cancel
更改为false。

表单上有一个名为“FormClosing”的事件

快速示例:

 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to quit?", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                e.Cancel = true;
            }

        }

建议使用
form.ShowDialog()
启动表单,它返回
DialogResult
。您应该检查它是
DialogResult.Ok
还是
form.DialogResult!=DialogResult.None
。在表单中,如果用户插入文件,则可以将
表单.DialogResult
明确设置为
DialogResult.Ok

只需使用自己的逻辑处理关闭事件即可

private void Form1_FormClosing_1(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show(text:"Are you sure you want to quit?",caption:string.Empty,buttons: MessageBoxButtons.YesNo) == DialogResult.No)
            {
                e.Cancel = true;
            }
        }

我假设您有一个打开文件对话框(允许用户选择一个文件),以及一个可能名为打开文件的按钮,用于将文件名传递到下一个表单。如果是这种情况,那么如果没有选择文件,您可以尝试禁用打开按钮

将以下代码视为所有逻辑发生的函数

private void BrowseFile()
{
//dlgopenfile is the name of Openfiledialog that allows the user to browse for a file.
//string filename is the name of selected file if any.
//Form2 is the next form.

try
{

switch (dlgopenfile.ShowDialog())
{
case DialogResult.OK://If Ok(Yes) button is pressed on Openfiledialog.
filename = dlgopenfile.FileName;
break;

case DialogResult.Cancel://If Cancel button is pressed on Openfiledialog.
filename = "";
break;
}

if (filename.Length >= 1)
{
if (File.Exists(filename) == true)
{
ButtonOpenFile.Enabled = true;
}
else
{
ButtonOpenFile.Enabled = false;
throw new FileNotFoundException("The file you selected does not exist.");
}
}
}
catch (FileNotFoundException ex)
{
MessageBox.Show(ex.Message, "Form1", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
如果用户试图在会话中期关闭表单,则会出现下一个函数

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
switch (MessageBox.Show("Do you want to exit ?", "Form1", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk))
{
case DialogResult.Yes:
this.Close();
break;

case DialogResult.No:
e.Cancel = true;
break;
}
}
catch (Exception)
{
//Exception handling code goes here.
}
}
最后,下面的函数使用所选文件作为参数调用Form2的构造函数

private void ButtonOpenFile_Click(object sender, EventArgs e)
{
//This Button is enabled only if the file has been selected and if its exists.
Form2 form2 = new Form2(filename);//filename is the name of selected file,as decided in function BrowseFile().
this.Close();//Close Form1.
form2.ShowDialog();//Show Form2 as modal dialog.
}

希望它能帮助你实现你所需要的。还有什么,请让我知道。

这与Jurriaan Pijpers的答案基本相同,该答案在本次发布前一小时发布。你还有什么要补充的吗?