自动关闭ShowDialog框

时间:2018-02-22 21:41:18

标签: c# winforms

我有两种形式,Form1和Form2。运行程序时,Form1是启动的表单。然后我想调用Form2,在Form1前面显示它,在Form2中执行一些操作,然后在没有任何输入的情况下自动关闭Form2,这样用户就可以继续使用Form1。

我尝试了以下内容:

string[] dobArrayKey = {"dob: "};
string[] dobArrayValue = {txtDob.Text};
string[] ptntNumArrayKey = { "PatientID: " };
string[] ptntNumArrayValue = { txtOfficeMR.Text};
string[] nameArrayKey = { "FirstName: ", "LastName: " };
string[] nameArrayValue = { txtFirstname.Text, txtLastname.Text };

List<List<string>> searchResults = new List<List<string>>();


Dictionary<string[], string[]> searchCriteria = new Dictionary<string[], string[]> 
{ 
    {dobArrayKey,dobArrayValue}
    ,{ptntNumArrayKey,ptntNumArrayValue}
    ,{nameArrayKey,nameArrayValue}
};

foreach (var item in searchCriteria)
{
    if (item.Value[0] != "" && item.Value[0] != null)
    {
        searchResults.Add(new List<string>());

        for (int x = 0; x <= item.Key.Count(); x++)
        {
            string strJSON = doPatientSearch(Convert.ToInt32(au.UserID)
                , Convert.ToInt32(Session["PracticeID"]), au.SessionID, item.Key[x].ToString() : item.Value[x].ToString() );         

            PatientSearchResponse ptLi = JsonConvert.DeserializeObject<PatientSearchResponse>(json2);

            foreach (PatientList3 patient in ptLi.PatientList)
            {
                searchResults[x].Add(patient.PatientNumber);
            }

        }
    }
}

public static string doPatientSearch(int UserID, int PracticeID, string SessionID, string PatientID = null,
        ,string first = null, string last = null, string dob = null,  string social = null)
{
    //search
}

当我尝试这个时,我得到一个带有“无法访问已处置对象”消息的ObjectDisposedException。我试过在网上搜索,但没找到任何东西。我尝试过使用this.Dispose(),以及使用.Show()代替.ShowDialog()。我正在努力以这种方式实现目标吗?

我已经考虑过让Form2成为自己的.exe并调用.exe作为替代方案,但我想在重写所有内容之前我会问StackOverflow的专家。

2 个答案:

答案 0 :(得分:3)

正如Markus所说,因为Form2构造函数调用了this.Close(),所以一旦行Form2 otherForm = new Form2();完成,Form2窗口就会关闭。

close()方法除了关闭窗口(以及任何拥有的窗口)之外还释放资源。因此,在以下行中,当调用otherForm.ShowDialog();时,它无法访问otherForm(已经处置)并抛出ObjectDisposedException。这解释了您遇到异常的原因。

要解决此问题,请从Form2构造函数中删除this.Close()。您仍然需要调用close()方法,但是在构造函数之外并在完成Form2中要执行的操作之后执行此操作。

例如:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        Form2 otherForm = new Form2();
        otherForm.Show(this);
        otherForm.DoStuff();

        //do more stuff here after Form2 has opened and closed
    }
}

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    public void DoStuff()
    {
        // do stuff here as you wish
        // close method goes here
    }
}

答案 1 :(得分:2)

你必须致电

//Do some stuff
this.close() 

以后 - 不在构造函数中 - 但是在Form.Activated事件中。

样品:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();

        var form2 = new Form2();
        form2.ShowDialog();

        //Stuff Done
        MessageBox.Show("Stuff Done");
    }
}


public partial class Form2 : Form {
    public Form2() {
        InitializeComponent();
        Activated += DoSomeStuff;
    }

    private async void DoSomeStuff(object sender, EventArgs e) {
        //Do You Stuff here
        await Task.Run(() => Thread.Sleep(2000));
        this.Close();
    }
}
相关问题