界面上有一个checkbox名叫cb1,我对其定义了单击操作cb1Click(sender:tobject)
在这个操作中对界面上的一系列控件作了数据显示的修改同时,我还有一个procedure,里面有这样一条语句:cb1.checked:=true;我想问的是这条语句会不会激发cb1Click事件?因为我的试验下来好像是这样的。如果这条语句真的能够激发cb1Click操作,那么请问如何才能只让cb1的checked为true而不要激发这个事件?谢谢
在这个操作中对界面上的一系列控件作了数据显示的修改同时,我还有一个procedure,里面有这样一条语句:cb1.checked:=true;我想问的是这条语句会不会激发cb1Click事件?因为我的试验下来好像是这样的。如果这条语句真的能够激发cb1Click操作,那么请问如何才能只让cb1的checked为true而不要激发这个事件?谢谢
解决方案 »
- 关于imageen控件中的TImageEnMView滚动条响应问题
- 关于TCP阻塞模式多线程同时处理与多个客户之间的数据传送?
- DLL的对象共享(在线等)
- 【紧急求救】操作文件替换字符串
- ██████【将Word文档上传到服务器后,用户可以在浏览器中打开(嵌入Iframe),怎么实现用户修改后,通过点击按钮保存到服务器上的Wo
- 马上要毕业了,做毕业设计,各位大虾帮帮忙,万份感激
- TcxDBPivotGrid 控件问题
- delphi控制word中的一个小问题
- 急请高手,如何关联两个memo中的内容??????????????
- 关于SQL SERVER 游标的问题
- 给我把ak 肯定把农行的那些人打死
- 如何获取OFFICE(word,execl)的句柄?
第二,我用Delphi 2007 Second Edition这个版本建了一个checkbox,建了一个edit,建了一个button
我点击button时,使得checkbox的属性checked发生变化,即,从true变到false,或是从false变到true.
同时,checkbox的onclick事件里定义了edit里显示的内容,即,如果checked为true,那么edit就显示'true',
如果checked为false,则edit就显示'false'第三,我的实验结果显示:当按下button时,edit里的内容发生了变化,在'true'和'false'之间变化着第四,如果按照我的实验结果来看,1楼的说法完全错误.第五,要说明的一点是,回答问题前最好先自己试验一下
这样会激发onclick事件,在onclick事件里判断那个全局变量是否为true;如果是的话,则exit;
还有一种是直接点击这个控件,当然前题是要有checkboxClick事件.可能还会有其他的方法,目前来讲,我只知道这两种.我的问题是这样的:我在直接点击checkbox的时候要发生一组反应,比如界面上数据的显示变化.而当我直接用第一种方法(checkbox.checked:=true)时,我要求没有什么变化,只要打个勾就行了.本以为这样的语句调用不会激发checkbox的onclick事件,但(无论是全局变量也好,局部变量也好,都试过了,详细看前面的回帖),但就是激发了,而这是我不想看到的结果.思考了一些时间后,我突然发现,这两种状态有一点区别,就是checkbox的焦点.如果是点击的话,则其焦点就应该为true,而用语句改变时,在我的程序中,checkbox的焦点不为true.这样就能区分两种情况了.最后我在checkbox的onclick事件中加入的焦点判断这一个条件,试验结果比较满意.
Since there is no OnCheckedChanged event, you will probably handle the program logic dependant on the checked state of the check box in its OnClick event.
However, if you programmatically change the Checked property the OnClick event will be fired - even though no user iteraction took place. There are (at least) two ways to programmatically change the checked property of the check box while "disabling" the OnClick event. Remove OnClick Handler, Change Checked, Put Back the Original OnClick handler
In Delphi for Win32 an event can have only one event handler (procedure) attached to it (even though there is a way to mimic multicast events in Delphi for Win32). The OnClick event's signature of a TCheckBox control is "type TNotifyEvent = procedure(Sender: TObject) of object;"
If you assign NIL to the OnClick event before you change the state of the check box, then revert to the original OnClick event handling procedure - the OnClick event will not be fired. procedure SetCheckedState(const checkBox : TCheckBox; const check : boolean) ;
var
onClickHandler : TNotifyEvent;
begin
with checkBox do
begin
onClickHandler := OnClick;
OnClick := nil;
Checked := check;
OnClick := onClickHandler;
end;
end;
Usage of this procedure is simple:
//toggle Checked state
begin
SetCheckedState(CheckBox1, NOT CheckBox1.Checked) ;
end;The SetCheckedState above toggles the Checked property of the CheckBox1 check box.
Protected Hack: ClicksDisabled := true
Another way to stop the OnClick from executing, when you programmatically change the Checked property of a Check box, is to take advantage of the "hidden" (protected) ClicksDisabled property.
By looking at the TCheckBox's SetState procedure which gets executed whenever the Checked property changes, the OnClick is fired if ClicksDisabled is not true. Since ClicksDisabled is protected you cannot access it from your code. Luckily, the protected hack technique enables you to access those hidden / protected properties of a Delphi control. The accessing protected members of a component provides more info on the subject. What you need to do is to declare a simple dummy class extending the TCheckBox in the same unit where you will use the ClicksDisabled property. Once you get your hands on the ClicksDisabled, simply set it to true, change the Checked property, then set ClicksDisabled back to false (default value): typeTCheckBoxEx = class(TCheckBox) ;...with TCheckBoxEx(CheckBox1) do
begin
ClicksDisabled := true;
Checked := NOT Checked;
ClicksDisabled := false;
end;Note: the above code toggles the Checked property of the check box named "CheckBox1" using the protected ClicksDisabled property.