fishmong3r
fishmong3r

Reputation: 1434

How to hide current form in Load()

I have an EULA form, this is the first form to be shown. Though if a user ticks a checkBox my program creates a hidden txt file in the same directory. On program start if the file exists I don't want the EULA form to be shown but the main Form1.

private void EULA_Load(object sender, EventArgs e)
{
    string path = Path.GetDirectoryName(Application.ExecutablePath);
    string file = path + "EULA.txt";
    if (File.Exists(file))
    {
        this.Hide();
        var form1 = new Form1();
        form1.Closed += (s, args) => this.Close();
        form1.Show();
     }
}

On ButtonClick I could use the code within the if clause above successfully.

The code above opens both the EULA and Form1. Why?

EDIT: I tried it in Program.cs in Main() as advised:

static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        string path = Path.GetDirectoryName(Application.ExecutablePath);
        string file = path + "Mailer_EULA.txt";
        if (File.Exists(file))
        {
            Application.Run(new Form1());
        }
        else
        {
            Application.Run(new EULA());
        }
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
    }

But this opens Form1 whether the file exists or not.

Upvotes: 0

Views: 101

Answers (1)

Tjaart van der Walt
Tjaart van der Walt

Reputation: 5179

Why not do the check on static void Main() in the Program class?

Try this:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        string path = Path.GetDirectoryName(Application.ExecutablePath);
        string file = Path.Combine(path, "EULA.txt");
        if (File.Exists(file))
        {
            var form1 = new Form1();
            form1.Closed += (s, args) => this.Close();
            form1.Show();
        }
        else 
        {
            var eula = new EULA();
            eula.Show();
        }
    }
}

Upvotes: 1

Related Questions