编程论坛's Archiver

adam1103 发表于 2007-11-27 21:50

如何显示多个复选框的内容

procedure TForm1.CheckBox1Click(Sender: TObject);
begin
  str_taste:='';
  If CheckBox1.Checked then
    If str_taste='' then
      str_taste:=str_taste + CheckBox1.Caption
    else
      str_taste:=str_taste + '.' +CheckBox1.Caption;
  If CheckBox2.Checked then
    If str_taste='' then
      str_taste:=str_taste + CheckBox2.Caption
    else
      str_taste:=str_taste +'.' + CheckBox2.Caption;
  If CheckBox3.Checked then
    If str_taste='' then
      str_taste:=str_taste + CheckBox3.Caption
    else
      str_taste:=str_taste + '.' + CheckBox3.Caption;
  If CheckBox4.Checked then
    If str_taste='' then
      str_taste:=str_taste + CheckBox4.Caption
    else
      str_taste:=str_taste + '.' + CheckBox4.Caption;
  If CheckBox5.Checked then
    If str_taste='' then
      str_taste:=str_taste + CheckBox5.Caption
    else
      str_taste:=str_taste + '.' + CheckBox5.Caption;
  If CheckBox6.Checked then
    If str_taste='' then
      str_taste:=str_taste + CheckBox6.Caption
    else
      str_taste:=str_taste + '.' + CheckBox6.Caption;
end;



ShowMessage('您的爱好是:' + str_taste+#13#10);

为什么只显示了一个复选框里的内容??
  如果要多个复选框该如何写代码..

谢谢

sky_yang_sky 发表于 2007-11-28 11:39

就是上面這麼寫,看看你沒顯示的復選框是不是caption為空了。

xu2000 发表于 2007-11-29 10:52

str_taste:='';    [em10]
这当然只能显示一个标题了。

sky_yang_sky 发表于 2007-11-29 15:38

str_taste:='';開始賦值為空,沒有問題,但是你要每一個控件中的Click事件中引用該過程才行,要不就會出現你上面的情況。

adam1103 发表于 2007-11-30 21:29

如何才能使每一個控件中的Click事件中引用該過程??

请赐教..

谢谢个为了..

sky_yang_sky 发表于 2007-12-1 15:10

分別選 中每一個控件,在事件屬性中的那個事件中選中在第一個控件中寫的那個事件就行了。

provoke 发表于 2007-12-3 12:58

添加一个Button控件,假设命名为Button1,则将上面的代码写入到Button1的点击事件中即可,就是将

procedure TForm1.CheckBox1Click(Sender: TObject);

改为

procedure TForm1.Button1Click(Sender: TObject);

你的错误在于将上面的代码放在CheckBox1控件的点击事件中,这样只有在点击CheckBox1时才会执行,换句话说,如果你先点击CheckBox1时其他CheckBox没有选中的话,就只是添加了CheckBox1的Caption,再点击其他CheckBox控件时,响应的是其他控件的Click事件,而不是CheckBox1的Click事件。

只有当最后选中的是CheckBox1时,才可以达到你的目的。

不过要考虑重复点击的情况,也就是取消已选中的CheckBox 时,要删除字符口串中对应Caption 的字符串,否则“选中-取消-再选中”之后,就会有重复的,重复选中几次就会有几个重复项。

adam1103 发表于 2007-12-3 21:32

太感谢了
谢谢你的说明..

想了好长时间都没有想通  原来是书上写错了

...

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.