我粘贴了下面代码中最重要的部分。
正如您所看到的,我想使用多个表单。但这就是我的表格行为:
打开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());
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();
}
}
}
}
答案 0 :(得分:1)
尝试以这种方式修改Selector.cs
的相关代码:
private void button1_Click(object sender, EventArgs e)
{
this.StartPathForm(1);
}
private void button2_Click(object sender, EventArgs e)
{
this.StartPathForm(2);
}
private void button3_Click(object sender, EventArgs e)
{
this.StartPathForm(3);
}
private void StartPathThread(int currGame)
{
System.Threading.Thread pathThread = new System.Threading.Thread(PathThreadStart);
pathThread.SetApartmentState(System.Threading.ApartmentState.STA);
pathThread.Start(currGame);
this.Close();
}
private void PathThreadStart(object currGame) {
Application.Run(new Path((int) currGame));
}
通过此修改,将初始化新线程并在其上运行Path
表单。然后,Selector
表单将立即关闭。希望这适合您的使用。