在我的应用程序首次运行时,打开两个表单。最顶层的表单需要优先,并禁止在后台与表单进行任何交互。我已经尝试ShowDialog()
作为引用here,但是这隐藏了我不想做的背景中的表单。有没有办法实现这个目标?
public Form1()
{
InitializeComponent();
if (!fileexists(@"c:\Management Tools\Absence Tracker\bin\data\tbase.skf"))
{ firstrunactions(); }
}
void firstrunactions()
{
//open the get-started form and invite user to populate serialisable objects
firstrun frwindow = new firstrun();
frwindow.ShowDialog();
}
答案 0 :(得分:1)
当您使用.ShowDialog()
时,暂停执行包含方法,直到您关闭新打开的窗口。因此,在调用.ShowDialog()
之前,请务必先删除。否则你的程序会陷入这种方法。如果您在显示背景窗口之前调用.ShowDialog()
将导致问题
但是在这里使用.ShowDialog()
是完全正确的并且具有正确的功能。
示例 如何执行此操作(导致与问题相同的行为):
public Form1()
{
InitializeComponent();
//this is the wrong place for showing a child window because it "hides" its parent
Form frwindow = new Form();
frwindow.ShowDialog(this);
}
它工作的神奇之地:
private void Form1_Shown(object sender, EventArgs e)
{
Form frwindow = new Form();
frwindow.ShowDialog(this);
}
修改:在您的情况下,将if(!fileexistst...)
移动到Form1_Shown()
- 事件就足够了。
答案 1 :(得分:0)
尝试使用frwindow.ShowDialog(this);
或者#34;这个"将另一个表单作为参数传递。
同时移动此部分if (!fileexists(@"c:\Management Tools\Absence Tracker\bin\data\tbase.skf"))
{ firstrunactions(); }
}
在OnLoad覆盖。