刚学新手求教
学着做一个计算器,想增加一个记录计算结果的功能,于是,我就加了个按键,再加了个ListBox1,作为记录容器,然而,找到的例子里显示计算内容的是Lable1,而我的程序里用的是Edit1,他的语句是这样的:存入计算结果: if lab1.Caption <> stro then list1.AddItem(lab1.Caption,sender);
复制计算结果:
if lab1.Caption <>stro then begin
Clipboard.AsText:=lab1.Caption;
label1.Caption :='复制成功!';
end;
清空计算结果:
list1.Clear;
因为我的程序里显示计算内容用的是Edit1,请问下大大们,我应该怎么写,初学者,不当之处,请谅解,谢谢! lab1.Caption跟 edit.text 互换一下就好了。
if lab1.Caption <> stro then list1.AddItem(lab1.Caption,sender);
if edit1.text<> stro then list1.AddItem(edit1.text,sender);
if lab1.Caption <>stro then begin
Clipboard.AsText:=lab1.Caption;
label1.Caption :='复制成功!';
end;
if edit1.text<>stro then begin
Clipboard.AsText:=edit1.text ;
edit1.text:='复制成功!';
end; 本帖最后由 tanyztj 于 2017-10-12 21:52 编辑
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Button8: TButton;
Button9: TButton;
Button10: TButton;
Button11: TButton;
Button12: TButton;
Button13: TButton;
Button14: TButton;
Button15: TButton;
Button16: TButton;
Button17: TButton;
Button18: TButton;
Button19: TButton;
Button20: TButton;
Button21: TButton;
Button23: TButton;
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure Button10Click(Sender: TObject);
procedure Button11Click(Sender: TObject);
procedure Button12Click(Sender: TObject);
procedure Button13Click(Sender: TObject);
procedure Button14Click(Sender: TObject);
procedure Button15Click(Sender: TObject);
procedure Button16Click(Sender: TObject);
procedure Button19Click(Sender: TObject);
procedure Button18Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button17Click(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure Button20Click(Sender: TObject);
procedure Button21Click(Sender: TObject);
procedure Button23Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
var
data,data1,data2:Real;
OK:Integer;
StrO:string='0';
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
//edit1输入居中
SetWindowLong(Edit1.handle, GWL_STYLE, GetWindowlong(edit1.Handle, GWL_STYLE)+ES_CENTER);
Edit1.Refresh;
end;
procedure TForm1.Button5Click(Sender: TObject); //1
begin
if Length(Edit1.Text)> 40 then
MessageBeep(MB_OK)
else
begin
if OK =1 then
Edit1.Text:='';
Edit1.Text:=Edit1.Text +'1';
end;
OK:=0;
end;
procedure TForm1.Button6Click(Sender: TObject); //2
begin
if Length(Edit1.Text)> 40 then
MessageBeep(MB_OK)
else
begin
if OK =1 then
Edit1.Text:='';
Edit1.Text:=Edit1.Text +'2';
end;
OK:=0;
end;
procedure TForm1.Button7Click(Sender: TObject); //3
begin
if Length(Edit1.Text)> 40 then
MessageBeep(MB_OK)
else
begin
if OK =1 then
Edit1.Text:='';
Edit1.Text:=Edit1.Text +'3';
end;
OK:=0;
end;
procedure TForm1.Button8Click(Sender: TObject); //4
begin
if Length(Edit1.Text)> 40 then
MessageBeep(MB_OK)
else
begin
if OK =1 then
Edit1.Text:='';
Edit1.Text:=Edit1.Text +'4';
end;
OK:=0;
end;
procedure TForm1.Button9Click(Sender: TObject); //5
begin
if Length(Edit1.Text)> 40 then
MessageBeep(MB_OK)
else
begin
if OK =1 then
Edit1.Text:='';
Edit1.Text:=Edit1.Text +'5';
end;
OK:=0;
end;
procedure TForm1.Button10Click(Sender: TObject);//6
begin
if Length(Edit1.Text)> 40 then
MessageBeep(MB_OK)
else
begin
if OK =1 then
Edit1.Text:='';
Edit1.Text:=Edit1.Text +'6';
end;
OK:=0;
end;
procedure TForm1.Button11Click(Sender: TObject);//7
begin
if Length(Edit1.Text)> 40 then
MessageBeep(MB_OK)
else
begin
if OK =1 then
Edit1.Text:='';
Edit1.Text:=Edit1.Text +'7';
end;
OK:=0;
end;
procedure TForm1.Button12Click(Sender: TObject); //8
begin
if Length(Edit1.Text)> 40 then
MessageBeep(MB_OK)
else
begin
if OK =1 then
Edit1.Text:='';
Edit1.Text:=Edit1.Text +'8';
end;
OK:=0;
end;
procedure TForm1.Button13Click(Sender: TObject);//9
begin
if Length(Edit1.Text)> 40 then
MessageBeep(MB_OK)
else
begin
if OK =1 then
Edit1.Text:='';
Edit1.Text:=Edit1.Text +'9';
end;
OK:=0;
end;
procedure TForm1.Button14Click(Sender: TObject);//0
begin
ifEdit1.Text ='' then
MessageBeep(MB_OK)
else
begin
if Length(Edit1.Text)> 40 then
MessageBeep(MB_OK)
else
begin
if OK =1 then
Edit1.Text:=''
else
Edit1.Text:=Edit1.Text +'0';
end;
end;
OK:=0;
end;
procedure TForm1.Button15Click(Sender: TObject); //00
begin
ifEdit1.Text ='' then
MessageBeep(MB_OK)
else
begin
if Length(Edit1.Text)> 40 then
MessageBeep(MB_OK)
else
begin
if OK =1 then
Edit1.Text:=''
else
Edit1.Text:=Edit1.Text +'00';
end;
end;
OK:=0;
end;
procedure TForm1.Button16Click(Sender: TObject); //.
begin
ifEdit1.Text ='' then
Edit1.Text:= '0.'
else
begin
if Length(Edit1.Text)> 40 then
MessageBeep(MB_OK)
else
begin
if OK =1 then
begin
Edit1.Text:='';
Edit1.Text:= '0.';
end
else
Edit1.Text:=Edit1.Text +'.';
end;
end;
OK:=0;
end;
procedure TForm1.Button19Click(Sender: TObject);
begin
Edit1.Text:='';
OK:=0;
end;
procedure TForm1.Button18Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.Button1Click(Sender: TObject); //+
var
i:Integer;
begin
i:=Length(Edit1.Text);
if Edit1.Text='' then
MessageBeep(MB_OK)
else
begin
if (Edit1.Text='+')or (Edit1.Text='-')or
(Edit1.Text='*')or (Edit1.Text='*') then
MessageBeep(MB_OK)
else
Edit1.Text:=Edit1.Text+'+';
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i:Integer;
begin
i:=Length(Edit1.Text);
if Edit1.Text='' then
MessageBeep(MB_OK)
else
begin
if (Edit1.Text='+')or (Edit1.Text='-')or
(Edit1.Text='*')or (Edit1.Text='*') then
MessageBeep(MB_OK)
else
Edit1.Text:=Edit1.Text+'-';
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
i:Integer;
begin
i:=Length(Edit1.Text);
if Edit1.Text='' then
MessageBeep(MB_OK)
else
begin
if (Edit1.Text='+')or (Edit1.Text='-')or
(Edit1.Text='*')or (Edit1.Text='*') then
MessageBeep(MB_OK)
else
Edit1.Text:=Edit1.Text+'*';
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
var
i:Integer;
begin
i:=Length(Edit1.Text);
if Edit1.Text='' then
MessageBeep(MB_OK)
else
begin
if (Edit1.Text='+')or (Edit1.Text='-')or
(Edit1.Text='*')or (Edit1.Text='*') then
MessageBeep(MB_OK)
else
Edit1.Text:=Edit1.Text+'/';
end;
end;
procedure TForm1.Button17Click(Sender: TObject);
var
i:string;
mun:array of Real;
mun1:array of string;
L,j,a,k:Integer;
begin
a:=1;
j:=1;
OK:=1;
k:=1;
L:=1;
fora:=1 to Length(Edit1.Text) do
begin
if (Edit1.Text='+') or (Edit1.Text='-') or
(Edit1.Text='*') or (Edit1.Text='/') then
begin
mun:= StrToFloat(i);
mun1:= Edit1.Text;
j:=1+j;
L:=1+L;
i:='';
end
else
i:=i+ Edit1.Text;
end;
mun:= StrToFloat(i);
data:= mun;
for a:=2 to j do
begin
data1:= mun;
if mun1='+' then
data2:=data+data1
else if mun1='-' then
data2:=data-data1
else if mun1='*' then
data2:=data*data1
else if mun1='/' then
data2:=data/data1;
k:=k+1;
data:= data2;
end;
Edit1.Text:=Edit1.Text+'='+floattostr(data2);
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
If not(key in ['0'..'9',#8,#43,#45,#42,#47,#46,#187,#13]) then // #8 表示Delete键
Begin
Key:= #0; // #0 表示没有输入
MessageBeep(MB_OK);
End;
end;
procedure TForm1.Button20Click(Sender: TObject);
begin
Edit1.Text := Copy(Edit1.Text,0,Length(Edit1.Text)-1);
Edit1.SetFocus;
Edit1.SelStart := Length(Edit1.Text);
end;
procedure TForm1.Button21Click(Sender: TObject);
begin
if edit1.text<> stro then listBox1.AddItem(edit1.text,sender);
end;
procedure TForm1.Button23Click(Sender: TObject);
begin
listBox1.Clear;
end;
end.
解决了很多问题,现在有二个问题:
1、上面复制那个问题没解决。(我现在粘上的代码去掉了这部分)
2、我想实现按回车键就显示计算结果,怎么做?谢谢大大
3、还有个问题,比如显示的是这样的内容:2*6=12,我想在另一个Label中只取=号后面的结果,可以吗?怎么做?谢谢 我回答你的第二个问题按下回车键问题
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
If (Key =VK_RETURN)Then
Button1.Click ;
end;
你应该可以看得懂的 结合你的实例修改下 2*6=12
var
x,y:integer; str1,str2:String;
begin
str1:= '2*6=12';
x:=pos('=',str1);//查找位置
y:=Length(str1);//计算字符长度
str2:=Copy(str1,x+1,y-x);//复制=后面的字符赋值给str2
end;
页:
[1]