0

我想取消选中其他表单关闭时的 ContextMenuStrip 项目。这是我的代码:

表格1:

   public bool ischecked
    {
        get { return openForm1ToolStripMenuItem.Checked; }
        set { openForm1ToolStripMenuItem.Checked = value; }
    }

    bool isForm2Open = false;
    bool isForm3Open = false;

    private void openForm1ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (!isForm2Open)
        {
            Form2 frm2 = new Form2();
            frm2.Show();
            isForm2Open = true;
            openForm1ToolStripMenuItem.Checked = true;
        }
        else
        {
            openForm1ToolStripMenuItem.Checked = false;
            isForm2Open = false;
        }
    }

表格2:

private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
    // Code for Unckeck openForm1ToolStripMenuItem
}

为什么 ischecked 不能以其他形式访问?

4

3 回答 3

1
private void openForm1ToolStripMenuItem_Click(object sender, EventArgs e) {
    ToolStripMenuItem item = sender as ToolStripMenuItem;
    if (!isForm2Open)
    {
        Form2 frm2 = new Form2();
        frm2.FormClosed += (s,ev) => {
           item.Checked = false;
           isForm2Open = false;
        };
        frm2.Show();
        isForm2Open = true;
        openForm1ToolStripMenuItem.Checked = true;
    }
    else
    {
        openForm1ToolStripMenuItem.Checked = false;
        isForm2Open = false;
    }
}
于 2013-08-30T16:53:59.997 回答
0

您可以像这样将表单显示为对话框

 frm2.ShowDialog();

或者您必须在 form2 中创建一个事件。当表单关闭时触发。您将在表格 1 中注册该事件,然后取消选中您的框。

在表格 2 中声明事件,如

 public event EventHandler onFormClosed;

然后在 Form2 的结束事件中

 private void Form2_FormClosed(object sender, FormClosedEventArgs e)
 {
    if(onFormClosed!= null)
          onFormClosed(this,EventArgs.Empty);
 }

然后在你的表格 1

 Form2 frm2 = new Form2();
 frm.onFormClosed += frm_onFormClosed;
 frm2.Show();

这将创建一个这样的方法

 private void frm_onFormClosed(object sender, EventArgs e)
 {
    if (this.InvokeRequired)
    {   
        this.Invoke(() =>  openForm1ToolStripMenuItem.Checked = false);
    }
    else
    {
       openForm1ToolStripMenuItem.Checked = false;
    }

 }
于 2013-08-30T16:53:28.497 回答
0

您需要在第二个表单中引用第一个表单才能更改它的属性。此外,上面的两个答案是完成此任务的更好方法,即使他们不直接回答所提出的问题。:)

表格1:

   public bool ischecked
    {
        get { return openForm1ToolStripMenuItem.Checked; }
        set { openForm1ToolStripMenuItem.Checked = value; }
    }

    bool isForm2Open = false;
    bool isForm3Open = false;

    private void openForm1ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (!isForm2Open)
        {
            Form2 frm2 = new Form2();
            frm2.MainForm = this;
            frm2.Show();
            isForm2Open = true;
            openForm1ToolStripMenuItem.Checked = true;
        }
        else
        {
            openForm1ToolStripMenuItem.Checked = false;
            isForm2Open = false;
        }
    }

表格2:

public Form MainForm { get; set; }

private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
  this.MainForm.ischecked = false;
}
于 2013-08-30T16:57:09.483 回答