Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In order to debug your code and work out what the problem is, change your code entirely – the comments explain how each works to help you solve the problem.</p> <pre><code>var TempResultFile : TEdit; begin if SaveDialog1.Execute then // Only try assignment if file chosen begin // Find the right TEdit // The default branch will be used if the wrong menu item is // connected to the event, or you forgot to set the tag value case (Sender as TMenuItem).Tag of 1: TempResultFile := ResultTFile1; 2: TempResultFile := ResultTFile2; 3: TempResultFile := ResultTFile3; else // If none of the above match raise Exception.CreateFmt('Invalid tag %d in %s', [TMenuItem(Sender).Tag, TMenuItem(Sender).Name])); end; // At this point, we know TempResultFile has been assigned a value TempFileResult.Text := SaveDialog1.FileName; end else // User clicked cancel on SaveDialog1. ShowMessage('SaveDialog1.Execute returned False`); end; </code></pre> <p>This does several things that will help you:</p> <ul> <li><p>If the <code>SaveDialog</code> doesn't appear, you know you didn't assign the OnClick handler for a menu item.</p></li> <li><p>If the user chooses <code>Cancel</code> on the <code>SaveDialog</code>, it returns false, and this will show you a dialog to indicate that fact (to eliminate that problem).</p></li> <li><p>If the user chooses a file, it checks to make sure that a valid <code>Tag</code> assignment was made for the <code>TMenuItem</code>, and raises an exception if you forgot (and gives you the invalid tag value and the name of the component that was clicked).</p></li> </ul> <p>In addition, it's safer code than what's in your original question, which can cause an access violation if the tag is not one of the values in your <code>case</code> statement and the <code>SaveDialog1.Execute</code> returns true. (There may be nothing assigned to <code>TempResultFile</code> when you access it's <code>Text</code> property.)</p> <p>Once you've figured out what you're doing wrong, you can remove the final </p> <pre><code>end else ShowMessage('SaveDialog1.Execute returned False`); </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