DELPHI 多个edit计算
现有6个edit分别为1-6首先赋值给edit1.text一个值比如100要求无论edit2-6怎样改变edit1动态显示(100-edit1-6的值)而且要求edit1...
现有6个edit分别为1-6
首先赋值给edit1.text一个值 比如100
要求 无论edit2-6怎样改变 edit1动态显示 (100 - edit1-6的值) 而且要求 edit1不能小于0
我没说清楚 还有要求
edit2-6 的和不能大于 edit1的值
而且 edit2-6不能小于0
还有个问题就是当edit的值为空就会报错 但是加入检测后 比如检测为空 就赋值'0' 这样 删成空时老是显示0 再输入就变成 012这样了 展开
首先赋值给edit1.text一个值 比如100
要求 无论edit2-6怎样改变 edit1动态显示 (100 - edit1-6的值) 而且要求 edit1不能小于0
我没说清楚 还有要求
edit2-6 的和不能大于 edit1的值
而且 edit2-6不能小于0
还有个问题就是当edit的值为空就会报错 但是加入检测后 比如检测为空 就赋值'0' 这样 删成空时老是显示0 再输入就变成 012这样了 展开
展开全部
procedure EditKeyPress(Sender: TObject; var Key: Char);
procedure EditChange(Sender: TObject);
procedure EditKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
//初始化Edit
procedure TForm1.FormCreate(Sender: TObject);
var
i :integer;
begin
for i := 0 to ComponentCount - 1 do
if Components[i] is TEdit then
begin
(Components[i] as TEdit).Text := '0';
(Components[i] as TEdit).OnKeyPress := EditKeyPress;
(Components[i] as TEdit).OnChange := EditChange;
(Components[i] as TEdit).OnKeyUp := EditKeyUp;
end;
end;
//检测按下的键是不是0到9和退格键
procedure TForm1.EditKeyPress(Sender: TObject; var Key: Char);
begin
if not (Key in ['0'..'9',#8]) then
Key := #0;
end;
//这里是计算部分
procedure TForm1.EditChange(Sender: TObject);
var
i,n :integer;
begin
n := 0;
if (Sender as TEdit).Name <> 'Edit1' then
begin
for i := 0 to ComponentCount - 1 do
begin
if Components[i] is TEdit then
if ((Components[i] as TEdit).Text <> '') and ((Components[i] as TEdit).Name <> 'Edit1') then
n := n + strtoint((Components[i] as TEdit).Text)
else
(Components[i] as TEdit).Text := '0';
end;
edit1.Text := inttostr(Max - n);
end
end;
procedure TForm1.EditKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
i :integer;
begin
if pos('0',(Sender as TEdit).Text) = 1 then //检测Edit的第一位是不是0,是0就去掉
begin
(Sender as TEdit).Text := Copy((Sender as TEdit).Text,2,length((Sender as TEdit).Text)-1);
(Sender as TEdit).SelStart := Length((Sender as TEdit).Text);
end;
if (Sender as TEdit).Name = 'Edit1' then
begin
if Edit1.Text = '' then
Edit1.Text := '0';
Max := strtoint(Edit1.Text) //把Edit1的值赋给全局变量Max
end
else
begin
if strtoint((Sender as TEdit).Text) > Max then //检测输入的数是不是大于Edit1的数
begin
showmessage((Sender as TEdit).Text+'>'+inttostr(Max));
for i := 0 to self.ComponentCount - 1 do
if Components[i] is TEdit then
(Components[i] as TEdit).Text := '0';
end
else if Max < (strtoint(Edit2.Text)+strtoint(Edit3.Text)+strtoint(Edit4.Text)+strtoint(Edit5.Text)+strtoint(Edit6.Text)) then //检测Edit2到Edit6相加是否大于Edit1
begin
showmessage(inttostr(Max)+'<'+Edit2.Text+'+'+Edit3.Text+'+'+Edit4.Text+'+'+Edit5.Text+'+'+Edit6.Text);
for i := 0 to self.ComponentCount - 1 do
if Components[i] is TEdit then
(Components[i] as TEdit).Text := '0';
end;
end;
end;
应该没什么问题啦,不明白再问吧
procedure EditChange(Sender: TObject);
procedure EditKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
//初始化Edit
procedure TForm1.FormCreate(Sender: TObject);
var
i :integer;
begin
for i := 0 to ComponentCount - 1 do
if Components[i] is TEdit then
begin
(Components[i] as TEdit).Text := '0';
(Components[i] as TEdit).OnKeyPress := EditKeyPress;
(Components[i] as TEdit).OnChange := EditChange;
(Components[i] as TEdit).OnKeyUp := EditKeyUp;
end;
end;
//检测按下的键是不是0到9和退格键
procedure TForm1.EditKeyPress(Sender: TObject; var Key: Char);
begin
if not (Key in ['0'..'9',#8]) then
Key := #0;
end;
//这里是计算部分
procedure TForm1.EditChange(Sender: TObject);
var
i,n :integer;
begin
n := 0;
if (Sender as TEdit).Name <> 'Edit1' then
begin
for i := 0 to ComponentCount - 1 do
begin
if Components[i] is TEdit then
if ((Components[i] as TEdit).Text <> '') and ((Components[i] as TEdit).Name <> 'Edit1') then
n := n + strtoint((Components[i] as TEdit).Text)
else
(Components[i] as TEdit).Text := '0';
end;
edit1.Text := inttostr(Max - n);
end
end;
procedure TForm1.EditKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
i :integer;
begin
if pos('0',(Sender as TEdit).Text) = 1 then //检测Edit的第一位是不是0,是0就去掉
begin
(Sender as TEdit).Text := Copy((Sender as TEdit).Text,2,length((Sender as TEdit).Text)-1);
(Sender as TEdit).SelStart := Length((Sender as TEdit).Text);
end;
if (Sender as TEdit).Name = 'Edit1' then
begin
if Edit1.Text = '' then
Edit1.Text := '0';
Max := strtoint(Edit1.Text) //把Edit1的值赋给全局变量Max
end
else
begin
if strtoint((Sender as TEdit).Text) > Max then //检测输入的数是不是大于Edit1的数
begin
showmessage((Sender as TEdit).Text+'>'+inttostr(Max));
for i := 0 to self.ComponentCount - 1 do
if Components[i] is TEdit then
(Components[i] as TEdit).Text := '0';
end
else if Max < (strtoint(Edit2.Text)+strtoint(Edit3.Text)+strtoint(Edit4.Text)+strtoint(Edit5.Text)+strtoint(Edit6.Text)) then //检测Edit2到Edit6相加是否大于Edit1
begin
showmessage(inttostr(Max)+'<'+Edit2.Text+'+'+Edit3.Text+'+'+Edit4.Text+'+'+Edit5.Text+'+'+Edit6.Text);
for i := 0 to self.ComponentCount - 1 do
if Components[i] is TEdit then
(Components[i] as TEdit).Text := '0';
end;
end;
end;
应该没什么问题啦,不明白再问吧
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
procedure TForm1.FormCreate(Sender: TObject);
begin
Edit1.Text := '100';
end;
procedure TForm1.Edit2Change(Sender: TObject);
var
iResult: Integer;
begin
iResult := 100 - StrToInt(Edit2.Text) - StrToInt(Edit3.Text) -
StrToInt(Edit4.Text) - StrToInt(Edit5.Text) -
StrToInt(Edit6.Text);
if iResult > 0 then Edit1.Text := IntToStr(Result);
end;
//然后将Edit3-6的所有OnChange都选择Edit2Change
//注意程序中没有加转化类型的判断
begin
Edit1.Text := '100';
end;
procedure TForm1.Edit2Change(Sender: TObject);
var
iResult: Integer;
begin
iResult := 100 - StrToInt(Edit2.Text) - StrToInt(Edit3.Text) -
StrToInt(Edit4.Text) - StrToInt(Edit5.Text) -
StrToInt(Edit6.Text);
if iResult > 0 then Edit1.Text := IntToStr(Result);
end;
//然后将Edit3-6的所有OnChange都选择Edit2Change
//注意程序中没有加转化类型的判断
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询