-3

我已经使用以下代码将工具控件连接到 another1:

--- Form1.cs

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

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2(this);
            frm.Show();
        }

        public string LabelText
        {
            get { return Lbl.Text; }
            set { Lbl.Text = value; }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }

--- Form2.cs

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

        private Form1 mainForm = null;
        public Form2(Form callingForm)
        {
            mainForm = callingForm as Form1;
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }



        private void timer1_Tick(object sender, EventArgs e)
        {
            this.mainForm.LabelText = txtMessage.Text;

            if (timer1.Enabled == true)
            {
                int line = 1 + richTextBox1.GetLineFromCharIndex(richTextBox1.GetFirstCharIndexOfCurrentLine());
                int column = 1 + richTextBox1.SelectionStart - richTextBox1.GetFirstCharIndexOfCurrentLine();
                txtMessage.Text = "line: " + line.ToString() + " , column: " + column.ToString();
            }
        }
    }

***** 输出是**

在此处输入图像描述

来自 Form2 的标签文本连接到 Form1 。

所以它已经修复了。

现在我的问题是有没有办法可以为 void 函数做同样的事情?

我的意思是,例如:在 Form1 中,我得到了 1button,里面有一个控件:richTextBox1.Copy(); 那么这个控件将用于 Form2 上的 richTextBox1。(这将复制Form2上richtextbox中的选定文本)这可能吗?真的需要帮助。非常感谢提前!

4

2 回答 2

1

Here's something to get you started:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        Form2 frm2 = new Form2(this);            
    }
}

And make sure that richTextBox1 is declared public.

And:

public partial class Form2 : Form
{
    Form1 sendingForm;

    public Form2(Form1 frm1)
    {
        InitializeComponent();
        sendingForm = frm1;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Text = sendingForm.richTextBox1.Text;
    }
}

What's done here is: Initializing the Form2 instance with a reference to the sender Form1 instance, and using that reference to get to the RichTextBox.

EDIT:

Maybe (!) this is what you're looking for:

mainForm.richTextBox1.Copy();
于 2013-05-16T12:27:57.620 回答
0

您将 Form2 的声明移至 Class 级别:

--Form1

    Form2 frm = null;

    private void button1_Click(object sender, EventArgs e)
    {
        frm = new Form2(this);
        frm.Show();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (frm != null)
        {
            frm.CopyRichTextBox();
        }
    }

--Form2

    public void CopyRichTextBox()
    {
        this.richTextBox1.Copy();
    }
于 2013-05-16T13:13:02.570 回答