移动表单时ObjectDisposedException

时间:2016-07-28 09:19:39

标签: c# winforms

我制作了自定义表单。

现在我正在尝试实现该功能,在按住标题栏的同时拖动表单。但它总是抛出这个例外:

  

system.ObjectDisposedException

有谁知道问题是什么?

这是我的代码:

    [DllImportAttribute("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    [DllImportAttribute("user32.dll")]
    public static extern bool ReleaseCapture();

    Form form;
    Button buttonCancel;
    Button buttonOk;
     DialogResult InputBox(string title, string promptText, ref string value)
    {

        form = new Form();
        buttonCancel = new Button();
        buttonOk = new Button();

        Label label = new Label();
        TextBox textBox = new TextBox();

        Panel titlebar = new Panel();
        Label lbltitle = new Label();
        Panel pnlexit = new Panel();
        Label lblexit = new Label();


        form.Text = title;
        label.Text = promptText;
        textBox.Text = value;

        buttonOk.Text = "OK";
        buttonCancel.Text = "Cancel";
        buttonOk.DialogResult = DialogResult.OK;
        buttonCancel.DialogResult = DialogResult.Cancel;

        label.SetBounds(9, 20, 372, 13);
        textBox.SetBounds(12, 36, 372, 20);
        buttonOk.SetBounds(228, 72, 75, 23);
        buttonCancel.SetBounds(309, 72, 75, 23);
        buttonCancel.Location = new Point(250, 100);
        buttonCancel.Parent = form;
        buttonCancel.BackColor = ColorTranslator.FromHtml("#CDDC39");
        buttonCancel.FlatStyle = FlatStyle.Flat;
        buttonCancel.FlatAppearance.BorderSize = 0;
        buttonCancel.MouseClick += ButtonCancel_MouseClick;

        buttonOk.Location = new Point(170, 100);
        buttonOk.Parent = form;
        buttonOk.BackColor = ColorTranslator.FromHtml("#CDDC39");
        buttonOk.FlatStyle = FlatStyle.Flat;
        buttonOk.FlatAppearance.BorderSize = 0;


        form.Height = 150;
        form.Width = 350;
        form.BackColor = ColorTranslator.FromHtml("#455A64");
        form.FormBorderStyle = FormBorderStyle.None;
        form.AutoSize = false;
        form.StartPosition = FormStartPosition.CenterScreen;
        form.AcceptButton = buttonOk;
        form.CancelButton = buttonCancel;


        titlebar.Width = form.Width;
        titlebar.Height = 25;
        titlebar.BackColor = ColorTranslator.FromHtml("#37474F");
        titlebar.Parent = form;
        titlebar.Controls.Add(pnlexit);
        titlebar.MouseDown += Titlebar_MouseDown;

        lblexit.Text = "X";
        lblexit.Font = new Font("Arial", 9.5f, FontStyle.Bold);
        lblexit.TextAlign = ContentAlignment.MiddleCenter;
        lblexit.Dock = DockStyle.Fill;
        lblexit.MouseClick += lblexit_click;

        pnlexit.Height = titlebar.Height - 10;
        pnlexit.Width = 30;
        pnlexit.BackColor = ColorTranslator.FromHtml("#CDDC39");
        pnlexit.Location = new Point(titlebar.Width - 33, 0);
        pnlexit.Parent = titlebar;
        pnlexit.Controls.Add(lblexit);

        lbltitle.Parent = titlebar;
        lbltitle.Text = title;
        lbltitle.ForeColor = ColorTranslator.FromHtml("#CDDC39");
        lbltitle.Font = new Font("Arial",  9.5f, FontStyle.Bold);
        lbltitle.Location = new Point(5, 3);

        label.AutoSize = true;
        label.Location = new Point(25, 50);
        label.Parent = form;
        label.ForeColor = Color.White;

        textBox.Parent = form;
        textBox.Location = new Point(25, 65);
        textBox.Width = 300;

        DialogResult dialogResult = form.ShowDialog();
        value = textBox.Text;
        Console.WriteLine("Value = " + value);
        return dialogResult;


    }

    private void Titlebar_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            ReleaseCapture();
            SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
        }
    }

    private  void ButtonCancel_MouseClick(object sender, MouseEventArgs e)
    {
       // throw new NotImplementedException();
    }

    private  void lblexit_click(object sender, MouseEventArgs e)
    {
        form.Close();
    }

0 个答案:

没有答案
相关问题