Form1에서 Form2를 만들어 띄우기
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2(); // form2에 form1 전달
f.Show();
}
}
Form1에서 Form2는 하나만 만들게 한다
public partial class Form1 : Form
{
Form2 f;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if(f == null) // Form2가 없을 때만 만든다
f = new Form2();
f.Show();
}
}
Form2에서 Form1의 Control에 접근할 수 있나?
// Form2에서 Form1의 Text 변경
private void button2_Click(object sender, EventArgs e)
{
f.Text = textBox1.Text;
}
// Form2에서 Form1의 label1 변경
private void button3_Click(object sender, EventArgs e)
{
// Form1의 label1의 Modifiers 속성을 public으로 바꾼 다음
f.label1.Text = "label1.Text = " + textBox1.Text;
}
Form1에서 static class 작성
public static class Common
{
public static string str = "";
}
// Form2의 “Common Class의 값 지정” 버튼
private void button4_Click(object sender, EventArgs e)
{
Common.str = this.textBox1.Text;
}
// Form1의 “Common class의 값을 가져오기” 버튼
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show(Common.str);
}