简体   繁体   English

如何从Delphi中的TGroupBox获取复选框值?

[英]How to get checkbox value from a TGroupBox in Delphi?

I have a groupbox and three check boxes in a form. 我在表单中有一个groupbox和三个复选框。 And I want to get the user selected values in an array of values and If he doesn't selects one then the default enabled checkbox value needs to be selected into an array. 我想让用户在值数组中选择值,如果他没有选择一个值,则需要将默认启用的复选框值选择到数组中。

var selectedValues:String;
selectedValues:= getSelectedCheckboxValue(); //Here I want to pass the  selected values to store it into an array
function getSelectedCheckboxValue(): string;
begin
 var retArr:array[1..3] of string;
 return retArr;
end;

Is my approach correct? 我的方法正确吗? Or Is there an other way to do it. 还是有其他方法可以做到这一点。

I belive I've guessed what you are asking about 我相信我猜到你在问什么

Try this function: 试试这个功能:

function GetSelectedCheckboxValue(CheckListBox: TCheckListBox): string;
var
  i: Integer;
begin
  Result := '';

  for i := 0 to CheckListBox.Count - 1 do
    if CheckListBox.Checked[i] then
      Result := Result + CheckListBox.Items[i] + ',';


  System.Delete(Result, Length(Result), 1);
end;

and call it like this: 并这样称呼它:

  Caption := GetSelectedCheckboxValue(CheckListBox1);

Or if you want the result in an Array : 或者,如果您希望将结果放入Array中:

function GetSelectedCheckboxValue_Take2(CheckListBox: TCheckListBox): TStringDynArray;
var
  i: Integer;
  ResultArray: TStringDynArray;
begin
  ResultArray := nil;
  for i := 0 to CheckListBox.Count - 1 do
    if CheckListBox.Checked[i] then
    begin
      SetLength(ResultArray, Length(ResultArray) + 1);
      ResultArray[Length(ResultArray) - 1] := CheckListBox.Items[i];
    end;

  Result := ResultArray;
end;

The last part 最后一部分

And If he doesn't selects one then the default enabled checkbox value needs to be selected into an array. 而且,如果他没有选择一个,则需要将默认启用的复选框值选择到一个数组中。

Makes no sense what so ever. 如此毫无意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM