새로운 폼을 띄운 다음 그 폼에서 값을 받아서 처리해야 할 경우가 있다.
이때 폼을 띄울 때 처리해야 할 코드이다.
//폼 중복 열기 방지
foreach (Form openForm in Application.OpenForms)
{
if (openForm.Name == "FindForm")
{
openForm.Activate();
return;
}
}
FindForm searchf = new FindForm();
searchf.StartPosition = FormStartPosition.Manual;
searchf.Location = new Point(100, 100);
if (searchf.ShowDialog() == DialogResult.OK)
{
// 값을 넘겨 받아서 실제 처리할 함수
}
======= 값을 넘겨주는 Form 창 =======
public partial class FindForm : Form
{
private int listView1_No;
private int listView2_No;
private int LV_No;
public int LV1_No
{
get { return listView1_No; }
}
public int LV2_No
{
get { return listView2_No; }
}
public int SelectedViewNo
{
get { return LV_No; }
}
public FindForm()
{
InitializeComponent();
}
private void btnOK_Click(object sender, EventArgs e)
{
string Value1 = textBox1.Text;
string Value2 = textBox2.Text;
if (LV1.Checked)
{
LV_No = 1; // Value1
}
else
{
LV_No = 2; // Value2
}
int.TryParse(Value1, out listView1_No);
if (Value1.Length == 0)
{
MessageBox.Show("Value1 의 값을 입력하지 않았습니다");
return;
}
else if (listView1_No == 0)
{
MessageBox.Show("1 이상의 숫자를 입력해야 합니다");
return;
}
int.TryParse(Value2, out listView2_No);
if (Value2.Length == 0)
{
MessageBox.Show("Value2 의 값을 입력하지 않았습니다");
return;
}
else if (listView2_No == 0)
{
MessageBox.Show("1 이상의 숫자를 입력해야 합니다");
return;
}
DialogResult = DialogResult.OK;
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
//숫자,백스페이스만 입력받는다.
if (!(Char.IsDigit(e.KeyChar)) && e.KeyChar != 8) //8:백스페이스,45:마이너스,46:소수점
{
e.Handled = true;
}
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
//숫자,백스페이스만 입력받는다.
if (!(Char.IsDigit(e.KeyChar)) && e.KeyChar != 8) //8:백스페이스,45:마이너스,46:소수점
{
e.Handled = true;
}
}
}
'C# > Layout 및 델리게이트' 카테고리의 다른 글
C# 폼간 데이터 전송 : Properties(속성) get, set (완벽한 정리) (6) | 2015.11.30 |
---|---|
C# 폼간 데이터 전송 (부모폼에서 자식폼, 자식폼에서 부모폼) (0) | 2015.11.29 |
C# Panel 또는 TabPage 에 다른 폼 올리기 (0) | 2015.09.22 |
C# 폼 중복실행 방지, Show(), ShowDialog() (2) | 2015.09.20 |
C# MDI(Multiple Document Interface) (0) | 2015.09.17 |