Recent

Author Topic: Calling an OnChange event from an OnClick event of a Menu entry  (Read 964 times)

stephanos

  • New Member
  • *
  • Posts: 38
Calling an OnChange event from an OnClick event of a Menu entry
« on: November 15, 2022, 01:05:07 am »
Using: Lazarus V 2.2.2, FPC V 3.2.2, i386-win32-win32/win64

Dear All

I have a problem with a menu items on click event not calling a FileNameEdit’s OnChange event.

My programme has a number of BitBtns and one FileNameEdit object. 

For each BitBtn I made a Menu entry that calls each buttons OnCLick events.  These work perfectly well.

The object “FileNameEdit1” has an OnChange event.  When clicked it opens a file dialogue windows where I select files.  After selecting and then closing the file dialogue windows a lot of validation to the path\file takes place.  All is working as it should.

I made a Menu entry (called Append) to call the FileNameEdit objects OnChange event.  Append does not offer an OnChange event.  The three events for the Append menu are: OnClick, OnDrawItem, OnMeasaureItem.  So I used the OnClick event and entered “FileNameEdit1Change”.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FileNameEdit1Change(Sender: TObject);
  2. begin
  3. // lots of things being done
  4. end;  // end of procedure

The programme compiles.  It also runs.  When I click the Menu item “Append” nothing happens.  No crashing of the programme, nothing. The FileNameEdit1 button works as it should when it is clicked.

Any help appreciated.

dje

  • Full Member
  • ***
  • Posts: 134
Re: Calling an OnChange event from an OnClick event of a Menu entry
« Reply #1 on: November 15, 2022, 02:22:12 am »
Works fine here. (Lazarus 2.2.2)

Are you sure FileNameEdit1Change isn't being called? Add something like ShowMessage('HERE') to do a sanity check.
What did you want done via "Append"? There must be more to this than just calling TFileNameEdit.OnChange.

Just a guess, but if you want to popup the file dialog via a menu click, then look into:
Code: Pascal  [Select][+][-]
  1. FileNameEdit1.RunDialog;

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: Calling an OnChange event from an OnClick event of a Menu entry
« Reply #2 on: November 15, 2022, 08:13:37 am »
I don't even understand, what he's trying to achieve.
1) At a guess: In his Menu-OnClick he has something like "... BitBtn1Click;" --> Don't! Assign BitBtn-Click-Procedure directly to the Menu-Items OnClick in Object-Inspector
2) An Edit's OnChange is called/fired when the content of the Edit changes. Just Clicking into the Edit/setting Focus to it doesn't fire OnChange. His FileDialog is probably called in OnEnter or even outside (beforehand) of it
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

stephanos

  • New Member
  • *
  • Posts: 38
Re: Calling an OnChange event from an OnClick event of a Menu entry
« Reply #3 on: November 16, 2022, 12:34:45 am »
Dear All

Allow me to clarify and restate the problem.  I have ripped my audio CDs and made .mp3 files of them.  All the mp3 files are stored on the MP3 player.  This programme runs from the player when it is plugged into the computer.  The purpose of the programme is to write an .m3u playlist file, disconnect the player from the computer and run the playlist file so as to listen to the selected files while driving without having to work the MP3 player while driving.

The button I click is not a BitBtn.  It is a FileNameEdit1 from the Misc tab/ribbon.  It is clicked and I select a number of files, that are mp3s.  Once selected and the file dialogue is closed, the selected paths\filenames are passed to a string.  See the code.  This works.  It has no issues.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FileNameEdit1Change(Sender: TObject);
  2.    TheFileNames := FileNameEdit1.DialogFiles.Text; // Copy the list of files to string: TheFileNames
  3.    FileNameEdit1.DialogFiles.Text := '';


Then a lot of validating is done to that string to make sure the files names end in mp3 and the path\file names consist of 1 byte chars that the mp3 player will read.  If the path\file name contains invalid char the the entry in the Play List file will not play.  Validated entries are appended to a Play List (.m3u) file.  This works.

After making Menu entries that successfully call various BitBtn OnClick events, I made a menu entry to call the OnChange event.  The only thing that does not work is this Menu entry called "Append", whose OnClick event calls the FileEdit1Change event.  Please see attached PNG.  This is the problem.  The Menu entry does not work.  The FileNameEdit1Change button still works, but not via the menu entry.  The Menu Append event OnClick event does not call the FileNameEdit1Change event.

I have not understood most of the comments made so far.   Is someone saying that OnChange is the wrong event?

This advice:
Quote
Add something like ShowMessage('HERE') to do a sanity check
.   
look interesting but I do not know what to do.  Add the code where?

Hope that helps to understand.

dje

  • Full Member
  • ***
  • Posts: 134
Re: Calling an OnChange event from an OnClick event of a Menu entry
« Reply #4 on: November 16, 2022, 01:57:14 am »
Quote
The FileNameEdit1Change button still works, but not via the menu entry.  The Menu Append event OnClick event does not call the FileNameEdit1Change event.

RE: My advice "Add something like ShowMessage('HERE') to do a sanity check"

Place a ShowMessage('HERE') at the start of the FileNameEdit1Change method. Then try the TMenuItem click again.

I'm still unsure what you are trying to achieve via the TMenuitem "Append" (?) If you are trying to provide a menuitem option which is the same as clicking on the TFileNameEdit button, then you need to use FileNameEdit1.RunDialog.

ie: Add a new TMenuItem OnClick event and call RunDialog for the TFileNameEdit control.

You haven't indicated what workflow you want from the TMenuItem "Append", therefore I can only make guesses.

You have described a working method of adding filenames to a M3U list, but, not explained why you actually need an alternative method to access this feature.

Given that you clear the DialogFiles via "FileNameEdit1.DialogFiles.Text := ''", I'm unsure how you expect there to be any filenames to append. Calling OnChange doesn't popup the file select dialog, so, how do you expect filenames to be entered into FileNameEdit1?

Maybe have a rethink about your question.

stephanos

  • New Member
  • *
  • Posts: 38
Re: Calling an OnChange event from an OnClick event of a Menu entry
« Reply #5 on: November 16, 2022, 12:00:17 pm »
Thanks

Regrading
Quote
I'm still unsure what you are trying to achieve via the TMenuitem "Append" (?) If you are trying to provide a menu item option which is the same as clicking on the TFileNameEdit button, ......
Yes that is what I am trying to achieve.  I have achieved it for 6 of the buttons in the programme, all of which were OnCLick events.  The OnClick event of 6 Menu entries calling the OnClick event of 6 BitBtns. 

But FileNameEdit1Change is not an OnCLick event.

Quote
.... then you need to use FileNameEdit1.RunDialog.
OK.  But what is .RunDialog? It is not an event like OnClick.  Is it the entry to put in the OnClick event of the Menu entry called Label?

Regarding
Quote
You have described a working method of adding filenames to a M3U list, but, not explained why you actually need an alternative method to access this feature.
I want to be able to click my buttons via a Menu option which I have also enabled to use keystrokes, in case the mouse fails.  The objective  was not serious, but I wanted to learn the coding to expand my skill.  The keyboard strokes are working with the 6 BitBtns as well.

Regarding
Quote
Given that you clear the DialogFiles via "FileNameEdit1.DialogFiles.Text := ''", I'm unsure how you expect there to be any filenames to append.
Well let me explain
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FileNameEdit1Change(Sender: TObject);
  2.    TheFileNames := FileNameEdit1.DialogFiles.Text; // Copy the list of files to string: TheFileNames
  3.    FileNameEdit1.DialogFiles.Text := '';             // Empty .DialogFiles.Text so that the content is no longer used

Line 2 is the list of paths\filenames, selected by me and contained in the DialogFiles.Text, being copied into a string called TheFileNames.  And it works.  This programme works.

So it seems I am nearer a solution, but not one I understand.

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: Calling an OnChange event from an OnClick event of a Menu entry
« Reply #6 on: November 16, 2022, 12:31:47 pm »
*sigh* if people would just invest as much time in reading the damned documentation as posting questions where it takes a gazillion Q/A-Sessions to finally understand
https://lazarus-ccr.sourceforge.io/docs/lcl/editbtn/tfilenameedit.html

OP has a FileNameEdit
Quote
TFileNameEdit - an EditBox to hold a filename, with an attached SpeedButton that will summon a File Open dialog

So, at a guess: He clicks on the attached SpeedButton to call the FileDialog, he selects his Files, and then processes OnChange.
Now, he wants to access that functionality from a Menu.
Just reading the (DAMNED!!) doc's, i see a Property "OnButtonClick"
[SARCASM ON]
Now whatever could that be for?
[SARCASM OFF]

EDIT: And i just noticed: What does the OP's Problem has to do with "Using the Lazarus-IDE"?
@moderators, move to somewhere else?
« Last Edit: November 16, 2022, 12:40:59 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: Calling an OnChange event from an OnClick event of a Menu entry
« Reply #7 on: November 16, 2022, 01:01:36 pm »
I apologize (and OTOH i do not).
I just tried it with OnButtonClick, and it didn't work (that i apologize for).
dje provided an answer (use FileNameEdit1.RunDialog in MenuItem1Click), and that works (that's what i'm not apologizing for)
« Last Edit: November 16, 2022, 03:08:36 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

stephanos

  • New Member
  • *
  • Posts: 38
Re: Calling an OnChange event from an OnClick event of a Menu entry
« Reply #8 on: November 16, 2022, 04:32:03 pm »
OK, good and thanks

Meanwhile, what does

Quote
FileNameEdit1.RunDialog;
mean?  Where does it go?  Is is an Event , not listed in the Object Inspector?

I have not been idle.  "FileNameEdit1.RunDialog" when placed in the OnClick input of the Append menu gives an error of "FileNameEdit1.RunDialog is not a valid method name"

The Menu Append's OnClick's input drop down gives me a number of options including the one I thought was obvious, "FileNameEdit1Change".  But as mentioned before, it does not work.

I think we have established that the OnChange event for the FileNameEdit1 object (is that right is FileNameEdit1 an object?), is the correct even for the correct object.  Is that right?

So if anyone can clarify what I do with "FileNameEdit1.RunDialog", then I will have learnt something more

Wait to hear

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: Calling an OnChange event from an OnClick event of a Menu entry
« Reply #9 on: November 16, 2022, 05:31:45 pm »
Create an OnClick-Event-Handler for the Menu-item
Should look like this (writing on my iPad)
Code: Pascal  [Select][+][-]
  1. Procedure MyForm.mnuApeendClick(Sender:TObject);
  2. Begin
  3.     Filenameedit1.RunDialog;
  4. End;
  5.  
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

stephanos

  • New Member
  • *
  • Posts: 38
Re: Calling an OnChange event from an OnClick event of a Menu entry
« Reply #10 on: November 16, 2022, 07:47:40 pm »
Dear All

Solved via the last post

Code: Pascal  [Select][+][-]
  1. procedure TForm1.AppendClick(Sender: TObject);
  2. begin
  3.   FileNameEdit1.RunDialog;
  4. end;

Thanks to all

 

TinyPortal © 2005-2018