Solved

Combining 2 procedures

Posted on 2006-11-08
6
154 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
Independent Software Vendors: 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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