I know this seems like a Basic question but I can not seem to figure this out. I have two forms in this project "Login" and "Config". Login is the base form that loads. Inside the Login_Load function I try and read the config file for the application. If the application is not avaialble I then open the Config form and want to hide the Login form. The config form does open but I can not seem to hide the Login form. I added some MessageBox's so I could see whats going on and it DOES hide the login form inside the ReadConfig function but after that function completes and it continues on the Login_Load the form displays again. Can someone help me figure out what is going on?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace BoxOffice
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private void Login_Load(object sender, EventArgs e)
{
labLoginStatus.Text = "";
ReadConfig();
}
private bool IsConfigAvailable()
{
if (File.Exists(Application.StartupPath.ToString() + "\\config.xml"))
{
return true;
}
return false;
}
private bool ReadConfig()
{
if (IsConfigAvailable())
{
// Config file is available so lets read it
return true;
}
else
{
// Config file is not avaialble so lets display the config edit form
Config frmConfig = new Config();
frmConfig.Show();
this.Visible = false;
this.Hide();
return false;
}
}
}
}

1 answers
Try changing the ReadConfig() method to this:
private bool ReadConfig()
{
if (IsConfigAvailable())
{
// Config file is available so lets read it
return true;
}
else
{
// Config file is not available so lets display the config edit form
Config frmConfig = new Config();
this.Hide();
frmConfig.ShowDialog();
return false;
}
answered 2 years ago by:
17279
Thanks that worked... but why? The difference I see is that I start to open the config... close the current form.. then instead of show, use showdialog
answered 2 years ago by:
0
The reason why that works is, because, when you call frmConfig.ShowDialog(), the method doesn't return until the Config form is closed. The Load eventhandler then resumes and when it finishes the Login form becomes visible automatically. In fact, it wasn't necessary to call this.Hide() because the Login form wasn't even visible at that stage.
As you had it before, calling frmConfig.Show() made the Config form visible but control was then returned immediately to the Load eventhandler. Setting tthe Visible property to false and calling the Hide() method had no effect because the Login form wasn't yet visible but when the Load eventhandler ended it became automatically visible as well as the Config form.
answered 2 years ago by:
17279
This post was imported from csharpfriends, if you have a similiar question please ask it again.
All previous members have been migrated, hope you enjoy the new platform!