我怎样才能使它在单击表单2上的按钮时影响form1(C#)Winforms上的richtextbox?

时间:2013-04-03 20:57:06

标签: c# winforms button visual-studio-2012 richtextbox

我有一个WinForms应用程序。我希望能够在form2上按一个按钮,然后在form1上反映richtextbox。

例如,如果form2上的按钮被编码为" Hello"被点击后,我会喜欢"你好"要在form1上的richtextbox上显示的文本。

我该怎么做呢?我在网上搜索但找不到任何东西。

Form1中

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Text;
using System.Drawing.Printing;
using System.Diagnostics;


namespace Basic_Word_Processor_Version1._0._0
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            Instance = this;
        }
            private string filepath = null;
        private int checkPrint;

代码

 public static Form1 Instance { get; private set; }

        // You still need this like in the first scenario.
        public RichTextBox RichTextBox1 { get { return richTextBoxPrintCtrl1; } }

        // This constructor should already exist. Just add the one line to it.


    }

Form3

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Basic_Word_Processor_Version1._0._0
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        Form1.Instance.richTextBoxPrintCtrl1.Text = "";
    }
    public partial class Form1 : Form
{
    public static Form1 Instance { get; private set; }
    public RichTextBox RichTextBox1 { get { return richTextBoxPrintCtrl1; } }

    public Form1()
    {
        InitializeComponent();
        Instance = this;
    }
}

2 个答案:

答案 0 :(得分:3)

您可以通过属性公开控件。假设您在form2中引用了form1:

在form1中:

public RichTextBox PrintCtrl1 { get { return richTextBoxPrintCtrl1; } }

在form2中:

form1.PrintCtrl1.Text = "Howdy from form2.";

<强>更新 如果在form2中没有对form1的引用,则还可以通过静态属性公开form1的实例:

在form1中:

public static Form1 Instance { get; private set; }

// You still need this like in the first scenario.
public RichTextBox PrintCtrl1 { get { return richTextBoxPrintCtrl1; } }

// This constructor should already exist. Just add the one line to it.
public Form1()
{
    Instance = this;
}

然后在form2中,你会这样做,而不是我在上面显示的内容:

Form1.Instance.PrintCtrl1.Text = "Howdy from form2.";

您的Form1类现在应该是这样的(加上您添加的任何其他内容):

public partial class Form1 : Form
{
    public static Form1 Instance { get; private set; }
    public RichTextBox PrintCtrl1 { get { return richTextBoxPrintCtrl1; } }

    public Form1()
    {
        InitializeComponent();
        Instance = this;
    }
}

您的Form3类应该如下所示:

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

    protected void button1_Click(object sender, EventArgs e)
    {
        Form1.Instance.PrintCtrl1.Text = "";
    }
}

答案 1 :(得分:0)

我知道在这个页面上已经有一个已接受的答案,是的,虽然答案会“起作用”,但这有两个原因。首先,养成使用静态来获取事物可见性的习惯是一个非常糟糕的习惯,当不必要地使用时,违反了OOP编程的概念。其次,通过执行公共静态表单实例,您已经使它成为第二种形式不可重用。除了与第一个表单交互之外,它无法做任何事情。更好的方法是使用事件来促进表单之间的通信。以下代码示例演示了如何执行此操作。

Form1中:

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

    private void button1_Click(object sender, EventArgs e)
    {
        //Declare your new form
        Form2 form2 = new Form2();

        //Register the event
        form2.changeTextEvent += new EventHandler<TextChangedEventArgs>              (form2_changeTextEvent);

        //Show your new form
        form2.ShowDialog();
    }

    //Handler for the event from form 2
    void form2_changeTextEvent(object sender, TextChangedEventArgs e)
    {
        //Sets the text of this form equal to the text in our custom event args
        //Just a simple example of doing something with the event arg
        this.Text = e.Text;
    }
}

窗体2:

public partial class Form2 : Form
{
    //Declare your event
    public event EventHandler<TextChangedEventArgs> changeTextEvent;
    private String newText = "Custom events FTW!!!";

    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //If the event is registered fire it, otherwise do nothing
        if (changeTextEvent != null)
        {
            //fire the event and give our custom event args some text
            changeTextEvent(sender, new TextChangedEventArgs(newText));
        }
    }
}

自定义事件Arg:

public class TextChangedEventArgs : EventArgs
{
    private String text;

    //Did not implement a "Set" so that the only way to give it the Text value is in 
    //the constructor
    public String Text
    {
        get { return text; }
    }

    public TextChangedEventArgs(String theText)
        : base()
    {
        text = theText;
    }
}

以这种方式实现,Form2现在可以完全重用,并且可以以任何控制/形式触发事件。不同的形式可以以不同的方式对事件做出反应,但形式2永远不需要改变。