学习型 ASP/PHP/ASP.NET 主机 30元/年全能 ASP/PHP/ASP.NET 主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付
发新话题
打印

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

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

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);

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

谢谢

TOP

就是上面這麼寫,看看你沒顯示的復選框是不是caption為空了。
你微笑的面對整個世界,整個世界也將會微笑的面對你。

TOP

str_taste:='';   
这当然只能显示一个标题了。
我会拿出我全部的钱财,以保你衣食无忧。我会献出我所有的智慧,以助你一帆风顺。我会想到所有的笑语,以令你展眉开颜。我会挤出最长的时间,以使你终生幸福。        [本人原创的结婚宣言]

TOP

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

TOP

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

请赐教..

谢谢个为了..

TOP

分別選 中每一個控件,在事件屬性中的那個事件中選中在第一個控件中寫的那個事件就行了。
你微笑的面對整個世界,整個世界也將會微笑的面對你。

TOP

添加一个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 的字符串,否则“选中-取消-再选中”之后,就会有重复的,重复选中几次就会有几个重复项。
爱我至爱,至死不渝!

TOP

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

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

...

TOP

发新话题