Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As others have stated, there are only so many things that can go wrong with the code you have shown:</p> <p>1) the <code>TMenuItem.Tag</code> may contain the wrong value.</p> <p>2) <code>TempResultFile</code> may not be assigned a valid <code>TEdit</code> pointer. Despite what others have said, leaving the variable uninitialized DOES NOT guarantee that an Access Violation will occur, though it is likely. There is also the possibility that if the <code>TEdit</code> was not created correctly, or has been freed, that the pointer being assigned may be nil. That will cause an AV if you try to use it.</p> <p>3) <code>SaveDialog1.Execute()</code> may be returning False. That happens if you cancel the dialog, but it can also happen if the dialog has an internal error. In some situations, you can use <code>CommDlgExtendedError()</code> to check for that condition.</p> <p>4) <code>SaveDialog1.FileName</code> is empty, which should not happen if <code>SaveDialog1.Execute()</code> returns true, however it can happen if you are using a fairly modern Delphi version, running your app on Windows Vista or later, and select a non-filesystem file.</p> <p>During your debugging, make sure you are checking for all those conditions, eg:</p> <pre><code>var Item: TMenuItem; TempResultFile : TEdit; S: String; begin Item := Sender as TMenuItem; case Item.Tag of 1: TempResultFile := ResultTFile1; 2: TempResultFile := ResultTFile2; 3: TempResultFile := ResultTFile3; else raise Exception.CreateFmt('%s.Tag (%d) is not an expected value!', [Item.Name, Item.Tag]); end; if TempResultFile = nil then raise Exception.Create('TempResultFile is nil!'); if not SaveDialog1.Execute then raise Exception.CreateFmt('SaveDialog1.Execute returned false! Possible CommDlg error? (%d)', [CommDlgExtendedError()]); S := SaveDialog1.FileName; if S = '' then raise Exception.Create('SaveDialog1.FileName is empty!'); TempResultFile.Text := S; end; </code></pre> <p>As an alternative to using the <code>TMenuItem.Tag</code>, the <code>TPopupMenu.PopupComponent</code> property will tell you which Button displayed the PopupMenu. You can set the <code>TButton.Tag</code> property to point at the <code>TEdit</code> component that corresponds to that Button, then you don't have to use the <code>TMenuItem.Tag</code> property anymore to hunt for the <code>TEdit</code> component, eg:</p> <pre><code>procedure TForm1.FormCreate(Sender: TObject); begin ResultTButton1.Tag := NativeInt(ResultTFile1); ResultTButton2.Tag := NativeInt(ResultTFile2); ResultTButton3.Tag := NativeInt(ResultTFile3); end; procedure TForm1.MenuItemClick(Sender: TObject); var ResultTButton : TButton; TempResultFile : TEdit; begin ResultTButton := PopupMenu.PopupComponent as TButton; TempResultFile := TEdit(ResultTButton.Tag); if TempResultFile &lt;&gt; nil then begin if SaveDialog1.Execute then TempResultFile.Text := SaveDialog1.FileName; end; end; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload