Hallo zusammen,
ich habe eine Eingabeprüfung für Integer und Gleitpunktzahlen programmiert und finde die Implementierung eher suboptimal, da der gleiche Code 2x geschrieben ist. Der Grund ist, dass ich keinen gemeinsamen Typ für TPanel und TGroupBox habe.
Die Situaltion:
Auf meinem Formular befinden sich Panels und in den Panels ggf. weitere Panels oder GroupBoxen. Innerhalb dieser Unterobjekte befinden sich dann die Editboxen. Die Namen der Editboxen enthalten den Hinweis, ob ein Integer oder ein Gleitpunktwert eingegeben werden muss.
Um alle Editboxen zu erreichen muss ich das über einen rekursiven Aufruf umsetzen. Der Aufruf braucht aber das untergeordnete Objekt als Parameter. Dieses kann nun ein Panel oder eine GroupBox sein. Wie kann ich die beiden folgenden Funktionen zusammenfassen? Beide tun das Selbe, nur eine bekommt das Panel als Parameter, die andere die GroupBox:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90:
| function boCheckPanelInput(Const oPanel :TPanel) : Boolean; var boReturn : Boolean; szShortCut : String; icnI : Integer; begin
boReturn := True;
icnI := 0; while icnI <= oPanel.ControlCount - 1 do begin if (oPanel.Controls[icnI] is TEdit) then begin szShortCut := copy(oPanel.Name,4,3); if (LowerCase(copy(oPanel.Controls[icnI].Name,4,6))=Lowercase(szShortCut + 'Int')) and not(boIsStrInt((oPanel.Controls[icnI] as TEdit).Text)) then boReturn := False; if (LowerCase(copy(oPanel.Controls[icnI].Name,4,6))=Lowercase(szShortCut + 'Flt')) and not(boIsStrDbl((oPanel.Controls[icnI] as TEdit).Text, True)) then boReturn := False;
if not (boReturn) then begin
MessageDlg('Falsche Eingabe',mtInformation, [mbOk], 0); (oPanel.Controls[icnI] as TEdit).SelStart := 0; (oPanel.Controls[icnI] as TEdit).SelLength := Length((oPanel.Controls[icnI] as TEdit).Text); (oPanel.Controls[icnI] as TEdit).SetFocus; end; end else if (oPanel.Controls[icnI] is TPanel) then begin boReturn := boCheckPanelInput((oPanel.Controls[icnI]) as TPanel); end else if (oPanel.Controls[icnI] is TGroupBox) then begin boReturn := boCheckGroupBoxInput((oPanel.Controls[icnI]) as TGroupBox); end; if not (boReturn) then icnI := oPanel.ControlCount; inc(icnI); end; Result := boReturn; end;
function boCheckGroupBoxInput(Const oGroupBox :TGroupBox) : Boolean; var boReturn : Boolean; szShortCut : String; icnI : Integer; begin
boReturn := True;
icnI := 0; while icnI <= oGroupBox.ControlCount - 1 do begin if (oGroupBox.Controls[icnI] is TEdit) then begin szShortCut := copy(oGroupBox.Name,4,3); if (LowerCase(copy(oGroupBox.Controls[icnI].Name,4,6))=Lowercase(szShortCut + 'Int')) and not(boIsStrInt((oGroupBox.Controls[icnI] as TEdit).Text)) then boReturn := False; if (LowerCase(copy(oGroupBox.Controls[icnI].Name,4,6))=Lowercase(szShortCut + 'Flt')) and not(boIsStrDbl((oGroupBox.Controls[icnI] as TEdit).Text, True)) then boReturn := False;
if not (boReturn) then begin
MessageDlg('Falsche Eingabe',mtInformation, [mbOk], 0); (oGroupBox.Controls[icnI] as TEdit).SelStart := 0; (oGroupBox.Controls[icnI] as TEdit).SelLength := Length((oGroupBox.Controls[icnI] as TEdit).Text); (oGroupBox.Controls[icnI] as TEdit).SetFocus; end; end else if (oGroupBox.Controls[icnI] is TPanel) then begin boReturn := boCheckPanelInput((oGroupBox.Controls[icnI]) as TPanel); end else if (oGroupBox.Controls[icnI] is TGroupBox) then begin boReturn := boCheckGroupBoxInput((oGroupBox.Controls[icnI]) as TGroupBox); end;
if not (boReturn) then icnI := oGroupBox.ControlCount; inc(icnI); end; Result := boReturn; end; |