type
  TForm1 = class(TForm)
    Button1: TButton;
    CheckBox1: TCheckBox;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure MyonclickEvent1(Sender:TObject);
    procedure MyonclickEvent2(Sender:TObject);
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}
procedure TForm1.MyonclickEvent1(Sender:TObject);
begin
  showmessage(Button1.Name);
end;
procedure TForm1.MyonclickEvent2(Sender:TObject);
begin
  showmessage('hello ,boy');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
     Button1.OnClick:=nil;
  if CheckBox1.Checked = true then
    Button1.OnClick:=MyonclickEvent1;
  if CheckBox1.Checked = true then
    Button1.OnClick:=MyonclickEvent2;
end;end.
为什么我每次点击button1,不管checkbox是否选中,得到的值都是hello, boy

解决方案 »

  1.   

    if CheckBox1.Checked = true then
        Button1.OnClick:=MyonclickEvent1;
      if CheckBox1.Checked = false then
        Button1.OnClick:=MyonclickEvent2;
    -------------
    爱如火,紫如情!
    -------------
      

  2.   

    if CheckBox1.Checked = true then
        Button1.OnClick:=MyonclickEvent1;
      if CheckBox1.Checked = false then
        Button1.OnClick:=MyonclickEvent2;
    都为True时应该两个都显示,
      if中没有必要加Button1.OnClick:=它吧!直接调用过程就可以了!
      

  3.   

    if CheckBox1.Checked = true then
        Button1.OnClick:=MyonclickEvent1;
      if CheckBox1.Checked = false then
        Button1.OnClick:=MyonclickEvent2;
    我这样试了,但是只有单击第一次按钮时结果是正确的,第二次。以后单击按钮的结果就和第一次的结果单击按钮的结果相同了,不管是否选中checkbox
      

  4.   

    if CheckBox.checked = true then
    Button1.OnClick:=MyonclickEvent1
    else
    Button1.OnClick:=MyonclickEvent2;