【问题标题】:Set contextmenustrip of a richtextbox control in mdi child from parent从父级的 mdi 子级中设置富文本框控件的 contextmenustrip
【发布时间】:2014-08-19 08:54:37
【问题描述】:

我想通过 mdi 子表单中的 mdi 父表单代码分配特定的富文本框 contextmenustrip,但它不起作用。 如何设置richtextbox1 使用contextMenuStrip2?

mdi 子项包含两个已创建的上下文菜单条,称为 contextMenuStrip1 和 contextMenuStrip2。 Richtextbox1 的默认值为 contextMenuStrip1。

使用以下代码, richTextBox1 文本属性已按预期更改,但是尝试更改 contextmenustrip 什么也没做。由于 childForm.Controls["richTextBox1"].ContextMenuStrip.Name = "contextMenuStrip2";.

Mdiparent.cs:

Form childForm = new Form1("contextMenuStrip2");
childForm.Show();

Form1.cs:

public Form1(String correctcontextmenu)
        {
            InitializeComponent();
            richTextBox1.ContextMenuStrip = correctcontextmenu;
        }

错误列表包含:

错误 1 ​​无法将类型“string”隐式转换为“System.Windows.Forms.ContextMenuStrip”

【问题讨论】:

    标签: c# .net richtextbox contextmenustrip


    【解决方案1】:

    如您所见,字符串不是 ContextMenuStrip。由于 ContextMenuStrips 是 ChildForm 私有的,因此您应该只传递一个标志来告诉表单它应该使用哪个菜单:

    public Form1(bool useOtherMenu)
    {
      InitializeComponent();
      if (useOtherMenu)
      {
        richTextBox1.ContextMenuStrip = contextMenuStrip2;
      }
    }
    

    然后调用它:

    Form childForm = new Form1(true);
    childForm.MdiParent = this;
    childForm.Show();
    

    【讨论】:

    • 您好,谢谢它有效。我创建了一个 switch 语句来覆盖字符串 contextmenustrip 对象映射。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多