TextBox의 KeyPress 이벤트 함수를 통해 Key 입력을 검사하고, 이를 통해 IP 주소를 정확히 입력받는다.

 

  • 오로지 숫자와 BackSpace, '.' 만 입력을 받고, 다른 키 입력은 무시 처리한다.
  • 숫자를 3개 이상 입력되면 자동으로 '.'을 입력해준다.
  • '.'을 입력하면 연속으로 '.'을 입력했는지와 IP자릿수를 넘었는지 체크한다.
  • 아이피는 255를 초과할수 없다.
  • 모든 자릿수의 IP를 입력하였으면, 이후의 입력은 무시한다

 

private void OnKeyPressSingleIP(object sender, KeyPressEventArgs e)
{
        int iPos = 0;               // IP 구역의 현재 위치
        int iDelimitNumber = 0;     // IP 구역의 갯수

        int iLength = textBox.Text.Length;
        int iIndex = textBox.Text.LastIndexOf(".");

        int iIndex2 = -1;
        while (true)
        {
                iIndex2 = textBox.Text.IndexOf(".", iIndex2 + 1);
                if (iIndex2 == -1)
                break;

                ++iDelimitNumber;
        }

        // 숫자키와 백스페이스, '.' 만 입력 가능
        if ((e.KeyChar < 48 || e.KeyChar > 57) &&
        e.KeyChar != 8 && e.KeyChar != '.')
        {
                MessageBox.Show("숫자만 입력 가능합니다""오류");
                e.Handled = true;
                return;
        }

        if (e.KeyChar != 8)
        {
                if (e.KeyChar != '.')
                {
                        if (iIndex > 0)
                        iPos = iLength - iIndex;
                        else
                        iPos = iLength + 1;

                        if (iPos == 3)
                        {
                                // 255 이상 체크
                                string strTmp = textBox.Text.Substring(iIndex + 1) + e.KeyChar;
                                if (Int32.Parse(strTmp) > 255)
                                {
                                        MessageBox.Show("255를 넘길수 없습니다.""오류");
                                        e.Handled = true;
                                        return;
                                }
                                else
                                {
                                        // 3자리가 넘어가면 자동으로 .을 찍어준다
                                        if (iDelimitNumber < 3)
                                        {
                                                textBox.AppendText(e.KeyChar.ToString());
                                                textBox.AppendText(".");
                                                iDelimitNumber++;
                                                e.Handled = true;
                                                return;
                                        }
                                }
                        }

                        // IP 마지막 4자리째는 무조건 무시
                        if (iPos == 4)
                        {
                                e.Handled = true;
                                return;
                        }
                }
                else
                {
                        // 아이피가 3구역 이상 쓰였으면, 이후 키는 무시한다
                        if (iDelimitNumber + 1 > 3)
                        {
                                MessageBox.Show("IP 주소가 정확하지 않습니다.""오류");
                                e.Handled = true;
                                return;
                        }
                        else
                        {
                                // 연속으로 .을 찍었으면 오류
                                if (textBox.Text.EndsWith("."))
                                {
                                        MessageBox.Show("IP 주소가 정확하지 않습니다.""오류");
                                        e.Handled = true;
                                        return;
                                }
                                else
                                iDelimitNumber++;
                        }
                }
        }
}

이 글은 스프링노트에서 작성되었습니다.

+ Recent posts