【问题标题】:C# how get a value of richTextBox added to second formC#如何将richTextBox的值添加到第二种形式
【发布时间】:2013-08-29 09:43:40
【问题描述】:

我写程序。它是简单的编辑器链接记事本,在主窗体中创建了新窗体,我不知道如何在子窗体的richTextField 中获取和设置值。当您单击新建文件程序时,请使用 trah 功能。

private void NewWindow()
{
     Form2 f2 = new Form2();
     f2.MdiParent = this;
     f2.Text = "Document " + WindowNumber.ToString();
     WindowNumber++;
     f2.Show();
 }

当我有许多打开的窗口时,我无法在每个窗口中访问richTextBox。

怎么做?

【问题讨论】:

    标签: c# winforms forms file-io get


    【解决方案1】:

    通常,控件被添加到带有受保护访问修饰符的表单中。然后,要从外部获取它们的值,您需要在每个表单上创建一个公共属性以公开文本。

    public string RichText{
        get{ return myTextBox.Text;}
    }
    

    【讨论】:

      【解决方案2】:

      如果您希望父表单编辑子表单或其他方式,我不太明白。

      如果您希望父级能够编辑子级,那么子级应该公开一个类似 Oscar 示例的方法来编辑 RichTextBox,并且父级应该将子级保存在某处:

      List<Form2> frm = new List<Form2>();
      private void NewWindow()
      {
           Form2 f2 = new Form2();
           f2.MdiParent = this;
           f2.Text = "Document " + WindowNumber.ToString();
           WindowNumber++;
           f2.Show();
           frm.Add(f2);
      }
      

      如果孩子应该编辑父母,你有几种方法可以做到。可能最好的设计是使用事件:

        public delegate void EditHandler();
        public event EditHandler edit;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-23
        • 1970-01-01
        • 2020-07-27
        • 1970-01-01
        • 1970-01-01
        • 2011-07-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多