Deactive OnClick event of Mouse

I have buttons in form and each button has its own function.
When I click a button to running a process, I want OnClick event of mouse deactive until the process completed. As long as the process is running, we can't click a button to running the other process.
How to make it ?
(I don't want using Enabled property from each button !!!).

Regards,
Joely
joelyAsked:
Who is Participating?
 
scrapdogConnect With a Mentor Commented:
To disable the OnClick event for an object, sets its OnClick event property to nil.

Button1.OnClick := nil;

To reenable OnClick, set it back to the event handler.

Button1.OnClick := Button1Click;
0
 
viktornetCommented:
:) Wuz up Kevin? ;-)
0
 
scrapdogCommented:
Everything is meaty.
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
scrapdogCommented:
Vik looks like you are almost ready for a t-shirt!!
0
 
viktornetCommented:
Ya still alive??? . o O

Yup, I'm waiting to get 30000... I don't really answer questions anymore... Sometimes go and see how are things going, but never really take the opportunity to answer .,I'm studying C/C++ now B-).............................................    How about ya??? Did ya get one?? I mean a t-shirt..

Cheers,
Viktor
0
 
scrapdogCommented:
Nope, not yet.
0
 
viktornetCommented:
Well, I gotta go to bed now... Tomorrow is my last day of school fo this week.. There is some kind of Teacher's Institude day or something.. Anyway, I'll get a free day and then the weekend.. Well, I like that.. talk to ya tomorrow... Bye...

Cheers,
Viktor
0
 
jerrypslCommented:
It'll be better if you disable the button to let user know that some processing has taken place instead of deactivating the OnClick event. But if you want to do it that way, you can use the method proposed by scrapdog.
0
 
joelyAuthor Commented:
How if in my form I have 15 buttons ?
Should I initialize one by one ?
Based on your proposed answer, could you given me other way to make it easier.

0
 
scrapdogCommented:
Joely:  setting the event handler to nil is the easiest way to do it.  A possible way to avoid having to do it for every button is to write ONE function to handle all of them.  You would just disable all of them, because chances are the user is not going to press the button again until the event is finished.  If you NEED to keep the current button disabled you could pass a parameter representing the one you don't want to diasble.

procedure DisableButtons;
begin
  Button1.OnClick := nil;
  Button2.OnClick := nil;
  ...
end;

procedure EnableButtons;
begin
  Button1.OnClick := Button1Click;
  Button2.OnClick := Button2Click;
  ...
end;


and your event handlers would look something like this:

procedure TForm1.Button1Click;
begin
  DisableButtons;
  {your event handler statements here}
  EnableButtons;
end;

procedure TForm1.Button2Click;
begin
  DisableButtons;
  {your event handler statements here}
  EnableButtons;
end;

and so on.
0
 
viktornetCommented:
You could do this one.....

const
  MAX_BUT = 14;

var
  Buttons : array[0..MAX_BUT] of TButton;

{...}
OnCreate();
var
  Count : integer;
begin
  count := 0;
  for i := 0 to ComponentCount - 1 do begin
    if count > MAX_BUT then break;
    if components[i] is TButton then begin
      Buttons[count] := Components[i];
      Inc(count);
    end;
  end;
end;

procedure Disable;
begin
  for i := 0 to MAX_BUT do
    TButton(Buttons[i]).OnClick := nil;
end;

procedure Enable;
begin
  for i := 0 to MAX_BUT do
    TButton(Buttons[i]).OnClick := MyButtonClick;
end;

Hope this helps....

Cheers,
Viktor
0
 
scrapdogCommented:
Viktor's method is also based on setting the event handler to nil, and it would work also!!
0
 
viktornetCommented:
That's your method Kevin, but I just changed it so you use less code to set the buttons... It assumes that your form got only those 14 buttons, or at least they were the first one to put on the form...

Cheers,
Viktor
0
 
scrapdogCommented:
Actually, Vik, now that I looked at your code closely, you are resetting OnClick to MyButtonClick for all buttons.  This means that all buttons would have the same event handler.  Your would probably have to have an array of event handler addresses (which you would assign in OnCreate like you did with the buttons.)

Like this:

TButton(Buttons[i]).OnClick := TNotifyEvent(ButtonClick[i]);

(or something like that).

0
 
viktornetCommented:
Yeah you might do that... Here is the full code,...

const
       MAX_BUT = 14;
     var
       Buttons : array[0..MAX_BUT] of TButton;
       OnClicks : array[0..MAX_BUT] of TNotifyEvent;
     {...}
     OnCreate();
     var
       Count : integer;
     begin
       count := 0;
       for i := 0 to ComponentCount - 1 do begin
         if count > MAX_BUT then break;
         if components[i] is TButton then begin
           Buttons[count] := Components[i];
           Inc(count);
         end;
       end;
     end;

     procedure Disable;
     begin
       for i := 0 to MAX_BUT do begin
         OnClicks[i] := TButton(Buttons[i]).OnClick;
         TButton(Buttons[i]).OnClick := nil;
       end;
     end;

     procedure Enable;
     begin
       for i := 0 to MAX_BUT do
         TButton(Buttons[i]).OnClick := TNotifyEvent(OnClicks[i]);
     end;

Hope this helps y'all

C'Yall

Cheers,
Viktor
0
 
scrapdogCommented:
Ahhh, that's better.  

(btw, you probably won't need the TNotifyEvent typecast after all, because you declared the array as an array of TNotifyEvent.)

:)
0
 
viktornetCommented:
I know, but better be sure than sorry ;)

Anyway, you can remove it if it makes you sick ;-)

Cheers,
Viktor
0
 
viktornetCommented:
As a matter of fact, I've declared the arry of buttons also to be TButton so if that makes you seek you can also remove the type cast to TButton()

Cheers,
Viktor
0
 
scrapdogCommented:
Vik:  I entered your code and made me so sick I threw up. :)

0
 
viktornetCommented:
:)
0
All Courses

From novice to tech pro — start learning today.