Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Probably an easy question about function.

Status
Not open for further replies.

draakans

Programmer
Dec 20, 2007
13
CA
Hello,
I have a question that should be easy for most of you guys.

I'm not well experienced with Delphi and I need to know a way to make a function out of this:

If (Checklistbox1.Checked[0]) then begin if (StrIsDigit(Pr01.Text, Pr01.GetTextLen)) then MainIni.WriteString('PRISES', 'PR01', Pr01.text) else begin Once:=TRUE;Showmessage('Quantite a: ' + CheckListBox1.Items[0] + ' n''est pas valide');end;end;
If (Checklistbox1.Checked[1]) then begin if (StrIsDigit(PR02.Text, PR02.GetTextLen)) then MainIni.WriteString('PRISES', 'PR02', PR02.text) else begin Once:=TRUE;Showmessage('Quantite a: ' + CheckListBox1.Items[1] + ' n''est pas valide');end;end;
If (Checklistbox1.Checked[2]) then begin if (StrIsDigit(PR03.Text, PR03.GetTextLen)) then MainIni.WriteString('PRISES', 'PR03', PR03.text) else begin Once:=TRUE;Showmessage('Quantite a: ' + CheckListBox1.Items[2] + ' n''est pas valide');end;end;
If (Checklistbox1.Checked[3]) then begin if (StrIsDigit(PR04.Text, PR04.GetTextLen)) then MainIni.WriteString('PRISES', 'PR04', PR04.text) else begin Once:=TRUE;Showmessage('Quantite a: ' + CheckListBox1.Items[3] + ' n''est pas valide');end;end;
If (Checklistbox1.Checked[4]) then begin if (StrIsDigit(PR05.Text, PR05.GetTextLen)) then MainIni.WriteString('PRISES', 'PR05', PR05.text) else begin Once:=TRUE;Showmessage('Quantite a: ' + CheckListBox1.Items[4] + ' n''est pas valide');end;end;
If (Checklistbox1.Checked[5]) then begin if (StrIsDigit(PR06.Text, PR06.GetTextLen)) then MainIni.WriteString('PRISES', 'PR06', PR06.text) else begin Once:=TRUE;Showmessage('Quantite a: ' + CheckListBox1.Items[5] + ' n''est pas valide');end;end;
If (Checklistbox1.Checked[6]) then begin if (StrIsDigit(PR07.Text, PR07.GetTextLen)) then MainIni.WriteString('PRISES', 'PR07', PR07.text) else begin Once:=TRUE;Showmessage('Quantite a: ' + CheckListBox1.Items[6] + ' n''est pas valide');end;end;
If (Checklistbox1.Checked[7]) then begin if (StrIsDigit(PR08.Text, PR08.GetTextLen)) then MainIni.WriteString('PRISES', 'PR08', PR08.text) else begin Once:=TRUE;Showmessage('Quantite a: ' + CheckListBox1.Items[7] + ' n''est pas valide');end;end;
If (Checklistbox1.Checked[8]) then begin if (StrIsDigit(PR09.Text, PR09.GetTextLen)) then MainIni.WriteString('PRISES', 'PR09', PR09.text) else begin Once:=TRUE;Showmessage('Quantite a: ' + CheckListBox1.Items[8] + ' n''est pas valide');end;end;
If (Checklistbox1.Checked[9]) then begin if (StrIsDigit(PR10.Text, PR10.GetTextLen)) then MainIni.WriteString('PRISES', 'PR10', PR10.text) else begin Once:=TRUE;Showmessage('Quantite a: ' + CheckListBox1.Items[9] + ' n''est pas valide');end;end;
If (Checklistbox1.Checked[10]) then begin if (StrIsDigit(PR11.Text, PR11.GetTextLen)) then MainIni.WriteString('PRISES', 'PR11', PR11.text) else begin Once:=TRUE;Showmessage('Quantite a: ' + CheckListBox1.Items[10] + ' n''est pas valide');end;end;
If (Checklistbox1.Checked[11]) then begin if (StrIsDigit(PR12.Text, PR12.GetTextLen)) then MainIni.WriteString('PRISES', 'PR12', PR12.text) else begin Once:=TRUE;Showmessage('Quantite a: ' + CheckListBox1.Items[11] + ' n''est pas valide');end;end;
If (Checklistbox1.Checked[12]) then begin if (StrIsDigit(PR13.Text, PR13.GetTextLen)) then MainIni.WriteString('PRISES', 'PR13', PR13.text) else begin Once:=TRUE;Showmessage('Quantite a: ' + CheckListBox1.Items[12] + ' n''est pas valide');end;end;
If (Checklistbox1.Checked[13]) then begin if (StrIsDigit(PR14.Text, PR14.GetTextLen)) then MainIni.WriteString('PRISES', 'PR14', PR14.text) else begin Once:=TRUE;Showmessage('Quantite a: ' + CheckListBox1.Items[13] + ' n''est pas valide');end;end;
If (Checklistbox1.Checked[14]) then begin if (StrIsDigit(PR15.Text, PR15.GetTextLen)) then MainIni.WriteString('PRISES', 'PR15', PR15.text) else begin Once:=TRUE;Showmessage('Quantite a: ' + CheckListBox1.Items[14] + ' n''est pas valide');end;end;
If (Checklistbox1.Checked[15]) then begin if (StrIsDigit(PR16.Text, PR16.GetTextLen)) then MainIni.WriteString('PRISES', 'PR16', PR16.text) else begin Once:=TRUE;Showmessage('Quantite a: ' + CheckListBox1.Items[15] + ' n''est pas valide');end;end;
If (Checklistbox1.Checked[16]) then begin if (StrIsDigit(PR17.Text, PR17.GetTextLen)) then MainIni.WriteString('PRISES', 'PR17', PR17.text) else begin Once:=TRUE;Showmessage('Quantite a: ' + CheckListBox1.Items[16] + ' n''est pas valide');end;end;

I use a CheckListBox with items that were red from an textfile. The code is: If the number written on the Pr##.text (which is a TEditBox) then Write on an Ini file the quantity. Else showmessage Invalid quantity.

I have over 100 items, so it makes more than 100 If that could be done in 1 function... I just don't know how to pass and increment the item name.

thank you :)
 
I don't have the code handy but if you set the tag value of your text boxes (PR12, PR13, etc) to match the checkbox then you can have a function like:
Code:
for i := 0 to GetLastCheckbox-1 do begin
  if Checklistbox1.Checked[i] then begin
    if(StrIsDigit(GetTextBoxTextFromTag(i)) then
      MainIni.WriteString('PRISES', 'PR'+inttostr(i),GetTextBoxTextFromTag(i))
    else
      Once := TRUE;

  //and so on


  end;
end;
 
Thanks for you reply.

I don't understand how to do the : GetTextBoxTextFromTag function. I gave the tag value to each text boxes as you said. But I don't know how to call a brilliant function using it.

I mean, I can't use "PR+IntToStr(tag).text" since it's an EditBox.



 

Create the edit boxes with numbered names or make them manualy if you like

Code:
EB := TEdit.Create(Parentform);
  with EB do
    begin
       name := 'EditBox'+inttostr(count);
       Enabled := False;
       Parent := ParentForm;


Use FindComponent() (built in function) to get the editbox you want and read or manipulte its properties

Code:
Procedure TParentForm.NameBox(m: integer; NewName: string);
var EBox: TComponent;
begin
   EBox := findcomponent('EditBox' + inttostr(m));
   TEdit(EBox).Text := NewName
end;

Steve: N.M.N.F.
If something is popular, it must be wrong: Mark Twain
 
Yeah I think that will do the work properly! :) Thanks a lot to all of you guys. These are great ideas.

I think you can mark this thread as Solved.
 
I would take it to the next level and write it as a property and get/put the data to the INI file. Here's an example of what I'm talking about from one of my apps:
Code:
type
  TQcIniDlg = class(TForm)
    EditQcDataDir: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    ..
  private
    { Private declarations }
    function GetQcDataDir: string;
    procedure SetQcDataDir(const Value: string);
    ..
  public
    { Public declarations }
    QcIniFile: TIniFile;
    property LastDEImport: TDate read GetLastDEImport write SetLastDEImport;
  end;

var
  QcIniDlg: TQcIniDlg;
  DefaultDb: string; // = 'C:\Program Files\QCMenu\@Data';
  AppPath: string;

implementation

{$R *.dfm}

function TQcIniDlg.GetQcDataDir: string;
begin
  result:= QcIniFile.ReadString('IO', 'QcDataDir', DefaultDb)
end;

procedure TQcIniDlg.SetQcDataDir(const Value: string);
begin
  QcIniFile.WriteString('IO', 'QcDataDir', Value)
end;

procedure TQcIniDlg.FormCreate(Sender: TObject);
begin
  AppPath:= ExtractFilePath(Application.ExeName);
  QcIniFile:= TIniFile.Create(AppPath + 'QcMenu.ini');
  ..
  EditQcDataDir.Text:= QcDataDir;
end;

procedure TQcIniDlg.FormDestroy(Sender: TObject);
begin
  FreeAndNil(QcIniFile)
end;
The above shows just one of several INI values this form handles. Each value has an edit-control that reads the value from the INI file (in OnCreate) and displays it in the control. When saved, all values are written back to the INI file. All via the Property.

Roo
Delphi Rules!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top