Solved

Deactive OnClick event of Mouse

Posted on 1998-11-04
20
253 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now