我如何使它在我点击form2上的按钮时,影响form1(C#)Winforms上的richtextbox?

我如何使它在我点击form2上的按钮时,影响form1(C#)Winforms上的richtextbox?,c#,winforms,button,visual-studio-2012,richtextbox,C#,Winforms,Button,Visual Studio 2012,Richtextbox,我有一个WinForms应用程序。我希望能够按下form2上的一个按钮,它将反映在form1上的richtextbox上 例如,如果form2上的按钮被编码为单击时键入“Hello”,那么我希望“Hello”文本出现在form1上的richtextbox上 我该怎么做呢?我在网上搜索过,但什么也找不到 表格1 代码 表格3 您可以通过属性公开控件。假设您在form2中引用了form1: 在表格1中: public RichTextBox PrintCtrl1 { get { return ric

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

例如,如果form2上的按钮被编码为单击时键入“Hello”,那么我希望“Hello”文本出现在form1上的richtextbox上

我该怎么做呢?我在网上搜索过,但什么也找不到

表格1

代码

表格3


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

在表格1中:

public RichTextBox PrintCtrl1 { get { return richTextBoxPrintCtrl1; } }
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;
}
表格2:

form1.PrintCtrl1.Text = "Howdy from form2.";
更新: 如果在form2中没有对form1的引用,也可以通过静态属性公开form1的实例:

在表格1中:

public RichTextBox PrintCtrl1 { get { return richTextBoxPrintCtrl1; } }
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;
}
然后在表格2中,你会这样做,而不是我上面展示的:

Form1.Instance.PrintCtrl1.Text = "Howdy from form2.";
您的Form1类现在应该如下所示(加上您添加的任何其他内容):

你的Form3课程应该是这样的:

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

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

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

表格1:

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;
    }
}

通过这种方式实现,Form2现在完全可重用,并且可以在注册事件的任何控件/表单中触发事件。不同的表单可能会以不同的方式对事件做出反应,但form2永远不需要更改。

您可以尝试在form1中创建一个静态变量,该变量将保存form1的实例,然后通过form1的静态变量(或搜索“singleton”)从form2调用richtextbox。对了,我已经尝试编写了您的代码,我发现了一个错误。错误3非静态字段、方法或属性“Basic\u Word\u Processor\u Version1.\u 0.\u 0.Form1.richTextBoxPrintCtrl1”需要对象引用。听起来您使用的是
Form1
类型,而不是
Form1
对象。我将更新我的答案,以解决您在form2中没有引用form1的情况。如果可以的话。干杯我还在学习编程,所以这是我的全部学习。另一个错误。:/错误1“Basic_Word_Processor_Version1._0._0.Form1”类型的声明缺少部分修饰符;存在此类型的另一个部分声明
public partial class Form3 : Form
{
    public Form3()
    {
        InitializeComponent();
    }

    protected void button1_Click(object sender, EventArgs e)
    {
        Form1.Instance.PrintCtrl1.Text = "";
    }
}
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;
    }
}
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));
        }
    }
}
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;
    }
}