计算器?
用一个纯代码写了一个很简单的计算器 各位大牛帮看看我是边做边看书 要用什么控件就去了解他 所以没怎么有全局观
看看我做这个能否改进下 我总觉得ComboBox那个地方可以改进 但又不知道咋弄更好
程序代码
程序代码:
CLEAR ALL CLOSE DATABASES all frmMain = CREATEOBJECT("C_Form") frmMain.show READ events CLEAR RETURN DEFINE CLASS C_Form as Form caption = "简单计算器" width = 600 height = 400 autocenter = .T. ADD OBJECT Label1 as label WITH caption = "第一个数", Width = 60, height = 25 ADD OBJECT Label2 as label WITH caption = "第二个数", Width = 60, height = 25 ADD OBJECT Label3 as label WITH caption = "最终结果", Width = 60, height = 25 ADD OBJECT Text1 as TextBox WITH value = "", Width = 100, height = 25 ADD OBJECT Text2 as TextBox WITH value = "", Width = 100, height = 25 ADD OBJECT Text3 as TextBox WITH value = "", Width = 100, height = 25 ADD OBJECT Button1 as CommandButton WITH caption = "计算", height = 25 ADD OBJECT ComboBox1 as ComboBox WITH caption = "计算方式", Height = 20, Width = 50 PROCEDURE Arrange WITH ThisForm.Label1 .Top = 100 .Left = 50 ENDWITH WITH ThisForm.Text1 .Top = ThisForm.Label1.Top .Left = ThisForm.Label1.Left + ThisForm.Label1.Width + 5 ENDWITH WITH ThisForm.Label2 .Top = ThisForm.Label1.Top .Left = ThisForm.Width / 2 ENDWITH WITH ThisForm.Text2 .Top = ThisForm.Label1.Top .Left = ThisForm.Label2.Left + ThisForm.Label2.Width + 5 ENDWITH WITH ThisForm.Label3 .Top = ThisForm.Label1.Top + ThisForm.Label1.Height + 50 .Left = ThisForm.Width / 2 - .Width ENDWITH WITH ThisForm.Text3 .Top = ThisForm.Label3.Top .Left = ThisForm.Label3.Left + ThisForm.Label3.Width + 5 ENDWITH WITH ThisForm.Button1 .Top = ThisForm.Label3.Top + ThisForm.Label3.Height + 30 .Left = ThisForm.Width / 2 - .Width / 2 ENDWITH WITH Top = ThisForm.Label3.Top .Left = ThisForm.Label3.Left - .Width - 5 .style = 0 .inputmask = .displayvalue .list(1) = '+' .list(2) = '-' .list(3) = '*' .list(4) = '/' .ColumnCount = 1 .ColumnLines = .F. ENDWITH ENDPROC PROCEDURE Activate ThisForm.Arrange ENDPROC PROCEDURE Resize ThisForm.Arrange ENDPROC PROCEDURE destroy CLEAR EVENTS ENDPROC PROCEDURE ComboBox1.Click LOCAL i as Integer WITH FOR i = 1 TO 4 IF .selected(i) .DisplayValue = .list(i) ENDIF NEXT ENDWITH ENDPROC PROCEDURE Button1.CLick LOCAL v as Double WITH ThisForm DO CASE CASE = '+' v = VAL(.Text1.Value) + VAL(.Text2.Value) CASE = '-' v = VAL(.Text1.Value) - VAL(.Text2.Value) CASE = '*' v = VAL(.Text1.Value) * VAL(.Text2.Value) CASE = '/' IF VAL(.Text2.Value) != 0 v = VAL(.Text1.Value) / VAL(.Text2.Value) ELSE MESSAGEBOX("零做除数是非法的", 0) return ENDIF OTHERWISE MESSAGEBOX("错误的计算方法", 0) return ENDCASE ENDWITH WITH ThisForm .Text3.Value = STR(v,10, 2) ENDWITH ENDPROC ENDDEFINE
附上 连编的app
附图: