?
Solved

Combining 2 procedures

Posted on 2006-11-08
6
Medium Priority
?
168 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

770 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