Solved

Deactive OnClick event of Mouse

Posted on 1998-11-04
20
257 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 30 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 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