내 Windows 양식 응용 프로그램에서 두 양식을 추가했습니다 : Form1
및 Form2
. Form1
에 버튼이 있고 Form2
에 richtextbox가 있습니다. Form1
의 버튼을 클릭하면 Form2
이 표시되고 파일 대화 상자가 표시됩니다.두 번째 양식의 richtextbox에 액세스
이제 rich textbox에 파일의 텍스트를로드하고 싶습니다. 문제는 코드에서 richtextbox에 액세스하는 방법입니까?
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "txt files (*.txt)|*.txt";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
this.Hide();
Form Form2 = new Form();
Form2.Show();
// load a text file to rich text box. How to access the rich text box here?
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
UPDATE :
나는 형식 2의 인스턴스를 만들고 생성자에 문자열을 전달하려했지만 그것은 작동하지 않습니다.
public partial class Form2 : Form
{
public Form2(string text)
{
InitializeComponent();
richTextBox1.Text = text;
}
}
System.Windows .Forms.Form ' 1 인자를받는 생성자를 포함하지 않습니다. –
@Love 그래서 생성자 코드를 게시했습니다. 수정해야합니다. –
.net 프레임 워크에서는 지원하지 않습니다. 생성자는 윈도우 폼을 제외하고 일반 클래스에서 작동합니다. –