SoFunction
Updated on 2025-03-07

WinForm IP address input box control implementation

This article shares the specific implementation code of WinForm IP address input box control for your reference. The specific content is as follows

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;

namespace 
{
 public partial class IPInput : UserControl
 {
 public IPInput()
 {
  InitializeComponent();
 }
 TextBox ParentTxt;
 private void IPInput_Load(object sender, EventArgs e)
 {
  ParentTxt = txt_1;
 }
 public void txt_KeyDown(object sender, KeyEventArgs e)
 {
  ParentTxt = (TextBox)sender;
  if ( == )
  {
  switch (('_')[1])
  {
   case "1":
   break;
   case "2":
   if ( == 0 &&  != "")
   {
    if (() > 255)
    {
    ( + "Not a valid item. Please specify a value between 1 and 255.", "mistake", , );
     = "255";
     = 0;
    }
    else
    {
    txt_1.Focus();
    }
   }
   else if ( == "")
   {
    txt_1.Focus();
   }
   break;
   case "3":
   if ( == 0 &&  != "")
   {
    if (() > 255)
    {
    ( + "Not a valid item. Please specify a value between 1 and 255.", "mistake", , );
     = "255";
     = 0;
    }
    else
    {
    txt_2.Focus();
    }
   }
   else if ( == "")
   {
    txt_2.Focus();
   }
   break;
   case "4":
   if ( == 0 &&  != "")
   {
    if (() > 255)
    {
    ( + "Not a valid item. Please specify a value between 1 and 255.", "mistake", , );
     = "255";
     = 0;
    }
    else
    {
    txt_3.Focus();
    }
   }
   else if ( == "")
   {
    txt_3.Focus();
   }
   break;
  }
  }
  else if ( == )
  {
  switch (('_')[1])
  {
   case "1":
   if ( ==  &&  != "")
   {
    if (() > 223)
    {
    ( + "Not a valid item. Please specify a value between 1 and 223.", "mistake", , );
     = "223";
     = ;
    }
    else
    {
    txt_2.Focus();
    }
   }
   else if ( == "")
   {
    txt_2.Focus();
   }
   break;
   case "2":
   if ( ==  &&  != "")
   {
    if (() > 255)
    {
    ( + "Not a valid item. Please specify a value between 1 and 255.", "mistake", , );
     = "255";
     = ;
    }
    else
    {
    txt_3.Focus();
    }
   }
   else if ( == "")
   {
    txt_3.Focus();
   }
   break;
   case "3":
   if ( ==  &&  != "")
   {
    if (() > 255)
    {
    ( + "Not a valid item. Please specify a value between 1 and 255.", "mistake", , );
     = "255";
     = ;
    }
    else
    {
    txt_4.Focus();
    }
   }
   else if ( == "")
   {
    txt_4.Focus();
   }
   break;
   case "4":
   break;
  }
  }
 }
 public void txt_KeyPress(object sender, KeyPressEventArgs e)
 {
  ParentTxt = (TextBox)sender;
  Regex regex = new Regex(@"^[0-9]+$");
  if (!(()) &&  != (Char))
  {
   = true;
  }
  else if ( == (Char))
  {
   = false;
  switch (('_')[1])
  {
   case "1":
   break;
   case "2":
   if ( == 0)
   {
    txt_1.Focus();
    if (txt_1.Text != "")
    {
    txt_1.Text = txt_1.(0, txt_1. - 1);
    }
    txt_1.SelectionStart = txt_1.;
   }
   break;
   case "3":
   if ( == 0)
   {
    txt_2.Focus();
    if (txt_2.Text != "")
    {
    txt_2.Text = txt_2.(0, txt_2. - 1);
    }
    txt_2.SelectionStart = txt_2.;
   }
   break;
   case "4":
   if ( == 0)
   {
    txt_3.Focus();
    if (txt_3.Text != "")
    {
    txt_3.Text = txt_3.(0, txt_3. - 1);
    }
    txt_3.SelectionStart = txt_3.;
   }
   break;
  }
  }
  else
  {
  switch (('_')[1])
  {
   case "1":
   if ( == )
   {
    if (( + ()) > 223)
    {
    ( + () + "Not a valid item. Please specify a value between 1 and 223.", "mistake", , );
     = true;
     = "223";
    }
    else
    {
     = false;
    }
   }
   else if( != 3)
   {
     = false;
   }
   else
   {
     = true;
   }
   break;
   default:
   if ( == )
   {
    if (( + ()) > 255)
    {
    ( + () + "Not a valid item. Please specify a value between 1 and 255.", "mistake", , );
     = true;
     = "255";
    }
    else
    {
     = false;
    }
   }
   else if ( != 3)
   {
     = false;
   }
   else
   {
     = true;
   }
   break;
  }
  }
 }
 public void txt_TextChanged(object sender, EventArgs e)
 {
  if ( == 3)
  {
  switch (('_')[1])
  {
   case "1":
   if ( == )
   {
    txt_2.Focus();
   }
   break;
   case "2":
   if ( == )
   {
    txt_3.Focus();
   }
   break;
   case "3":
   if ( == )
   {
    txt_4.Focus();
   }
   break;
   case "4":
   break;
  }
  }
 }
 }
}


using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;

namespace 
{
 public partial class ControlText : TextBox
 {
 public ControlText()
 {
  InitializeComponent();
 }
 public void txt_TextChange(object sender, EventArgs e)
 {
  if ( == 3)
  {
  ("{TAB}");
  }
 }
 protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
 {
  if (keyData == )
  {
  return true;
  }
  return (ref msg, keyData);
 }
 }
}

For more complete code, please click to download:WinForm IP address input box control

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.