1

Hello im pretty new to c# just downloaded VS2012 and created my new Application, but im getting a really strange Exception, I guess its my fault but again im really new to this

the exception occours when closing form2 that form1 created. this only occurs when an object its placed on form2.

I just got 2 forms with a button on each, button on form1 calls form2, when form2 is closed I show again form1, after a few seconds it throws InvalidOperationException on line base.Dispose

here is the code that trhows the exception

protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing); // here is the exceptjion
        }

Here is form1

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

        private void button1_Click(object sender, EventArgs e)
        {
            var frm = new Form2(this);
            frm.Show(this);
            this.Hide();
        }
    }

Here is form2

public partial class Form2 : Form
{
    private Form frm;

    public Form2(Form frm) : this()
    {
        this.frm = frm;
    }

    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        frm.Show();
        this.Close();
    }
}

Here is the stacktrace

No se controló System.InvalidOperationException
  HResult=-2146233079
  Message=Operación no válida a través de subprocesos: Se tuvo acceso al control 'button1' desde un subproceso distinto a aquel en que lo creó.
  Source=System.Windows.Forms
  StackTrace:
       en System.Windows.Forms.Control.get_Handle()
       en System.Windows.Forms.Control.get_InternalHandle()
       en System.Windows.Forms.Control.DestroyHandle()
       en System.Windows.Forms.Control.Dispose(Boolean disposing)
       en System.Windows.Forms.ButtonBase.Dispose(Boolean disposing)
       en System.ComponentModel.Component.Dispose()
       en System.Windows.Forms.Control.Dispose(Boolean disposing)
       en System.Windows.Forms.Form.Dispose(Boolean disposing)
       en PruebaExceocion.Form2.Dispose(Boolean disposing) en c:\Users\Alex\Documents\Visual Studio 2012\Projects\PruebaExceocion\PruebaExceocion\Form2.Designer.cs:línea 20
       en System.ComponentModel.Component.Dispose()
       en System.Windows.Forms.Form.WmClose(Message& m)
       en System.Windows.Forms.Form.WndProc(Message& m)
       en System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       en System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       en System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  InnerException: 
4

2 回答 2

0

取消它并隐藏它..像这样:

private void Form2_FormClosing(object sender, FormClosingEventArgs e) {
    e.Cancel = true;
    this.Hide();
    frm.Show();
}
于 2012-12-19T03:07:23.377 回答
0

如果您只是简单地使用 2 个表单进行编程,那么从另一个按钮调用一个也可以像

    //On form1
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.Show();
    }

仅此而已,您可以随时关闭第二个表单而不会出现任何错误(我在 vs12 上对此进行了测试)。每次单击按钮时,它将创建一个新的 Form2 实例。因此,如果您只希望每次单击都显示一个相同的 form2 实例,这会变得有点棘手。

于 2012-12-19T03:14:23.697 回答