Recent

Author Topic: [SOLVED] Prevent PopUp from showing  (Read 807 times)

CM630

  • Hero Member
  • *****
  • Posts: 1191
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
[SOLVED] Prevent PopUp from showing
« on: July 16, 2024, 08:02:09 am »
I want to prevent a popup from showing on some conditions.
For example, if a text box is empty.
The code below does not work - “No text” is shown when the test box is empty, but the popup is still shown.
I think I have to set the Popup event to Handled, but I cannot find a way to do it.



Code: Pascal  [Select][+][-]
  1. procedure TForm1.PopupMenu1Popup(Sender: TObject);
  2. begin
  3.   if edit1.Text ='' then
  4.   begin
  5.     ShowMessage ('No text');
  6.     PopupMenu1.Close;
  7.   end;
  8. end;  
« Last Edit: July 16, 2024, 12:34:35 pm by CM630 »
Лазар 4,0RC1 32 bit (sometimes 64 bit); FPC3,2,2

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: Prevent PopUp from showing
« Reply #1 on: July 16, 2024, 08:12:41 am »
Just set the controls PopupMenu property to nil to not show
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

dseligo

  • Hero Member
  • *****
  • Posts: 1406
Re: Prevent PopUp from showing
« Reply #2 on: July 16, 2024, 08:17:12 am »
I want to prevent a popup from showing on some conditions.
For example, if a text box is empty.
The code below does not work - “No text” is shown when the test box is empty, but the popup is still shown.
I think I have to set the Popup event to Handled, but I cannot find a way to do it.

What is test box? You mean edit1? You are saying that ShowMessage inside 'if' is executed although edit1.text is empty?
P.S.: Or 'ShowMessage' is not executed, but popup is shown? But you didn't write it like that.

Try this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.PopupMenu1Popup(Sender: TObject);
  2. begin
  3.   if edit1.Text ='' then
  4.   begin
  5.     ShowMessage ('No text, length: ' + IntToStr(Length(edit1.Text)));
  6.     PopupMenu1.Close;
  7.   end;
  8. end;  
« Last Edit: July 16, 2024, 08:19:48 am by dseligo »

CM630

  • Hero Member
  • *****
  • Posts: 1191
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Prevent PopUp from showing
« Reply #3 on: July 16, 2024, 09:05:41 am »

Just set the controls PopupMenu property to nil to not show

For the given example that sounds easy, but in my case that means too many preliminary checks.


What is test box? You mean edit1?

Yes. https://en.wikipedia.org/wiki/Text_box


You are saying that ShowMessage inside 'if' is executed although edit1.text is empty?
P.S.: Or 'ShowMessage' is not executed, but popup is shown? But you didn't write it like that.

I want to prevent the popup menu from showing in some circumstances.

I have attached a snippet in the first post.
Лазар 4,0RC1 32 bit (sometimes 64 bit); FPC3,2,2

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: Prevent PopUp from showing
« Reply #4 on: July 16, 2024, 09:10:15 am »

Just set the controls PopupMenu property to nil to not show

For the given example that sounds easy, but in my case that means too many preliminary checks.


then you should have posted your actual code, answers can only be given on what you have posted !!
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

dsiders

  • Hero Member
  • *****
  • Posts: 1280
Re: Prevent PopUp from showing
« Reply #5 on: July 16, 2024, 09:45:31 am »
I want to prevent a popup from showing on some conditions.
For example, if a text box is empty.
The code below does not work - “No text” is shown when the test box is empty, but the popup is still shown.
I think I have to set the Popup event to Handled, but I cannot find a way to do it.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.PopupMenu1Popup(Sender: TObject);
  2. begin
  3.   if edit1.Text ='' then
  4.   begin
  5.     ShowMessage ('No text');
  6.     PopupMenu1.Close;
  7.   end;
  8. end;  

Controls derived from TControl have an OnContextPopup event handler. In it, you can use whatever logic you need to enabled or disable the PopupMenu. Including:

Code: [Select]
procedure TForm1.Edit1ContextPopup(Sender: TObject; MousePos: TPoint; var Handled: Boolean);
begin
  Handled := (Edit1.Text = '');
end;

Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

CM630

  • Hero Member
  • *****
  • Posts: 1191
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Prevent PopUp from showing
« Reply #6 on: July 16, 2024, 12:34:24 pm »
...
Code: [Select]
procedure TForm1.Edit1ContextPopup(Sender: TObject; MousePos: TPoint; var Handled: Boolean);
begin
  Handled := (Edit1.Text = '');
end;


Thanks, it did it!

...then you should have posted your actual code, answers can only be given on what you have posted !!
Here it is, but I am not sure it would be helpful: https://sourceforge.net/p/lazmer/code/HEAD/tree/trunk/controls/wgcursorlegend.pas
Лазар 4,0RC1 32 bit (sometimes 64 bit); FPC3,2,2

petevick

  • Sr. Member
  • ****
  • Posts: 419
Re: Prevent PopUp from showing
« Reply #7 on: July 16, 2024, 12:47:43 pm »
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

 

TinyPortal © 2005-2018