Project/SMI
[C#] 메인폼 객체를 다른 생성된 폼에서 사용할 경우
빡썽
2010. 11. 28. 23:07
[부모 폼]
namespace MedicalInformation
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this); //현재 폼의 객체 this를 생성된 폼의 생성자에 넘긴다.
f2.Show();
}
public string GetName()
{
return textBox1.Text;
}
{
Form2 f2 = new Form2(this); //현재 폼의 객체 this를 생성된 폼의 생성자에 넘긴다.
f2.Show();
}
public string GetName()
{
return textBox1.Text;
}
public string GetAge()
{
return textBox2.Text;
}
}
}
{
return textBox2.Text;
}
}
}
[생성된 폼]
namespace MedicalInformation
{
public partial class Form2 : Form
{
Form1 f1 = new Form1(); // 또는 Form1 f1 = null; 로 선언해도 무관
public Form2()
{
InitializeComponent();
}
public Form2(Form1 _f1)
{
this.f1 = _f1; // 넘겨받은 Form1의 객체를 현재의 Form1의 객체에 대입
InitializeComponent();
FormSettingProc(); //InitializeComponent() 실행 후 폼의 셋팅이 완료됬으므로 f1객체 사용 가능
}
{
this.f1 = _f1; // 넘겨받은 Form1의 객체를 현재의 Form1의 객체에 대입
InitializeComponent();
FormSettingProc(); //InitializeComponent() 실행 후 폼의 셋팅이 완료됬으므로 f1객체 사용 가능
}
public void FormSettingProc()
{
string temp = f1.GetAge(); //Main폼에 있는 GetAge()함수를 사용할 수 있다.
label1.Text = temp;
}
}
}
{
string temp = f1.GetAge(); //Main폼에 있는 GetAge()함수를 사용할 수 있다.
label1.Text = temp;
}
}
}