我怎样才能使它,当我点击一个按钮在form2上,它会影响一个richtextbox在form1 (c#) Winform

本文关键字:一个 影响 richtextbox form1 Winform form2 按钮 | 更新日期: 2023-09-27 18:07:13

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

例如,如果form2上的按钮被编码为在点击时输入"Hello",那么我希望"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.

    }

对此

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

我怎样才能使它,当我点击一个按钮在form2上,它会影响一个richtextbox在form1 (c#) Winform

您可以通过属性公开控件。假设在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 = "";
    }
}

我知道在这个页面上已经有一个公认的答案,是的,虽然答案会"工作",但它有两个原因。首先,养成使用静态来获得事物可见性的习惯是一个非常不好的习惯,并且在不必要的情况下使用它会违反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;
    }
}

Form2:

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;
    }
}
通过这种方式实现,Form2现在是完全可重用的,并且可以在任何注册事件的控件/表单中触发事件。不同的表单可能以不同的方式对事件做出反应,但form2永远不需要改变。
相关文章: