Click here to Skip to main content
15,884,911 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need a textbox that only allows format of an IP address using windows application.i can't do this in windows application. Please help me to do this step by step.i don't know where implement the code.
i try but not sure this.i don't know where is implement code.

What I have tried:

<pre>private void txtSta1_IP_Adrs_TextChanged(object sender, EventArgs e)
        {
            bool isIPAddress = false;
            string pattern = @"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b";

            Regex regex = new Regex(pattern);
            isIPAddress = regex.IsMatch(pattern);
          
            string ip1 = "192.168.0.1";
            string ip2 = "302.0.0.1";

            //Regex.IsMatch(ip1, pattern); // returns true
            Regex.IsMatch(ip2, pattern); // returns false
         //   return isIPAddress;
           
        }
Posted
Updated 29-Dec-20 0:41am
v2

You need to apply regex as a validation to the input control.

Either you apply regex on Control.KeyUp[^] / Control.Validating[^] events to an input text control OR use a custom control that has this capability.

Take cue from this article: IP-Address TextBox[^]
 
Share this answer
 
Comments
Maciej Los 29-Dec-20 1:42am    
5ed!
Sandeep Mewara 29-Dec-20 3:53am    
:thanks:
Thirty seconds of Googling, it took me: IP-Address TextBox[^]

You need to either learn how to Google, or learn to start using Google. You caould have had this over twenty minutes ago with a trivial search, mostly taken from your question: "c# winforms textbox that only allows format of an IP address"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900