我有 2 个带有主窗口和第二个窗口的表单。主窗口 ( Form1
) 应从第二个窗口 ( Form2
)获取文本
第二个窗口Form2
(所以Form1
当我发送黄色示例时,它只是黑色文本。
我不是 C# 专家,因为我对此很陌生。我确信这是一个非常简单的问题,但对我来说并不那么容易。
Form1.cs
:
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 tester
{
public partial class Form1 : Form
{
public string text;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 NewForm2 = new Form2(this);
NewForm2.Show();
}
internal void populate()
{
richTextBox1.Text = text;
}
}
Form2.cs
:
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 Tester
{
public partial class Form2 : Form
{
Form1 texting;
public Form2(Form1 iForm)
{
texting = iForm;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
texting.text = richTextBox1.Text;
texting.populate();
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
if ( MyColorDialog.ShowDialog() != DialogResult.Cancel)
{
richTextBox1.ForeColor = MyColorDialog.Color;
}
}
}