简体   繁体   中英

Trouble of hiding Form C#

Previously what I did is:

this.Hide();
Newform a = new Newform();
a.Show();

Then problem should solve, but now it is not working. I tested if i use a button to hide the windows, it worked. But i do not want to show a button on the form.

The form is simply waiting for usb input and proceed to next window. (no button). This is my code:

private void Usbauthentication_Load(object sender, EventArgs e)
{
    Usbdetected();
    try
    {
    watcher.EventArrived += new EventArrivedEventHandler(this.WaitForUSBChangeEvent);
    watcher.Query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2");
    watcher.Start();
    }

    catch (ManagementException a)
    {
        Console.WriteLine(a);
    }
}

public void WaitForUSBChangeEvent(object sender, EventArrivedEventArgs e)
{
    this.Usbdetected();
}

public void Usbdetected()
{
    list = conn.Select();
    for (int i = 0; i < drives.Count(); i++)
    {
        drivelist.Add(drives[i].Name.Replace(@"\", ""));
        if (list.Contains(usb.getSerialNumberFromDriveLetter(drivelist[i])))
        {
            store = i;
            this.Hide();
            Login a = new Login();
            a.Show();
            break;
        }
    }
}

Something like this should do the trick:

 var form2 = new Form2();
            form2.Shown += (o, e) => this.Hide();
            form2.Show();

You also need to make sure you close the original form when the new form is closed:

form2.FormClosed += (o,e) => this.Close();

So in all, you need to write:

    Newform a = new Newform();
    a.Shown += (o, e) => this.Hide();
    a.FormClosed += (o, e) => this.Close();
    a.Show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM