1

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

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

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

表格1

    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.


    }

表格3

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

2 回答 2

3

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

在表格 1 中:

public RichTextBox PrintCtrl1 { get { return richTextBoxPrintCtrl1; } }

在表格 2 中:

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

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

在表格 1 中:

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 = "";
    }
}
于 2013-04-03T21:00:13.220 回答
0

我知道这个页面上已经有一个接受的答案,是的,虽然答案会“起作用”,但它很糟糕,原因有两个。首先,养成使用静态来获得事物可见性的习惯是一个非常不好的习惯,如果不必要地使用,就会违反 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;
    }
}

表格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));
        }
    }
}

自定义事件参数:

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 永远不需要改变。

于 2013-10-22T16:00:45.947 回答