请教一个基础问题,请指教。(300分)

阿虫

Unregistered / Unconfirmed
GUEST, unregistred user!
我的窗口上有几个CHECKBOX控件,每选中一个代表有一条命令,当选中其中的一个或几个后,
,点击按钮。我希望这几条命令可以依次发送到一个RICHEDIT中,然后将这些在RICHEDIT中的命令依次发送
到串口中。其它的都实现了,可是如何做个循环让选中CHECKBOX后将命令依次发到RICHEDIT
中总实现不了,请指教呀。
IF CHECKBOX1 CHECKED then

RICHEDIT。TEXT:=‘AAA’//‘AAA’是指我要发送的命令。
IF CHECKBOX2 CHECKED then

RICHEDIT。TEXT:=‘BBB’//‘BBB’是指我要发送的命令。
。。。。。。。。。。
请详写。
 
IF CHECKBOX1 CHECKED then

RICHEDIT。LINES.ADD(‘AAA’)//‘AAA’是指我要发送的命令。
IF CHECKBOX2 CHECKED then

RICHEDIT。LINES.ADD(‘BBB’)//‘BBB’是指我要发送的命令。
。。。。。。。。。。
 
同意楼上,只是语法。。。
IF CHECKBOX1.CHECKED then

RICHEDIT.LINES.ADD(‘AAA’)//‘AAA’是指我要发送的命令。
IF CHECKBOX2.CHECKED then

RICHEDIT.LINES.ADD(‘BBB’)//‘BBB’是指我要发送的命令。
。。。。。。。。。。
 
还有一种方法:
IF CHECKBOX1.CHECKED then

RICHEDIT.TEXT:=TRIM(RICHEDIT.TEXT)+‘AAA’;//‘AAA’是指我要发送的命令。
IF CHECKBOX2.CHECKED then

RICHEDIT.TEXT:=TRIM(RICHEDIT.TEXT)+‘BBB’;//‘BBB’是指我要发送的命令。
 
每次循环之前还要清空一下RECHEDIT,XXX。CLEAR;
 
各位老兄,我这个问题可是300分呀,如果是这个答案那不是太简单了吗。
同时你们以上的答案也不太好用呀,我的意思是要用一个 CheckBoxList,将所有的CHECKBOX
都放进去,然后在TIMER下写循环代码,当BUUTTON点中后,TIMER激活,开始依次发送命令,中间要
有廷时的。
 
假如窗體上有幾個 CheckBox1,CheckBox2........,timer等控件;
點中按鈕中寫:
Timer1.enabled:=true;
timer1的事件這樣寫:
procedure TForm1.Timer1Timer(Sender: TObject);
var i:integer;
box:TCheckBox;
begin
Timer1.Enabled:=False;
for i:=0 to 10do
begin
Box:=TCheckBox(self.FindComponent('CheckBox'+IntToStr(i)));
If Not Box.Checked then
ConTinue;
RICHEDIT1.LINES.ADD(Box.Caption);
// SendData();
你要發送數據的過程
Sleep(50);
end;
Timer1.Enabled:=True;
end;
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ComCtrls;
type
TForm1 = class(TForm)
CheckBox1: TCheckBox;
CheckBox2: TCheckBox;
CheckBox3: TCheckBox;
Timer1: TTimer;
Button1: TButton;
RichEdit1: TRichEdit;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
slCMD: TStringList;
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
slCMD := TStringList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
slCMD.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
slCMD.Clear;
if CheckBox1.Checked then
slCMD.Add('aaa');
if CheckBox2.Checked then
slCMD.Add('bbb');
if CheckBox3.Checked then
slCMD.Add('ccc');
//.......
Timer1.Enabled := true;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
if slCMD.Count > 0 then
begin
Richedit1.Lines.Add(slCMD[0]);
slCMD.Delete(0);
end
else
Timer1.Enabled := false;
end;

end.
 
procedure TForm1.Timer1Timer(Sender: TObject);
var i:integer;
begin
Timer1.Enabled:=False;
RICHEDIT1.Clear ;
for i:=0 to ComponentCount - 1do
begin
if Components is TCheckBox then
if TCheckbox(Components).Checked then
RICHEDIT1.LINES.ADD(TCheckbox(Components).Caption);
Sleep(50);
end;
Timer1.Enabled:=True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Timer1.Enabled := true ;
end;
 
假如窗體上有CheckListBox,Timer控件;
點中按鈕中寫:
Timer1.enabled:=true;
Timer1的事件這樣寫:
procedure TForm1.Timer1Timer(Sender: TObject);
var
i:integer;
begin
Timer1.Enabled:=False;
for i:=0 to CheckListBox.Items.Count-1do
begin
If CheckListBox.Checked then

begin
RichEdit1.Lines.ADD(CheckListBox.Items.Strings);
//SendData();
你要發送數據的過程
Sleep(50);
end;
end;
Timer1.Enabled:=True;
end;
 
上面几位朋友的程序我都试过了,都不太行,其中有的还有点错误,下面是我自已写的,不知
为什么实现不了循环发送,还请指教啊:
procedure TForm1.Button1Click(Sender: TObject);
var
s1:string;
i:integer;
begin
timer1.enabled:=true;
s1:=richedit1.text;
begin
if checkbox1.checked then
s1:='123456' else
if checkbox2.checked then
s1:='234567' else
if checkbox3.checked then
s1:='345678' else
s1:=' ';
begin
k100:=length(s1) div 2;
//确定循环次数。
FillMemory(@sbuf,255,0);
//将数组读入内存。
for i:=0 to k100 -1do
//????????????
sbuf:=strtoint('$'+copy(s1,2*i+1,2));
//将字符串赋于数组。
senddata;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
COMM1.STARTCOMM;
CheckBoxList := TStringList.Create;
CheckBoxList.AddObject('1',CheckBox1);
CheckBoxList.AddObject('2',CheckBox2);
CheckBoxList.AddObject('3',CheckBox3);
CheckBoxItem := 0;
end;

procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
COMM1.STOPCOMM;
CheckBoxList.Free;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
i:integer;
CheckBoxObj : TCheckBox;
begin
if timer1.enabled=true then
begin
for i := CheckBoxItem to CheckBoxList.Count-1do
begin
CheckBoxObj := TCheckBox(CheckBoxList.Objects);
end;

end;
end.
 
//组件编程,为你定做
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
CheckBox1: TCheckBox;
CheckBox2: TCheckBox;
RichEdit1: TRichEdit;
CheckBox3: TCheckBox;
CheckBox4: TCheckBox;
CheckBox5: TCheckBox;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Procedure MyClick(Sender: TObject);
end;

var
Form1: TForm1;
MyStringList: TStringList;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
MyStringList := TStringList.Create;
//将窗体中每个CheckBox的单击事件传给一个过程
for i := 0 to ComponentCount - 1do
begin
if Components is TCheckBox then
TCheckBox(Components).OnClick := MyClick;
end;
end;

procedure TForm1.MyClick(Sender: TObject);
begin
MyStringList.Clear;
RichEdit1.Clear;
if CheckBox1.Checked then
MyStringList.Add('AAA');
if CheckBox2.Checked then
MyStringList.Add('BBB');
//....
RichEdit1.Text := MyStringList.Text;
//增加自己发送串中的功能
ShowMessage('发串口值:' + RichEdit1.text);
end;

end.
 
谢谢大家了,春意的方法比较好些,但和我的想法不太一致,但可以实现,只不过要是用一个全选
按钮的话好象不太好用了。
 
顶部