Solved

Combining 2 procedures

Posted on 2006-11-08
6
148 Views
Last Modified: 2010-04-05
Dear Experts,

How can I combine 2 procedures called Open1Click.

And I have made the second procedure Open1Click myself
and it is correct, but I think i could be done better.

procedure TForm1.Open1Click(Sender: TObject);
var
  sFileName: String;
  i: Integer;
begin
  if OpenDialog1.Execute then
  begin
    sFileName := OpenDialog1.FileName;
    ShellExecute(Handle, 'open', PChar( sFilename), nil, nil, SW_SHOWNORMAL);
    i := FRecentFiles.IndexOf( sFileName);
    if i >= 0 then
      FRecentFiles.Delete( i);
    ReloadFileList( sFileName);
  end;
end;

procedure TMainForm.Open1Click(Sender: TObject);
begin
  if IsModified then
  begin
    case MessageDlg('Do you want to save the current file?', mtConfirmation,
      [mbYes, mbNo, mbCancel], 0) of
      mrYes:
        begin
          Save1Click(Sender);
          if SocOpen then Warning2
          else begin
            OpenDialog1.InitialDir := ExtractFileDir(Application.exename) + '\Sessions\';
            OpenDialog1.DefaultExt := 'emu';
            if OpenDialog1.Execute then
            begin
              Save1Click(Sender);
              LoadOptions(OpenDialog1.Filename);
              CurrentFile := OpenDialog1.FileName;
              MakeConnect
            end
            else begin
              OpenDialog1.InitialDir := ExtractFileDir(Application.exename) + '\Sessions\';
              OpenDialog1.DefaultExt := 'emu';
              if OpenDialog1.Execute then
              begin
                LoadOptions(OpenDialog1.Filename);
                CurrentFile := OpenDialog1.FileName;
                MakeConnect;
              end;
            end;
          end;
        end;
      mrNo:
        begin
          if SocOpen then Warning2
          else begin
            OpenDialog1.InitialDir := ExtractFileDir(Application.exename) + '\Sessions\';
            OpenDialog1.DefaultExt := 'emu';
            if OpenDialog1.Execute then
            begin
              LoadOptions(OpenDialog1.Filename);
              CurrentFile := OpenDialog1.FileName;
              MakeConnect;
            end;
          end;
        end;
      mrCancel: ;
    end;
  end
  else
    if socOpen then Warning2
    else begin
      OpenDialog1.InitialDir := ExtractFileDir(Application.exename) + '\Sessions\';
      OpenDialog1.DefaultExt := 'emu';
      if OpenDialog1.Execute then
      begin
        LoadOptions(OpenDialog1.Filename);
        CurrentFile := OpenDialog1.FileName;
        MakeConnect;
      end;
    end;
end;

Greetings,

P.Kiers
0
Comment
Question by:peterkiers
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:BdLm
ID: 17902072
1) in unit Mainform include unit Form with a use statement:
->  uses FilenameForm1;   //  Filename of Form1 Unit
2)  change code for procedure TMainForm.Open1Click
-> like:
procedure TMainForm.Open1Click(Sender: TObject);
begin
        Form1.Open1Click(Sender);

         .....  (  your other code)

end;

3)  you can also call from Form1 -> MainForm.Openclick  with these 2 steps above.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 17902262
Oeh, I need some time to figure this out.

Peter
0
 
LVL 28

Expert Comment

by:TName
ID: 17902342
Maybe I get it wrong myself, but I suspect this a misunderstanding.

Peter, do you have a form called Mainform AND a form called Form1, as I believe BdLm understandably interpreted your code to suggest, or do you just want to merge 2 procedures from 2 different projects, and one of the classes (TMainForm or TForm1) does NOT exist in your final project?

0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 1

Author Comment

by:peterkiers
ID: 17902395
Sorry, I didn't display the procedures right.

Their both in the same form, and I want them merge togetter.

procedure TMainForm.Open1Click(Sender: TObject);
var
  sFileName: String;
  i: Integer;
begin
  if OpenDialog1.Execute then
  begin
    sFileName := OpenDialog1.FileName;
    ShellExecute(Handle, 'open', PChar( sFilename), nil, nil, SW_SHOWNORMAL);
    i := FRecentFiles.IndexOf( sFileName);
    if i >= 0 then
      FRecentFiles.Delete( i);
    ReloadFileList( sFileName);
  end;
end;

procedure TMainForm.Open1Click(Sender: TObject);
begin
  if IsModified then
  begin
    case MessageDlg('Do you want to save the current file?', mtConfirmation,
      [mbYes, mbNo, mbCancel], 0) of
      mrYes:
        begin
          Save1Click(Sender);
          if SocOpen then Warning2
          else begin
            OpenDialog1.InitialDir := ExtractFileDir(Application.exename) + '\Sessions\';
            OpenDialog1.DefaultExt := 'emu';
            if OpenDialog1.Execute then
            begin
              Save1Click(Sender);
              LoadOptions(OpenDialog1.Filename);
              CurrentFile := OpenDialog1.FileName;
              MakeConnect
            end
            else begin
              OpenDialog1.InitialDir := ExtractFileDir(Application.exename) + '\Sessions\';
              OpenDialog1.DefaultExt := 'emu';
              if OpenDialog1.Execute then
              begin
                LoadOptions(OpenDialog1.Filename);
                CurrentFile := OpenDialog1.FileName;
                MakeConnect;
              end;
            end;
          end;
        end;
      mrNo:
        begin
          if SocOpen then Warning2
          else begin
            OpenDialog1.InitialDir := ExtractFileDir(Application.exename) + '\Sessions\';
            OpenDialog1.DefaultExt := 'emu';
            if OpenDialog1.Execute then
            begin
              LoadOptions(OpenDialog1.Filename);
              CurrentFile := OpenDialog1.FileName;
              MakeConnect;
            end;
          end;
        end;
      mrCancel: ;
    end;
  end
  else
    if socOpen then Warning2
    else begin
      OpenDialog1.InitialDir := ExtractFileDir(Application.exename) + '\Sessions\';
      OpenDialog1.DefaultExt := 'emu';
      if OpenDialog1.Execute then
      begin
        LoadOptions(OpenDialog1.Filename);
        CurrentFile := OpenDialog1.FileName;
        MakeConnect;
      end;
    end;
end;

Peter.
0
 
LVL 8

Accepted Solution

by:
BdLm earned 500 total points
ID: 17904825
one remark:  it is a good programming style to separate between GUI related code and real Algo  and data processing code.

step 1: add a new function to your class

type
  TMainForm = class(TForm)

    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private

    { Private-Deklarationen }
  public
    { Public-Deklarationen }
     procedure MyNewFunction (Filename: String);

  end;


procedure TMainForm.Open1Click(Sender: TObject);
var
  sFileName: String;
  i: Integer;
begin
  if OpenDialog1.Execute then
  begin
    sFileName := OpenDialog1.FileName;
    ShellExecute(Handle, 'open', PChar( sFilename), nil, nil, SW_SHOWNORMAL);
    i := FRecentFiles.IndexOf( sFileName);
    if i >= 0 then
      FRecentFiles.Delete( i);
    ReloadFileList( sFileName);
  end;

  //  call the new function ...

 MyNewFunction (sFileName);
end;

procedure TMainForm. MyNewFunction (Filename: String);

begin
  if IsModified then
  begin
    case MessageDlg('Do you want to save the current file?', mtConfirmation,
      [mbYes, mbNo, mbCancel], 0) of
      mrYes:
        begin

       .....


       LoadOptions(Filename);


end;



0
 
LVL 1

Author Comment

by:peterkiers
ID: 17905354
Thank you very much for the advice.

Peter Kiers
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TVirtualStringTree  search using TEdit 7 121
FMX enumerated colours 2 100
Help on project with Soap 10 54
Convert a string into a TDateTime 5 61
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question