在游戏配置过程中有一些较长的过程,例如下边判断输入端口合法性的:
{如果较少,还可以挨个判断}
procedure TfrmMain.ButtonNext3Click(Sender: TObject);
var
nPort: Integer;
begin
nPort := StrToIntDef(Trim(EditSelGate_GatePort.Text), -1);
if (nPort < 0) or (nPort > 65535) then begin
Application.MessageBox('网关端口设置错误!!!', '错误信息', MB_OK + MB_ICONERROR);
EditSelGate_GatePort.SetFocus;
Exit;
end;
g_Config.SelGate.GatePort[0] := nPort;
nPort := StrToIntDef(Trim(EditSelGate_GatePort1.Text), -1);
if (nPort < 0) or (nPort > 65535) then begin
Application.MessageBox('网关端口设置错误!!!', '错误信息', MB_OK + MB_ICONERROR);
EditSelGate_GatePort1.SetFocus;
Exit;
end;
g_Config.SelGate.GatePort[1] := nPort;
PageControl3.ActivePageIndex := 3;
end;
{如果较多,源代码里边是这样判断的,很明显,比较冗长}
procedure TfrmMain.ButtonNext4Click(Sender: TObject);
var
nPort1, nPort2, nPort3, nPort4, nPort5, nPort6, nPort7, nPort8: Integer;
begin
nPort1 := StrToIntDef(Trim(EditRunGate_GatePort1.Text), -1);
nPort2 := StrToIntDef(Trim(EditRunGate_GatePort2.Text), -1);
nPort3 := StrToIntDef(Trim(EditRunGate_GatePort3.Text), -1);
nPort4 := StrToIntDef(Trim(EditRunGate_GatePort4.Text), -1);
nPort5 := StrToIntDef(Trim(EditRunGate_GatePort5.Text), -1);
nPort6 := StrToIntDef(Trim(EditRunGate_GatePort6.Text), -1);
nPort7 := StrToIntDef(Trim(EditRunGate_GatePort7.Text), -1);
nPort8 := StrToIntDef(Trim(EditRunGate_GatePort8.Text), -1);
if (nPort1 < 0) or (nPort1 > 65535) then begin
Application.MessageBox('网关一端口设置错误!!!', '错误信息', MB_OK + MB_ICONERROR);
EditRunGate_GatePort1.SetFocus;
Exit;
end;
if (nPort2 < 0) or (nPort2 > 65535) then begin
Application.MessageBox('网关二端口设置错误!!!', '错误信息', MB_OK + MB_ICONERROR);
EditRunGate_GatePort2.SetFocus;
Exit;
end;
if (nPort3 < 0) or (nPort3 > 65535) then begin
Application.MessageBox('网关三端口设置错误!!!', '错误信息', MB_OK + MB_ICONERROR);
EditRunGate_GatePort3.SetFocus;
Exit;
end;
if (nPort4 < 0) or (nPort4 > 65535) then begin
Application.MessageBox('网关四端口设置错误!!!', '错误信息', MB_OK + MB_ICONERROR);
EditRunGate_GatePort4.SetFocus;
Exit;
end;
if (nPort5 < 0) or (nPort5 > 65535) then begin
Application.MessageBox('网关五端口设置错误!!!', '错误信息', MB_OK + MB_ICONERROR);
EditRunGate_GatePort5.SetFocus;
Exit;
end;
if (nPort6 < 0) or (nPort6 > 65535) then begin
Application.MessageBox('网关六端口设置错误!!!', '错误信息', MB_OK + MB_ICONERROR);
EditRunGate_GatePort6.SetFocus;
Exit;
end;
if (nPort7 < 0) or (nPort7 > 65535) then begin
Application.MessageBox('网关七端口设置错误!!!', '错误信息', MB_OK + MB_ICONERROR);
EditRunGate_GatePort7.SetFocus;
Exit;
end;
if (nPort8 < 0) or (nPort8 > 65535) then begin
Application.MessageBox('网关八端口设置错误!!!', '错误信息', MB_OK + MB_ICONERROR);
EditRunGate_GatePort8.SetFocus;
Exit;
end;
g_Config.RunGate.GatePort[0] := nPort1;
g_Config.RunGate.GatePort[1] := nPort2;
g_Config.RunGate.GatePort[2] := nPort3;
g_Config.RunGate.GatePort[3] := nPort4;
g_Config.RunGate.GatePort[4] := nPort5;
g_Config.RunGate.GatePort[5] := nPort6;
g_Config.RunGate.GatePort[6] := nPort7;
g_Config.RunGate.GatePort[7] := nPort8;
PageControl3.ActivePageIndex := 4;
end;
可以看到其实就是这么一回事:
1.读取输入到edit里的端口号.
2.检测是否在正常范围内(0..65535).
3.如果在就写入配置文件( g_Config.RunGate.GatePort[0] := nPort1;).
上边的3条已经把整个过程抽象出来了,因为edit是按编号来起名的,在这里可以用findcomponent来顺序查找检测,然后赋值.改成下边这样会好些:
{检查RunGate网关端口填写是否正确}
procedure TfrmMain.ButtonNext4Click(Sender: TObject);
var
NPort: array[0..7] of Integer;//定义端口数组
i: Integer;
MsgStr:string;
EditGate:TEdit; //定义edit控件
begin
for I := 0 to 7 do
begin
EditGate:=FindComponent('editrunGate_GatePort'+inttostr(i+1)) as TEdit;
NPort[i] := StrToIntDef(Trim(EditGate.Text), -1);
if (NPort[i] < 0) or (NPort[i] > 65535) then
begin
MsgStr:='网关端口'+inttostr(i+1)+'设置错误!!!';
Application.MessageBox(PChar(msgstr), '错误信息', MB_OK + MB_ICONERROR);
EditGate.SetFocus;;
Exit;
end
else
begin
g_Config.RunGate.GatePort[i] := NPort[i];
end;
end;
PageControl3.ActivePageIndex := 4;
end;
明显精炼了不少,其实可以用spinedit来控制的,连这个都省了,通过写这个是突然想到了findcomponen,练习一下它的用法,但这个过程还不是通用的,只适合在这里使用,写成函数就没必要了.通过自己动手,也算是学到了一点东西.