我的代码如下,当我运行程序时,首先弹出Login的模式窗体,当我单击取消时,就会报错,这个错误不知道怎么解决,还有就是如果我在主窗体关闭前弹出一个对话框(模式窗体),询问是否要关闭此窗体,单击“否”时,主窗体还是关闭了。请各位帮帮忙。
using System;
using System.Collections.Generic;
using System.Windows.Forms;namespace NetBar
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
LoginForm a = new LoginForm();
a.ShowDialog(); if (DBHelper.islogin == true)
{
Application.Run(new MainForm());
} }
}
}“正试图在 OS 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码,这样做会导致应用程序挂起。”
using System;
using System.Collections.Generic;
using System.Windows.Forms;namespace NetBar
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
LoginForm a = new LoginForm();
a.ShowDialog(); if (DBHelper.islogin == true)
{
Application.Run(new MainForm());
} }
}
}“正试图在 OS 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码,这样做会导致应用程序挂起。”
if(a.islogin==dialogResult.OK)
{
Application.Run(new MainForm()); }
{
// Determine if text has changed in the textbox by comparing to original text.
if (textBox1.Text != strMyOriginalText)
{
// Display a MsgBox asking the user to save changes or abort.
if(MessageBox.Show("Do you want to save changes to your text?", "My Application",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// Cancel the Closing event from closing the form.
e.Cancel = true;
// Call method to save file...
}
}
}
DialogResult drt = formLogin.ShowDialog();
if(drt == DialogResult.OK)
{
Application.Run(new MainForm());
}在login窗体里面
在前面做出判断的,如果可以登录就执行下一句的
this.DialogResult = System.Windows.Forms.DialogResult.OK;
想主窗体不关闭,在主窗体的FormClosing添加下一句
e.Cancel = true;
if (e.CloseReason == CloseReason.MdiFormClosing)
{
e.Cancel = true;
return;
}
if (flag == false)//为false则表明数据发生了改但没有保存
{
if (bb.FormCloseExt())
{
BTOk.PerformClick();
} }
//关闭前,提示是否保存
this.Dispose();
this.Close();
//当主窗体前发生
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
//弹出模式窗体,要求输入登录名及密码
InputPwdForm inputPwd = new InputPwdForm();
if (inputPwd.ShowDialog() == DialogResult.Cancel)
{
e.Cancel = true;
}
}
如果将上面的DialogResult.Cancel改成DialogResult.OK的话,主窗体是没有关闭,但是弹出模式时,选择“确定”(且验证通过)也不能关闭整个程序。若修改e.Cancel=false;问题又回来了。
要怎么改呢?
(我的代码如下)
//当主窗体前发生
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
//弹出模式窗体,要求输入登录名及密码
InputPwdForm inputPwd = new InputPwdForm();
if (inputPwd.ShowDialog() == DialogResult.Cancel)
{
e.Cancel = true;
}
}
如果将上面的DialogResult.Cancel改成DialogResult.OK的话,主窗体是没有关闭,但是弹出模式时,选择“确定”(且验证通过)也不能关闭整个程序。若修改e.Cancel=false;问题又回来了。
要怎么改呢?请各位多多帮忙。谢谢了。