Solved

Combining 2 procedures

Posted on 2006-11-08
6
146 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mydac connection data base issue 3 135
code issue 8 99
Tviruailstringtree sort multi columns on header click 1 52
how to update exe applicatio from internet ? 6 67
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

11 Experts available now in Live!

Get 1:1 Help Now