【发布时间】:2015-08-13 04:42:47
【问题描述】:
我在下面粘贴了我的代码中最重要的部分。
如您所见,我希望使用多个表单。但这就是我的表单的行为方式:
它会打开 Selector,当我按下第二个按钮时,它会找到 .ini 文件并打开 ExplorerForm,但 Selector 仍处于打开状态。我当然不想那样。我无法单击选择器,我只是听到错误声音并且资源管理器窗口闪烁。当我关闭资源管理器时,资源管理器和选择器都会关闭。 但是现在路径表单打开了......
当我按下选择器中的其他按钮之一时,它找不到 .INI 文件(没错)并打开路径表单(并以正确的方式关闭它)。
我已经使用了搜索,甚至在那里实现了其中一个答案:
How I can close a 1st form without closing the full application?
我的 Program.cs:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Selector());
选择器.cs:
private void button1_Click(object sender, EventArgs e)
{
Path pathForm = new Path(0);
this.Hide();
pathForm.ShowDialog();
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
Path pathForm = new Path(1);
this.Hide();
pathForm.ShowDialog();
this.Close();
}
private void button3_Click(object sender, EventArgs e)
{
Path pathForm = new Path(2);
this.Hide();
pathForm.ShowDialog();
this.Close();
}
Path.cs:
public Path(int currGame)
{
intGame = currGame;
if(MyIni.KeyExists("Path"+intGame))
{
var GamePath = MyIni.Read("Path"+intGame);
if(Directory.Exists(GamePath))
{
if (Directory.GetFiles(
GamePath, gameEXE(intGame), SearchOption.TopDirectoryOnly).Count() > 0)
{
InitializeComponent();
Explorer explorerForm = new Explorer();
this.Hide();
explorerForm.ShowDialog();
this.Hide();
}
}
}
InitializeComponent();
label1.Text = label1.Text + " " + gameString(currGame) + "!";
RegistryKey rk = Registry.LocalMachine;
RegistryKey sk1 = rk.OpenSubKey(
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 12100");
// III Steam
RegistryKey sk2 = rk.OpenSubKey(
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 12110");
// Vice City Steam
RegistryKey sk3 = rk.OpenSubKey(
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 12120");
// San Andreas Steam
if(intGame == 0)
{
if (sk1 != null)
{
if (sk1.GetValueNames().Contains("InstallLocation")
&& sk1.GetValue("InstallLocation").ToString() != "")
{
textBox1.Text = sk1.GetValue("InstallLocation").ToString();
}
}
}
else if(intGame == 1)
{
if(sk2 != null)
{
if(sk2.GetValueNames().Contains("InstallLocation")
&& sk2.GetValue("InstallLocation").ToString() != "")
{
textBox1.Text = sk2.GetValue("InstallLocation").ToString();
}
}
}
else if (intGame == 2)
{
if (sk3 != null)
{
if (sk3.GetValueNames().Contains("InstallLocation")
&& sk3.GetValue("InstallLocation").ToString() != "")
{
textBox1.Text = sk3.GetValue("InstallLocation").ToString();
}
}
}
}
【问题讨论】:
-
您能否解释一下您对打开/关闭每个表单时的期望是什么?
-
无论如何,你应该使用
Form.Show()方法而不是ShowDialog()。由于ShowDialog()之后的剩余代码将在对话框关闭后执行,这似乎不是您的期望。 -
我看到您在单击其中一个按钮时正在关闭
Selector表单。由于这是主要形式(由于Application.Run(new Selector())行,这将杀死整个应用程序。这是故意的吗?也许您可以详细说明一下您的意图。因为不清楚您是否希望能够打开是否有多个窗口,主窗口(@987654330@)是否保持打开,等等... -
@MangoWong 我不希望表单关闭所有其他表单。
-
@MangoWong 当我关闭 Selector 表单时,它应该只关闭 swelector 表单,但不是所有表单:-) 我不想要一个关闭所有其他表单的 MAIN 表单:-)