Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Deactive OnClick event of Mouse

Posted on 1998-11-04
20
Medium Priority
?
264 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:joely
20 Comments
 
LVL 5

Accepted Solution

by:
scrapdog earned 90 total points
ID: 1345689
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1345690
:) Wuz up Kevin? ;-)
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1345691
Everything is meaty.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Expert Comment

by:scrapdog
ID: 1345692
Vik looks like you are almost ready for a t-shirt!!
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1345693
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 1345694
Nope, not yet.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1345695
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
 

Expert Comment

by:jerrypsl
ID: 1345696
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
 

Author Comment

by:joely
ID: 1345697
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 1345698
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1345699
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 1345700
Viktor's method is also based on setting the event handler to nil, and it would work also!!
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1345701
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 1345702
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1345703
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 1345704
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1345705
I know, but better be sure than sorry ;)

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

Cheers,
Viktor
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1345706
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
 
LVL 5

Expert Comment

by:scrapdog
ID: 1345707
Vik:  I entered your code and made me so sick I threw up. :)

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1345708
:)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question