I have a new program I am working on and I would like to repeat a portion of code to redo the same thing again, and again.

When the program starts, it will give me a number from my NIC card (I think). Ok so I have it going to a TMemo. I have one button set up the clear the Tmemo, and I want to use another button to redo the function that produced the number in the first place. How do I do that?

I can provide code if needed.

Also, how would I, if it is any different to a major degree, repeat all code should that ever be necessary for me in the future.

Thanks for any and all help in advance! :)

ps. I forgot... This should be real easy, but I have searched for a good while and cant find an example. I am working on some error messages for a separate program. All I am wondering is how to show one error message for 3 different Tedit boxes. Seems silly to write the same error for 3 edit boxes. Basically the edit boxes need text in them, if no text is in any one of the boxes, an error message will appear. Thanks!

Just create a utility function to get the number. When your program starts, call the function. When you press the button, an event method (onClick) gets called. Inside Button1Click just call the function again.

Alas, for errors, it is usually simple enough just to write the messageBox( ... ) stuff over again. Borland compilers are very smart about optimizing strings, so you don't have to worry about bloating your code by the repetition.

However, if you know you'll use it the exact same way many, many times, you could just create a little method that calls the messageBox (or whatever the error message dialog function you are using is), then just call your method whenever the user error occurs.

Hope this helps.

"create a ("utility function") to get the number."

oh Duoas... HAHAHHA!!! cmon now... you know better than this.. lolol

"When your program starts, call the function. When you press the button, an event method (onClick) gets called. Inside Button1Click just call the function again."

When the program starts, the function is called i do believe... But to recall the function.. thats the stickler... I dont know what to put inside the button1click; I tried a few things that did not work.... but I cant learn without trying... Hence me being here. ;) Programming can be fun.. sometimes... :P

I dont want the code, but more in engrish that that would be nice. lol

Love you long time Duoas.. lol

so you are saying to write the error code out separately for each edit box?

if edit1.text <= '' then
 MessageDlg('whatever goes here',mtError,[mbOK],0)

if edit2.text <= '' then
 MessageDlg('same whatever here',mtError,[mbOK],0)

if edit3.text <= '' then
 MessageDlg('same whatever here',mtError,[mbOK],0)

there really isnt a way to make edit boxes to work together to make one error message without the other chinese (chinese because i dont have a clue what you were saying, lol) you wrote?

i tried edit1 + edit2 + edit3 and a myriad of other ideas to make one message work... the problem with this way is that you will receive the same error code for as many edit's are left without text... anyhoo.. thats fine... I just thought there would be a better way.

Thanks Duoas :)

Well, if you are checking them all for validity at the same time, then sure, just put them together:

if (edit1.text = '')
or (edit2.text = '')
or (edit3.text = '')
 then MessageDlg('whatever goes here',mtWarning,[mbOK],0)

But it is better to separate them, so that you can visually indicate which one is in error:

...

type
  TForm1 = class( TForm )
    ...
  private
    // Add yourself a function that checks for errors
    function check_edit_error( sender: tEdit ): boolean;
  end;

...

// Define it
function TForm1.check_edit_error( sender: tEdit ): boolean;
  begin
  // Was there an error?
  result := sender.text = '';
  // Yes, complain
  if result then begin
    messageDlg( 'fooey', mtWarning, [mbOK], 0 );
    sender.setFocus
    end
  end;

...

procedure TForm1.fooButtonClick( sender: tObject );
  begin
  // Check all the edit boxes we need right now for error
  if check_edit_error( edit1 )
  or check_edit_error( edit3 )
    then exit;

  ...

  end;

Don't use mtError except for errors that are fatal or nearly fatal to your application. For something like this I would use mtWarning or mtInformation, since the program is simply explaining to the user that it cannot continue until proper input is given.

Hope this helps.

Very big help.. darn syntax... I didnt add () around the edit.text boxes.. that was the problem. :\

Thanks also for the other info regarding not using mterror unless needed. I have also employed that as well.

Any idea on the original question of how to get a TButton to do something over again? I know you told me a way to do it, but I didnt quite get it. Thanks either way bud. :) Im going to dig around some more on it.

I have the button doing the function over and over again, but I did it by saying the the button click should perfom a FormCreate.

This cant be what you meant. can it?

I must have been really tired when last I posted because I thought that the beginning and end of this thread was two separate threads...

As per your original question, whatever you are doing in FormCreate that needs to be done again, just stick it in another, private method.

...

type
  TForm1 = class( TForm )
    ...
  private
    // Add yourself a method that does what you want
    procedure do_something;
  end;

...

// Define it
procedure TForm1.do_something;
  begin
  // does it here
  end;

...

procedure TForm1.FormCreate( sender: tObject );
  begin
  ...
  do_something;  // use it
  ...
  end;

procedure TForm1.fooButtonClick( sender: tObject );
  begin
  do_something;  // use it again
  ...
  end;

Your solution to just call FormCreate again is also OK, so long as FormCreate only does what you need done again. I would prefer to separate it out into a separate method just for logical consistency and to make you life easier should you later want to make FormCreate do something unrelated to clicking the button (as is wont to happen).

Hope this helps.

Actually, I am not doing anything on form create. There is a function or 2 that is being called when the program strarts. So what I did was went to the TButoon's events in the object inspector. That is where I added the formcreate in the onclick event. I dont think I can run a function from inside a procedure. If I can, I dont know how. I will go read more.

Thanks again for your help and patience.

"When the program starts" is actually a little vague.

Which functions/procedures are being called when your program starts?

Are they being called in the main program file's begin..end. block?
Or in some unit's initialization section?
Or in some form's constructor? or FormCreate event?

What I mean when saying when the program starts is when I "play" the program. There are 2 functions in the code the produce one number. The funcions are in the implementation part of the code.

I will show you what I have, I pulled most of this from googling on the net:

unit uMainForm;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function CoCreateGuid(P:Pointer):Integer;stdcall;external 'ole32.dll';

function GetNicAddr:AnsiString;
var
  T:AnsiString;
  I:Integer;
begin
  Result:='';
  SetLength(T,16);
  if CoCreateGUID(@T[1])=S_OK then begin
    for I:=11 to 15 do Result:=Result+IntToStr(Ord(T[I]))+'.';
    Result:=Result+IntToStr(Ord(T[16]));
  end;
end;

procedure TForm1.ClearTextButtonClick(Sender: TObject);
begin
Memo1.Clear;
end;

procedure TForm1.RedoFunctionButton(Sender: TObject);
begin
//nothing here...  the event handler does a formcreate to get another NIC number (which makes me think I am not getting the actual number anyway since the number changes everytime.
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
memo1.Lines.Text :=  GetNicAddr;
end;

end.

The Button making something happen again is the real lesson here; not the validity of the NIC number. Learning how to pull all the numbers from hardware in a computer is my next venture. To actually write a program that does that. It will be a long ordeal I am sure. :\

I hope this answers your question.

Ah, yes, that works.

Still, I would repeat myself instead of calling FormCreate again.

procedure TForm1.RedoFunctionButton(Sender: TObject);
begin
memo1.Lines.Text :=  GetNicAddr;
end;

Have fun!

commented: This man's patience with me is super-human. +1

I did what you suggested (and admittedly feel QUITE STUPID for not thinking of this before posting my question).

WOW at the simplicity of the answers to my problems....

Solved. Thank you Duoas.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.