Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 关闭特定的WinForm?_C#_Winforms - Fatal编程技术网

C# 关闭特定的WinForm?

C# 关闭特定的WinForm?,c#,winforms,C#,Winforms,假设我以以下方式打开一个表单: FormSomething FormSomething = new FormSomething(SomethingId); FormSomething.Show(); 在我的代码中,有可能同时打开许多表单。如何关闭FormSomething的特定实例 FormSomething的每个实例都有一个与之关联的Id 编辑:我想我真正想要的是能够从外部关闭FormSomething的特定实例 我真的很感激任何提示D表单类上有一个Close方法,您可以调用它 听起来您只需

假设我以以下方式打开一个表单:

FormSomething FormSomething = new FormSomething(SomethingId);
FormSomething.Show();
在我的代码中,有可能同时打开许多表单。如何关闭FormSomething的特定实例

FormSomething的每个实例都有一个与之关联的Id

编辑:我想我真正想要的是能够从外部关闭FormSomething的特定实例


我真的很感激任何提示D

表单类上有一个Close方法,您可以调用它

听起来您只需要保留一份已打开表单的列表,以便以后可以参考:

    List<FormSomthing> _SomethingForms = new List<FormSomething>();

    void DisplaySomething()
    {
        FormSomething FormSomething = new FormSomething(SomethingId);
        _SomethingForms.Add(FormSomething);
        FormSomething.Show();
    }

    void CloseThatSucka(int somethingId)
    {
        // You might as well use a Dictionary instead of a List (unless you just hate dictionaries...)
        var form = _SomethingForms.Find(frm => frm.SomethingId == somethingId);
        if(form != null)
        {
            form.Close();
            _SomethingForms.Remove(form);
        }
    }
List\u SomethingForms=newlist();
空显示某物
{
FormSomething FormSomething=新FormSomething(SomethingId);
_添加(FormSomething);
FormSomething.Show();
}
void CloseThatSucka(int somethingId)
{
//你也可以用字典代替列表(除非你讨厌字典…)
var form=\u SomethingForms.Find(frm=>frm.SomethingId==SomethingId);
if(form!=null)
{
form.Close();
_删除(形式);
}
}

表单类上有一个Close方法,您可以调用它

听起来您只需要保留一份已打开表单的列表,以便以后可以参考:

    List<FormSomthing> _SomethingForms = new List<FormSomething>();

    void DisplaySomething()
    {
        FormSomething FormSomething = new FormSomething(SomethingId);
        _SomethingForms.Add(FormSomething);
        FormSomething.Show();
    }

    void CloseThatSucka(int somethingId)
    {
        // You might as well use a Dictionary instead of a List (unless you just hate dictionaries...)
        var form = _SomethingForms.Find(frm => frm.SomethingId == somethingId);
        if(form != null)
        {
            form.Close();
            _SomethingForms.Remove(form);
        }
    }
List\u SomethingForms=newlist();
空显示某物
{
FormSomething FormSomething=新FormSomething(SomethingId);
_添加(FormSomething);
FormSomething.Show();
}
void CloseThatSucka(int somethingId)
{
//你也可以用字典代替列表(除非你讨厌字典…)
var form=\u SomethingForms.Find(frm=>frm.SomethingId==SomethingId);
if(form!=null)
{
form.Close();
_删除(形式);
}
}

只要跟踪它们就行了。字典是自然的收集对象。例如:

    Dictionary<int, Form2> instances = new Dictionary<int, Form2>();

    public void OpenForm(int id) {
        if (instances.ContainsKey(id)) {
            var frm = instances[id];
            frm.WindowState = FormWindowState.Normal;
            frm.Focus();
        }
        else {
            var frm = new Form2(id);
            instances.Add(id, frm);
            frm.FormClosed += delegate { instances.Remove(id); };
            frm.Show();
        }
    }
字典实例=新字典();
公共void OpenForm(int-id){
if(instances.ContainsKey(id)){
var frm=实例[id];
frm.WindowState=FormWindowState.Normal;
frm.Focus();
}
否则{
var frm=新表格2(id);
实例。添加(id,frm);
frm.FormClosed+=委托{instances.Remove(id);};
frm.Show();
}
}

只要跟踪它们就行了。字典是自然的收集对象。例如:

    Dictionary<int, Form2> instances = new Dictionary<int, Form2>();

    public void OpenForm(int id) {
        if (instances.ContainsKey(id)) {
            var frm = instances[id];
            frm.WindowState = FormWindowState.Normal;
            frm.Focus();
        }
        else {
            var frm = new Form2(id);
            instances.Add(id, frm);
            frm.FormClosed += delegate { instances.Remove(id); };
            frm.Show();
        }
    }
字典实例=新字典();
公共void OpenForm(int-id){
if(instances.ContainsKey(id)){
var frm=实例[id];
frm.WindowState=FormWindowState.Normal;
frm.Focus();
}
否则{
var frm=新表格2(id);
实例。添加(id,frm);
frm.FormClosed+=委托{instances.Remove(id);};
frm.Show();
}
}

一个小问题,如果(form!=null)测试中
删除
会更好。嘿,伙计们,好吧,我建立了所有的测试,但是它在form.Close()上崩溃了;我将得到错误消息后,拉我的代码出一次尝试,任何帮助将不胜感激,谢谢!跨线程操作无效:控件“FormSomething”是从创建它的线程以外的线程访问的。次要的一点是,如果(form!=null)
测试中
删除
会更好。嘿,伙计们,好的,我已经建立了所有的控件,但是它在form.Close()上崩溃了;我将得到错误消息后,拉我的代码出一次尝试,任何帮助将不胜感激,谢谢!跨线程操作无效:从创建控件的线程以外的线程访问控件“FormSomething”。