在继续执行程序之前验证用户的输入

时间:2013-09-03 14:33:30

标签: c# winforms

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

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

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

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

下一个表单的选项基于用户在第一个表单中的输入(要求用户选择文件),因此如果在用户取消第一个表单时调用第二个表单,则会产生问题对于第二种形式的方法。

有关如何处理关闭按钮的任何想法?

5 个答案:

答案 0 :(得分:1)

如果您想阻止关闭表单,可以处理事件

bool cancel = true;
protected override void OnFormClosing(FormClosingEventArgs e)
{
    e.Cancel = cancel;
    base.OnFormClosing(e);
}

在关闭表单时,请务必将cancel更改为false。

答案 1 :(得分:1)

表单上有一个名为“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;
            }

        }

答案 2 :(得分:1)

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

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

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.
}

希望它可以帮助你实现你所需要的。还有更多,请告诉我。

答案 3 :(得分:0)

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

答案 4 :(得分:0)

您可以使用自己的逻辑处理close事件

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;
            }
        }