'C# 폼 데이터 받아서 처리'에 해당되는 글 1건

728x90

새로운 폼을 띄운 다음 그 폼에서 값을 받아서 처리해야 할 경우가 있다.

이때 폼을 띄울 때 처리해야 할 코드이다.


//폼 중복 열기 방지
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;
        }
    }
}



블로그 이미지

Link2Me

,