帮忙写个过程,关于输入ip地址 ( 积分: 50 )

  • 主题发起人 主题发起人 gigglegile
  • 开始时间 开始时间
G

gigglegile

Unregistered / Unconfirmed
GUEST, unregistred user!
我要在edit中输入ip地址,能够自动判断每个点之间的输入最多的数字在3个以内,
我写了一点,下面没有思路了,大家看看啊
procedure TFrmUDP.edtSetNewIPKeyPress(Sender: TObject
var Key: Char);
begin
if not(key in ['0'..'9',#46,#8,#13]) then
begin
showmessage('请输入正确的ip地址');
exit;
end
else
begin

end;
end;
 
这样子是友好的,错误提示不能中断用户的输入,你强迫用户点你的对话框,这样不好
最好用label在一边显示信息,或用MaskEdit也比较合适
 
用正则表达式吧
这个正则((2[0-4]/d|25[0-5]|[01]?/d/d?)/.){3}(2[0-4]/d|25[0-5]|[01]?/d/d?)是网上的关于IP地址的
测试发现不对
对于15986.126.0.9 这样的IP地址他也匹配
 
下面呢??没有了吗??
 
你分别放入四个EDIT,然后紧沿着,相当于放在一排了,分别判断一下,如大于255就不能输入,不知这样是否可以满足你。
 
晕!
问题已经解决了!
写一个函数就行了!我在网上找到的!
帖给大家看看,一起学习!

function IsIp(Str:string):Boolean;
var
I,K,DotCnt : Integer;
Num: string;
Arr: Array [1..4] of string;
begin
Result := False;
DotCnt := 0;
//由'0'..'9', '.'组成
For I := 1 to Length(Str) do
begin
if Not (Str in ['0'..'9','.']) then
Exit
else
if Str='.' then
inc(DotCnt);
end;
//点分隔符号数量应该=3
if DotCnt <>3 then Exit;
For K := 1 to 3 do
begin
I:= Pos('.',Str);
Num := Copy(Str,1, I-1);
Delete(Str,1,I);
Arr[K]:= Num;
end;
Arr[4]:=Str;

try
DotCnt:=0;
For I := 1 to 4 do
begin
K:= StrToInt(Arr);
if ((K>=0) and (K<=255)) then //每字节的值应介于0~255之间
Inc(DotCnt);
end;

if(DotCnt=4) then //四个字节都满足0~255之间,则合法
Result:=True;
except
end;
end;
 
哈哈,DELPHI,自带控件IdNetworkCalculator1,就可以.
IdNetworkCalculator1.IsAddressInNetwork('1212.1.1.1')
gigglegile你不像有些人,自己知道解决了,就结贴.没下文.
技术大家共享.
 
谢谢jey,其实我好多delphi知识都是从delphibbs上学到的,要知恩图报吗,所以我也想让更多的新来的富翁们能学到更多东西,壮大delphi的实力!!
晕...说的越来越远了...汗...
 
其实用三方控件也挺好啊,有个专门的IP三方控件!
 
直接使用windows的ip控件就可以了
不过可能麻烦一点~~
 
if (not IsLegalIP(EdtBegin.Text))or(not IsLegalIP(EdtEnd.Text)) then
begin
ShowMessage('ip地址非法,请重新输入');
exit;
end
 
这是人家写的
unit IPEdit;
interface
uses Windows, Messages, SysUtils, Classes,
Forms, Controls, ComCtrls, CommCtrl;

type
TFieldRange = record
LowRange: Byte;
HighRange: Byte;
end;

TFieldChangeEvent = procedure(Sender: TObject
OldField, Value: Byte) of Object;

TIPEdit = class(TWinControl)
private
FIP: LongWord;
FFields:array[0..3] of Byte;
FFieldRanges: array [0..3] of TFieldRange;
FCreating: Boolean;
FOnChange: TNotifyEvent;
FOnEnter: TNotifyEvent;
FOnExit: TNotifyEvent;
FOnFieldChange: TFieldChangeEvent;
procedure SetIP(Value: LongWord);
function GetIP: LongWord;
function GetField(Index:Integer):Byte;
procedure SetField(Index:Integer
B:Byte);
function GetFieldRange(Field: Integer): TFieldRange;
procedure SetFieldRange(Field:integer
Value: TFieldRange);
procedure SetIPAddress;
function GetBlank: Boolean;
procedure WMNotifyFormat(var Message: TMessage)
message WM_NOTIFYFORMAT;
//处理IP控件的通知消息IPN_FIELDCHANGED
procedure CNNotify(var Message: TWMNotify)
message CN_NOTIFY;
procedure CNCommand(var Message: TWMCommand)
message CN_COMMAND;
protected
procedure CreateParams(var Params: TCreateParams)
override;
procedure CreateWnd
override;
procedure DestroyWnd
override;
public
constructor Create(AOwner: TComponent)
override;
destructor Destroy
override;
//清除IP控件中的IP串
procedure Clear;
//设置IP控件的输入焦点(field的有效取值为0..3)
procedure SetFieldFocus(Field:Byte);
published
//判断IP控件的IP串是否为空
property Blank: Boolean read GetBlank;
//Field0到Field3分别为IP控件的4个部分的值
property Field0: Byte index 0 read GetField write SetField;
property Field1: Byte index 1 read GetField write SetField;
property Field2: Byte index 2 read GetField write SetField;
property Field3: Byte index 3 read GetField write SetField;
//Field0Range到Field3Range限制IP控件各部分的取值的范围
property Field0Range: TFieldRange index 0 read GetFieldRange write SetFieldRange;
property Field1Range: TFieldRange index 1 read GetFieldRange write SetFieldRange;
property Field2Range: TFieldRange index 2 read GetFieldRange write SetFieldRange;
property Field3Range: TFieldRange index 3 read GetFieldRange write SetFieldRange;
//IP地址值(32位整数LongWord)
property IP: LongWord read GetIP write SetIP;
//事件属性
property OnChange: TNotifyEvent read FOnChange write FOnChange;
property OnEnter: TNotifyEvent read FOnEnter write FOnEnter;
property OnExit: TNotifyEvent read FOnExit write FOnExit;
property OnFieldChange: TFieldChangeEvent read FOnFieldChange write FOnFieldChange;
//以下属性继承自TWinControl控件
property Enabled;
property Font;
property TabOrder;
property TabStop;
property ParentShowHint;
property ShowHint;
property Hint;
property Visible;
end;

procedure Register;

implementation

{~~~~~~~~~~~~~~~~~~~~~~TIPEdit~~~~~~~~~~~~~~~~~~~~~~~~}
constructor TIPEdit.Create(AOwner: TComponent);
var
i: integer;
begin
//初始化ICC_INTERNET_CLASSES类控件
CheckCommonControl(ICC_INTERNET_CLASSES);
inherited Create(AOwner);
for i:= 0 to 3 do
begin
FFieldRanges.LowRange:= 0;
FFieldRanges.HighRange:= 255;
FFields:=0;
end;
FIP:=0;
Height:= 25;
Width:= 152;
TabSTop:= True;
end;

procedure TIPEdit.DestroyWnd;
begin
inherited DestroyWnd
end;

destructor TIPEdit.Destroy;
begin
inherited Destroy;
end;

procedure TIPEdit.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
CreateSubClass(Params, WC_IPADDRESS);
with Params do
begin
Height := 20;
end;
end;

procedure TIPEdit.CreateWnd;
begin
FCreating := True;
try
inherited CreateWnd;
SetIPAddress;
finally
FCreating := False;
end;
end;

function TIPEdit.GetBlank: Boolean;
begin
Result:= Boolean(SendMessage(Handle, IPM_ISBLANK, 0, 0));
end;

procedure TIPEdit.Clear;
begin
SendMessage(Handle, IPM_CLEARADDRESS, 0, 0);
end;

procedure TIPEdit.SetFieldFocus(Field:Byte);
begin
SendMessage(Handle, IPM_SETFOCUS, Field, 0);
end;

function TIPEdit.GetFieldRange(Field: Integer): TFieldRange;
begin
Result:= FFieldRanges[Field];
end;

procedure TIPEdit.SetFieldRange(Field: Integer
Value: TFieldRange);
begin
if Value.LowRange > Value.HighRange then exit;
if (FFieldRanges[Field].LowRange <> Value.LowRange) or
(FFieldRanges[Field].HighRange <> Value.HighRange) then
begin
FFieldRanges[Field]:= Value;
SendMessage(Handle, IPM_SETRANGE, Field,
MakeIPRange(Value.LowRange, Value.HighRange));
end;
end;

function TIPEdit.GetField(Index: Integer): Byte;
begin
if (Index>=0)and(Index<=3) then Result:=FFields[Index]
else Result:=0;
end;

procedure TIPEdit.SetField(Index:Integer
B:Byte);
begin
if (FFields[Index] <> B)then
begin
FFields[Index]:=B;
SetIPAddress;
end;
end;

procedure TIPEdit.SetIPAddress;
var
i:LongWord;
begin
i:=MAKEIPADDRESS(FFields[0], FFields[1], FFields[2], FFields[3]);
SendMessage(Handle, IPM_SETADDRESS, 0, i);
FIP:=i;
end;

procedure TIPEdit.SetIP(Value: LongWord);
begin
if (FIP <> Value) then
begin
FFields[0]:= First_IPAddress(Value);
FFields[1]:= Second_IPAddress(Value);
FFields[2]:= Third_IPAddress(Value);
FFields[3]:= Fourth_IPAddress(Value);
SetIPAddress;
end;
end;

function TIPEdit.GetIP: LongWord;
begin
SendMessage(Handle, IPM_GETADDRESS, 0, Integer(@Result));
end;

procedure TIPEdit.WMNotifyFormat(var Message: TMessage);
begin
with Message do
Result := DefWindowProc(Handle, Msg, WParam, LParam);
end;

procedure TIPEdit.CNNotify(var Message: TWMNotify);
var
pNM: PNMIPAddress;
begin
with (Message.NMHdr)^ do
begin
case Code of
IPN_FIELDCHANGED:
begin
pNM:= PNMIPADDRESS(Message.NMHdr);
if (pNM^.iField>=0)and(pNM^.iField<=3) then
FFields[pNM^.iField]:=pNM^.iValue;
if Assigned(FOnFieldChange) then
FOnFieldChange(self, pNM^.iField, pNM^.iValue);
end;
end;
end;
end;

procedure TIPEdit.CNCommand(var Message: TWMCommand);
begin
case Message.NotifyCode of
EN_CHANGE:
begin
if not FCreating then
if Assigned(FOnChange) then FOnChange(self);
end;
EN_KILLFOCUS: if Assigned(FOnExit) then FOnExit(self);
EN_SETFOCUS: if Assigned(FOnEnter) then FOnEnter(self);
end;
end;

//注册TIPEdit控件以及TAboutProperty属性编辑器
procedure Register;
begin
RegisterComponents('Additional', [TIPEdit]);
end;
end.
 
haha,lake_cx,又是你啊!
你的东西好长啊!
不过大家慢慢学习!
 
上面那个是windiws的IP控件,就像网络连接里设置IP的一样,新建个包,包含那个单元,安装一下就行了
 
后退
顶部