不允许与后台表单交互

本文关键字:交互 表单 后台 不允许 | 更新日期: 2023-09-27 18:11:51

第一次运行应用程序时,打开了两个表单。最上面的表单需要优先,并且不允许与后台表单进行任何交互。我已经尝试过这里引用的ShowDialog(),但是这隐藏了我不希望做的背景形式。有办法做到这一点吗?

public Form1()
        {
            InitializeComponent();
            if (!fileexists(@"c:'Management Tools'Absence Tracker'bin'data'tbase.skf"))
            { firstrunactions(); }
        }
void firstrunactions()
        {
            //open the get-started form and invite user to populate serialisable objects
            firstrun frwindow = new firstrun();
            frwindow.ShowDialog();
        }

不允许与后台表单交互

当您使用.ShowDialog()时,包含方法的执行将暂停,直到您关闭新打开的窗口。因此,请确保在调用.ShowDialog()之前完成其他所有操作。否则,程序就会卡在这个方法中。如果你在后台窗口显示之前调用.ShowDialog()会导致问题。
但是在这里使用.ShowDialog()是完全正确的,并且具有正确的功能。

示例如何这样做(导致与您的问题相同的行为):

    public Form1()
    {
        InitializeComponent();
        //this is the wrong place for showing a child window because it "hides" its parent
        Form frwindow = new Form();
        frwindow.ShowDialog(this);
    }

它起作用的神奇地方:

    private void Form1_Shown(object sender, EventArgs e)
    {
        Form frwindow = new Form();
        frwindow.ShowDialog(this);
    }

编辑:在您的情况下,将if(!fileexistst...)移动到Form1_Shown() -event中就足够了。

Try with frwindow.ShowDialog(this);或者"this"将另一种形式作为参数传递。也移动这部分if (!fileexists(@"c:'Management Tools'Absence Tracker'bin'data'tbase.skf")) { firstrunactions(); } }