IP Address Text Entry Box
You can use a MaskedTextBox with the mask "###.###.###.###"
However its often best just to use a standard text box as the masked text box approah creates issues. Note that the Windows network setup IP address box is not based on a standard tool unfortunately.
Parsing Removing Leading Zero's
IPAddress::TryParse doesn't accept leading zeros's (as it deals with them as octals – bloody stupid but there you go).
So you have to remove them. Do this:
String ^EnteredIpAddress;
String ^ParseIpAddress;
System::Net::IPAddress ^IpAddress;
EnteredIpAddress = txtIpAddress->Text->Replace(" ", "0"); //For masked text box convert spaces to zero's
//Remove any leading zero's for the parse function (which treats them as octal markers)
ParseIpAddress = "." + EnteredIpAddress; //Add a leading "."
ParseIpAddress = ParseIpAddress->Replace(".0", "."); //Remove a leading zero
ParseIpAddress = ParseIpAddress->Replace(".0", "."); //Remove a leading zero
ParseIpAddress = ParseIpAddress->Replace("..", ".0."); //Correct any bytes which we're zero
ParseIpAddress = ParseIpAddress->Substring(1); //Remove the leading "." again
if (!System::Net::IPAddress::TryParse(ParseIpAddress, IpAddress))
IpAddress = IPAddress::Parse("0.0.0.0");
Alternatively, as a function to call
//***************************************
//***************************************
//********** FORMAT IP ADDRESS **********
//***************************************
//***************************************
private: String ^FormatIpAddress (String ^EnteredIpAddress)
{
String ^ParseIpAddress;
System::Net::IPAddress ^IpAddress;
EnteredIpAddress = EnteredIpAddress->Replace(" ", "");
//Remove any leading zero's
ParseIpAddress = "." + EnteredIpAddress; //Add a leading "."
ParseIpAddress = ParseIpAddress->Replace(".0", "."); //Remove a leading zero
ParseIpAddress = ParseIpAddress->Replace(".0", "."); //Remove a leading zero
ParseIpAddress = ParseIpAddress->Replace("..", ".0."); //Correct any bytes which we're zero
ParseIpAddress = ParseIpAddress->Substring(1); //Remove the leading "." again
if (!System::Net::IPAddress::TryParse(ParseIpAddress, IpAddress))
IpAddress = IPAddress::Parse("0.0.0.0");
return(IpAddress->ToString());
}
Connverting from a IPAddress to a String
sTemp = IpAddress1->ToString();
Connverting from a string to IP Address
IPAddress ^address;
if (!IPAddress::TryParse(OurIpString, address))
address = IPAddress::Parse("255.255.255.255"); //Error - use default broadast address
Connverting from a IP Address To Bytes
IPEndPoint^ tempIpEP = gcnew IPEndPoint(SourceIpAddress,0);
EndPoint^ tempRemoteEP = safe_cast<EndPoint^>(tempIpEP);
SocketAddress^ ThisSocket = tempRemoteEP->Serialize();
if (ThisSocket->Family == AddressFamily::InterNetwork)
{
Port = Convert::ToInt16(ThisSocket[2]) << 8; //Port number is in bytes 2:3
Port |= Convert::ToInt16(ThisSocket[3]);
rxIpBytes[0] = ThisSocket[4]; //IP Address in bytes 4:7
rxIpBytes[1] = ThisSocket[5];
rxIpBytes[2] = ThisSocket[6];
rxIpBytes[3] = ThisSocket[7];
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.