Solved

Combining 2 procedures

Posted on 2006-11-08
6
145 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

709 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now