In a Windows application, I’m creating two forms, named Form1 and Configuration_Form. First, I’m loading the Configuration_Form. In this form, I check the connection to a txt file. If the txt file has some data, it means that it will load Form1.
Otherwise Configuration_Form won’t load anything.
Now I have a problem, suppose the txt file has some data, then it means that it’s going to load Form1 and open another empty form. I want to show only the Form1, not the empty form. How can I block that empty form?
This is my partial code:
public partial class Configuration_Form : Form
{
Form1 form = new Form1();
public Configuration_Form()
{
StreamReader tr = new StreamReader(Application.StartupPath + "\\" + "config.txt");
string config = tr.ReadToEnd();
if (config.Replace("\r\n", string.Empty) == "")
{
tr.Close();
InitializeComponent();
}
else
{
form.Show();
}
}
///////////////////////////
private void btn_submit_Click(object sender, EventArgs e)
{
try
{
if ((txtIP.Text != "") && (txtdatabase.Text != "") && (txtuser.Text != "") && (txtpass.Text != ""))
{
StreamWriter sr = new StreamWriter(Application.StartupPath + "\\" + "config.txt");
sr.Write(Convert.ToString((txtIP.Text) + ";" + (txtport.Text) + ";" + (txtdatabase.Text) + ";" + (txtuser.Text) + ";" + (txtpass.Text)));
sr.WriteLine();
sr.Close();
this.Hide();
form.Show();
}
else
{
DialogResult msg = MessageBox.Show("All are mandatory fileds!", "SBS-BIO-CONFIG Administrator", MessageBoxButtons.OK, MessageBoxIcon.Stop);
if (Convert.ToBoolean(msg) == true)
{
this.Show();
}
}
}
catch (Exception e1)
{
MessageBox.Show("'" + e1.Message + "'");
}
}
Here is the Form1 code:
public partial class Form1 : Form
{
MySqlConnection con;
MySqlCommand cmd;
MySqlDataAdapter DA;
MySqlDataReader DR;
DataSet DSS;
#region Form_Load
public Form1()
{
InitializeComponent();
}
Here is my Program.cs code:
namespace BIO_PUNCH_UPDATE
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Configuration_Form());
}
}
}
Please, help me to fix this error.
I suppose you need to load one of the forms depending on the config data. If so then here is the code:
Also if you need config data in one of the forms you can pass config string as constructor parameter.