简体   繁体   English

隐藏C#表格的麻烦

[英]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. 该表格只是在等待USB输入并进入下一个窗口。 (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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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