Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Deactive OnClick event of Mouse

Posted on 1998-11-04
20
Medium Priority
?
262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

670 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