Recent

Author Topic: Ip Address Edit component?  (Read 7875 times)

Ign_17

  • New Member
  • *
  • Posts: 43
Ip Address Edit component?
« on: January 16, 2017, 12:56:26 am »
Hi everybody,

well I´m looking for a component usefull for introducing IP addresses, something like a Tedit with four fields, changing from one field to another just pressing the '.' key.

I have tried it with a Tmaskedit component, or using four Tedit together and programming the OnKeyPress event in order to detect the '.' keystroke for changing between Tedits, but not satisfactory at all.

Any of you know any component or implementation of an IpAddress Edit component? 


Thanks so much in advance.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Ip Address Edit component?
« Reply #1 on: January 16, 2017, 08:09:58 am »
What is wrong with a TMaskEdit and this mask? "999.999.999.999". You can validate in the OnEdittingDone event. The MaskEdit.Text property will contain the IP address.
Specialize a type, not a var.

Ign_17

  • New Member
  • *
  • Posts: 43
Re: Ip Address Edit component?
« Reply #2 on: January 16, 2017, 12:43:09 pm »

Yes, yes, of course it works and there is nothing wrong with MaskEdit. I´m just wondering whether there is or not a more specific component for editing IP adresses.

For example I don´t like very much the way it shows the numbers after editing, left justifiy although I adjust the property "alignment" to   "taRightJustify". Is there any way to modify the alignment of the numbers using the "999.999.999.999" mask?

Thanks again.
 

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Ip Address Edit component?
« Reply #3 on: January 16, 2017, 11:57:34 pm »
...after editing...Is there any way to modify the alignment of the numbers
Suppose that the mask equal to '999.999.999.999;1; ' // space char at the end!
Code: Pascal  [Select][+][-]
  1. type
  2.   TIPv4Rec = record
  3.     Octets: array [1..4] of Byte;
  4.   end;
  5.  
  6. function StrToIPV4(const S: string; out R: TIPv4Rec): Boolean;
  7. var
  8.   i, DotPos: Integer;
  9.   RestOfString: string;
  10.   Value: Integer;
  11. begin
  12.   RestOfString := S;
  13.   for i := Low(R.Octets) to High(R.Octets) do
  14.   begin
  15.     DotPos := Pos('.', RestOfString);
  16.     if DotPos = 0 then
  17.       DotPos := Length(RestOfString) + 1;
  18.     if not TryStrToInt(Copy(RestOfString, 1, DotPos - 1), Value) or
  19.       not (Value in [Low(Byte)..High(Byte)]) then
  20.     begin
  21.       Exit(False);
  22.     end;
  23.     R.Octets[i] := Value;
  24.     Delete(RestOfString, 1, DotPos);
  25.   end;
  26.   Result := True;
  27. end;
  28.  
  29. procedure TForm1.MaskEdit1EditingDone(Sender: TObject);
  30. var
  31.   S: string;
  32.   R: TIPv4Rec;
  33. begin
  34.   S := DelSpace((Sender as TMaskEdit).EditText);
  35.   if not StrToIPV4(S, R) then
  36.   begin
  37.     MessageDlg(S + ' is not valid IPv4 string', mtError, [mbOK], 0);
  38.     ActiveControl := TWinControl(Sender);
  39.   end;
  40.   if not (R.Octets[1] in [1..223]) then
  41.   begin
  42.     MessageDlg('First octet must be from 1 to 223', mtError, [mbOK], 0);
  43.     ActiveControl := TWinControl(Sender);
  44.   end;
  45.   // Align right
  46.   TMaskEdit(Sender).Text := Format('%3d.%3d.%3d.%3d', [
  47.     R.Octets[1], R.Octets[2], R.Octets[3], R.Octets[4]
  48.     ]);
  49.   // Store the R in some place
  50. end;

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: Ip Address Edit component?
« Reply #4 on: January 18, 2017, 03:43:48 am »
Yes, yes, of course it works and there is nothing wrong with MaskEdit. I´m just wondering whether there is or not a more specific component for editing IP adresses.

Probably not as a native component in FreePascal/Lazarus, but on Windows at least, you could easily write a custom component that wraps the Win32 IP Address control.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Ign_17

  • New Member
  • *
  • Posts: 43
Re: Ip Address Edit component?
« Reply #5 on: January 24, 2017, 12:55:03 pm »
...after editing...Is there any way to modify the alignment of the numbers
Suppose that the mask equal to '999.999.999.999;1; ' // space char at the end!
Code: Pascal  [Select][+][-]
  1. type
  2.   TIPv4Rec = record
  3.     Octets: array [1..4] of Byte;
  4.   end;
  5.  
  6. function StrToIPV4(const S: string; out R: TIPv4Rec): Boolean;
  7. var
  8.   i, DotPos: Integer;
  9.   RestOfString: string;
  10.   Value: Integer;
  11. begin
  12.   RestOfString := S;
  13.   for i := Low(R.Octets) to High(R.Octets) do
  14.   begin
  15.     DotPos := Pos('.', RestOfString);
  16.     if DotPos = 0 then
  17.       DotPos := Length(RestOfString) + 1;
  18.     if not TryStrToInt(Copy(RestOfString, 1, DotPos - 1), Value) or
  19.       not (Value in [Low(Byte)..High(Byte)]) then
  20.     begin
  21.       Exit(False);
  22.     end;
  23.     R.Octets[i] := Value;
  24.     Delete(RestOfString, 1, DotPos);
  25.   end;
  26.   Result := True;
  27. end;
  28.  
  29. procedure TForm1.MaskEdit1EditingDone(Sender: TObject);
  30. var
  31.   S: string;
  32.   R: TIPv4Rec;
  33. begin
  34.   S := DelSpace((Sender as TMaskEdit).EditText);
  35.   if not StrToIPV4(S, R) then
  36.   begin
  37.     MessageDlg(S + ' is not valid IPv4 string', mtError, [mbOK], 0);
  38.     ActiveControl := TWinControl(Sender);
  39.   end;
  40.   if not (R.Octets[1] in [1..223]) then
  41.   begin
  42.     MessageDlg('First octet must be from 1 to 223', mtError, [mbOK], 0);
  43.     ActiveControl := TWinControl(Sender);
  44.   end;
  45.   // Align right
  46.   TMaskEdit(Sender).Text := Format('%3d.%3d.%3d.%3d', [
  47.     R.Octets[1], R.Octets[2], R.Octets[3], R.Octets[4]
  48.     ]);
  49.   // Store the R in some place
  50. end;


With a long delay, thank you very much for your code example. Finally I have used your example as a model for what i was looking for, and works fine.

Thanks a lot,

Regards.

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Ip Address Edit component?
« Reply #6 on: January 25, 2017, 05:53:00 am »
Quote
Finally I have used your example as a model for what i was looking for, and works fine.

If possible, please, share with the community.
Be mindful and excellent with each other.
https://github.com/cpicanco/

minesadorada

  • Sr. Member
  • ****
  • Posts: 452
  • Retired
Re: Ip Address Edit component?
« Reply #7 on: January 25, 2017, 10:47:17 am »
Built-in unit fileinfo has a function TryStrToVersionQuad
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Ip Address Edit component?
« Reply #8 on: January 25, 2017, 03:11:12 pm »
Built-in unit fileinfo has a function TryStrToVersionQuad
1. It's array of Word, IPv4 is array of Byte.
2. Try this code  ::)
Code: Pascal  [Select][+][-]
  1. uses FileInfo;
  2.  
  3. procedure TForm1.FormCreate(Sender: TObject);
  4. var
  5.   R: TVersionQuad;
  6. begin
  7.   if TryStrToVersionQuad('66666.77777.88888.99999', R) then
  8.     Caption := VersionQuadToStr(R);
  9. end;

minesadorada

  • Sr. Member
  • ****
  • Posts: 452
  • Retired
Re: Ip Address Edit component?
« Reply #9 on: January 25, 2017, 03:13:19 pm »
Built-in unit fileinfo has a function TryStrToVersionQuad
1. It's array of Word, IPv4 is array of Byte.
2. Try this code  ::)
Code: Pascal  [Select][+][-]
  1. uses FileInfo;
  2.  
  3. procedure TForm1.FormCreate(Sender: TObject);
  4. var
  5.   R: TVersionQuad;
  6. begin
  7.   if TryStrToVersionQuad('66666.77777.88888.99999', R) then
  8.     Caption := VersionQuadToStr(R);
  9. end;
File a bug report.  I saw no checking for Qword > 255 in fileinfo.
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

 

TinyPortal © 2005-2018