急求用VB编辑简易计算器代码
1、创建控件组的方法首先创建一个命令按钮,调整其大小(觉得合适就行),名称为Command1,Caption 属性为数字 0 ;然后进行“复制”和“粘贴”,当选择“粘贴”时,出现对话框提示已有一个同名控件,询问是否创建控件组,选择“是”后,即创建了一个名为“Command”的控件组。
这时,第一个按钮的Index属性值默认为“0”,第二个的Index属性值自动设为“1”,并且大小与第一个按钮相同,只需修改其 Caption 属性为数字“1”并将其拖至合适位置即可。此后继续使用“粘贴”的方法建立其他控件组中其余按钮,共20个按钮,每建立一个,就将它拖到合适处,并修改相应的Caption属性值。
2、各控件组其属性设置如下:
设置效果如下图所示:
二、编写代码
Dim s1 As Single, s2 As Single, ysf As String
‘定义两个单精度数变量用与存放参与运算的数,一个字符型存放运算符
Private Sub Command1_Click(Index As Integer)
Text1.Text = Text1.Text & Command1(Index).Caption ’将command1的单击事件与文本框显示的内容连接
End Sub
Private Sub Command2_Click()
Text1.Text = Text1.Text + “。”
If (InStr(Text1.Text, “。”) = 1) Then ‘第一位不能为小数
Text1.Text = “”
End If
If InStr(Text1.Text, “。”) 《 Len(Text1.Text) Then ’防止出现两个小数点
Text1.Text = Left
(Text1.Text, Len(Text1.Text) - 1)
End If
End Sub
Private Sub
Command3_Click()
s2 = Val(Text1.Text) ‘开始加减乘除运算
Select Case ysf Case “+”
Text1.Text = s1 + s2
Case “-”
Text1.Text = s1 - s2
Case “*”
Text1.Text = s1 * s2
Case “/”
If s2 = 0 Then
MsgBox “分母不能为零!”
Text1.Text = “”
Else
Text1.Text = s1 / s2 End If End Select
Text1 = IIf(Left(Text1.Text, 1) = “。”, 0 & Text1.Text, Text1.Text) ‘
这个很关键,如果没有这个的话,得出小于1的小数前面没有0
End Sub
Private Sub Command4_Click()
If Text1.Text = “” Then ’文本为空就结束
Exit Sub
End If
Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) ‘文本退一格
End Sub
Private Sub Command5_Click()
Text1.Text = “” ’清除当前框内文本
End Sub
Private Sub Command6_Click(Index As Integer)
s1 = Val(Text1.Text) ‘将s1隐藏起来 ysf = Command6(Index).Caption
Text1.Text = “”
End Sub
Private Sub Command7_Click()
If Left(Text1.Text, 1) 《》 “-” Then ’判断作为负数
Text1.Text = “-” & Text1.Text
Else
Text1.Text = Right(Text1.Text, Len(Text1.Text) - 1)
End If
End Sub
Private Sub Command8_Click()
Text1.Text = Text1.Text * Text1.Text ‘平方
End Sub
拓展资料Visual Basic(VB)是由微软公司开发的包含环境的事件驱动编程语言。它源自于BASIC编程语言。VB拥有图形用户界面(GUI)和快速应用程序开发(RAD)系统,可以轻易的使用DAO、RDO、ADO连接数据库,或者轻松的创建ActiveX控件。程序员可以轻松地使用VB提供的组件快速创建一个应用程序。
参考链Visual Basic——百度百科接
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double x, y;
int i;
private void button6_Click(object sender, EventArgs e)
{
textBox1.Text += "5";
}
private void button13_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{ }
else
textBox1.Text += "0";
}
private void button9_Click(object sender, EventArgs e)
{
textBox1.Text += "1";
}
private void button10_Click(object sender, EventArgs e)
{
textBox1.Text += "2";
}
private void button11_Click(object sender, EventArgs e)
{
textBox1.Text += "3";
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text += "4";
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Text += "6";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += "7";
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text += "8";
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text += "9";
}
private void button4_Click(object sender, EventArgs e)
{
x = double.Parse(textBox1.Text);
textBox1.Text = "";
i++;
}
private void button8_Click(object sender, EventArgs e)
{
x = double.Parse(textBox1.Text);
textBox1.Text = "";
i += 2;
}
private void button12_Click(object sender, EventArgs e)
{
x = double.Parse(textBox1.Text);
textBox1.Text = "";
i += 3;
}
private void button16_Click(object sender, EventArgs e)
{
try
{
x = double.Parse(textBox1.Text);
textBox1.Text = "";
i += 4;
}
catch (System.Exception ex)
{ MessageBox.Show(ex.Message); }
}
private void button15_Click(object sender, EventArgs e)
{
try
{
y = double.Parse(textBox1.Text);
textBox1.Text = "";
}
catch (System.Exception ex)
{ MessageBox.Show(ex.Message); }
if (i == 1)
{
x = x + y;
textBox1.Text += x.ToString();
i--;
}
else if (i == 2)
{
x = x - y;
textBox1.Text += x.ToString();
i -= 2;
}
else if (i == 3)
{
x = x * y;
textBox1.Text += x.ToString();
i -= 3;
}
else if (i == 4)
{
try
{
x = x / y;
textBox1.Text += x.ToString();
i -= 4;
}
catch (System.Exception ex)
{ MessageBox.Show(ex.Message); }
}
}
private void button17_Click(object sender, EventArgs e)
{
x = y = 0;
i = 0;
textBox1.Text="";
}
private void button14_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{ }
else
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
}
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestZ extends JFrame implements ActionListener{
private JPanel jPanel1,jPanel2;
private JTextField resultField;
private JButton s1,s2,s3,s4,s5,s6,s7,s8,s9,s0,b1,b2,b3,b4,f1,f2;
private boolean end,add,sub,mul,div;
private String str;
private double num1,num2;
public TestZ(){
super("计算器");
setSize(300,240);
Container con=getContentPane();
con.setLayout(new BorderLayout());
jPanel1=new JPanel();
jPanel1.setLayout(new GridLayout(1,1));
jPanel2=new JPanel();
jPanel2.setLayout(new GridLayout(4,4));
resultField=new JTextField("0");
jPanel1.add(resultField);
con.add(jPanel1,BorderLayout.NORTH);
s1=new JButton(" 1 "); s1.addActionListener(this);
s2=new JButton(" 2 "); s2.addActionListener(this);
s3=new JButton(" 3 "); s3.addActionListener(this);
s4=new JButton(" 4 "); s4.addActionListener(this);
s5=new JButton(" 5 "); s5.addActionListener(this);
s6=new JButton(" 6 "); s6.addActionListener(this);
s7=new JButton(" 7 "); s7.addActionListener(this);
s8=new JButton(" 8 "); s8.addActionListener(this);
s9=new JButton(" 9 "); s9.addActionListener(this);
s0=new JButton(" 0 "); s0.addActionListener(this);
b1=new JButton(" + "); b1.addActionListener(this);
b2=new JButton(" - "); b2.addActionListener(this);
b3=new JButton(" * "); b3.addActionListener(this);
b4=new JButton(" / "); b4.addActionListener(this);
f1=new JButton(" . "); f1.addActionListener(this);
f2=new JButton(" = "); f2.addActionListener(this);
jPanel2.add(s1);
jPanel2.add(s2);
jPanel2.add(s3);
jPanel2.add(b1);
jPanel2.add(s4);
jPanel2.add(s5);
jPanel2.add(s6);
jPanel2.add(b2);
jPanel2.add(s7);
jPanel2.add(s8);
jPanel2.add(s9);
jPanel2.add(b3);
jPanel2.add(s0);
jPanel2.add(f1);
jPanel2.add(f2);
jPanel2.add(b4);
con.add(jPanel2,BorderLayout.CENTER);
}
public void num(int i){
String s = null;
s=String.valueOf(i);
if(end){
//如果数字输入结束,则将文本框置零,重新输入
resultField.setText("0");
end=false;
}
if((resultField.getText()).equals("0")){
//如果文本框的内容为零,则覆盖文本框的内容
resultField.setText(s);
}
else{
//如果文本框的内容不为零,则在内容后面添加数字
str = resultField.getText() + s;
resultField.setText(str);
}
}
public void actionPerformed(ActionEvent e){ //数字事件
if(e.getSource()==s1)
num(1);
else if(e.getSource()==s2)
num(2);
else if(e.getSource()==s3)
num(3);
else if(e.getSource()==s4)
num(4);
else if(e.getSource()==s5)
num(5);
else if(e.getSource()==s6)
num(6);
else if(e.getSource()==s7)
num(7);
else if(e.getSource()==s8)
num(8);
else if(e.getSource()==s9)
num(9);
else if(e.getSource()==s0)
num(0);
//符号事件
else if(e.getSource()==b1)
sign(1);
else if(e.getSource()==b2)
sign(2);
else if(e.getSource()==b3)
sign(3);
else if(e.getSource()==b4)
sign(4);
//等号
else if(e.getSource()==f1){
str=resultField.getText();
if(str.indexOf(".")<=1){
str+=".";
resultField.setText(str);
}
}
else if(e.getSource()==f2){
num2=Double.parseDouble(resultField.getText());
if(add){
num1=num1 + num2;}
else if(sub){
num1=num1 - num2;}
else if(mul){
num1=num1 * num2;}
else if(div){
num1=num1 / num2;}
resultField.setText(String.valueOf(num1));
end=true;
}
}
public void sign(int s){
if(s==1){
add=true;
sub=false;
mul=false;
div=false;
}
else if(s==2){
add=false;
sub=true;
mul=false;
div=false;
}
else if(s==3){
add=false;
sub=false;
mul=true;
div=false;
}
else if(s==4){
add=false;
sub=false;
mul=false;
div=true;
}
num1=Double.parseDouble(resultField.getText());
end=true;
}
public static void main(String[] args){
TestZ th1=new TestZ();
th1.show();
}
}
懒的写了,给你找了一个
Dim data
Dim a
Private Sub Command1_Click(index As Integer)
Text1.Text = Text1.Text + "1"
End Sub
Private Sub Command2_Click(index As Integer)
Text1.Text = Text1.Text + "2"
End Sub
Private Sub Command3_Click(index As Integer)
Text1.Text = Text1.Text + "3"
End Sub
Private Sub Command4_Click(index As Integer)
Text1.Text = Text1.Text + "4"
End Sub
Private Sub Command5_Click(index As Integer)
Text1.Text = Text1.Text + "5"
End Sub
Private Sub Command6_Click(index As Integer)
Text1.Text = Text1.Text + "6"
End Sub
Private Sub Command7_Click(index As Integer)
Text1.Text = Text1.Text + "7"
End Sub
Private Sub Command8_Click(index As Integer)
Text1.Text = Text1.Text + "8"
End Sub
Private Sub Command9_Click(index As Integer)
Text1.Text = Text1.Text + "9"
End Sub
Private Sub Command0_Click(index As Integer)
Text1.Text = Text1.Text + "0"
End Sub
Private Sub f_click()
If Val(Text1.Text) > 0 Then
Text1.Text = "-" + Text1.Text
ElseIf Text1.Text = "" Then
Text1.Text = "-"
Else: Text1.Text = -Val(Text1.Text)
End If
End Sub
Private Sub dot_click()
Text1.Text = Text1.Text + "."
End Sub
Private Sub clean_click()
data = 0
Text1.Text = ""
End Sub
Private Sub clean2_click()
Text1.Text = ""
End Sub
Private Sub add_click()
data = Val(Text1.Text)
a = 1
Text1.Text = ""
End Sub
Private Sub minus_click()
data = Val(Text1.Text)
a = 2
Text1.Text = ""
End Sub
Private Sub times_click()
data = Val(Text1.Text)
a = 3
Text1.Text = ""
End Sub
Private Sub divide_click()
data = Val(Text1.Text)
a = 4
Text1.Text = ""
End Sub
Private Sub pingfanggen_click()
If Val(Text1.Text) >= 0 Then
data = Val(Text1.Text)
Text1.Text = Sqr(data)
Else: Text1.Text = "Error!"
End If
End Sub
Private Sub mi_click()
data = Val(Text1.Text)
a = 5
Text1.Text = ""
End Sub
Private Sub equal_click()
If Text1.Text = "" Then
Text1.Text = data
Else
If a = 1 Then
Text1.Text = data + Val(Text1.Text)
End If
If a = 2 Then
Text1.Text = data - Val(Text1.Text)
End If
If a = 3 Then
Text1.Text = data * Val(Text1.Text)
End If
If a = 4 Then
If text1.text=0 then
Text1.text=”Error!”
else
Text1.Text = data / Val(Text1.Text)
End if
End If
If a = 5 Then
b = 1
For i = 1 To Val(Text1.Text)
b = b * data
Next i
Text1.Text = b
End If
End If
End Sub
Private Sub btnnums_Click(Index As Integer)
If Index = 10 Then
If InStr(lblxs.Caption, ".") Then
Exit Sub
Else
lblxs.Caption = lblxs.Caption + "."
Exit Sub
End If
End If
If lblxs.Caption <> "0" Then
lblxs.Caption = lblxs.Caption + btnnums(Index).Caption
Else
lblxs.Caption = btnnums(Index).Caption
End If
End Sub
Private Sub btnce_Click()
lblxs.Caption = "0"
End Sub
Private Sub btnfh_Click(Index As Integer)
num1 = Val(lblxs.Caption)
lblxs.Caption = "0"
num2 = Val(lblxs.Caption)
If btnfh(0) Then
fh = "+"
ElseIf btnfh(2) Then
fh = "-"
ElseIf btnfh(1) Then
fh = "*"
ElseIf btnfh(3) Then
fh = "/"
End If
End Sub
Private Sub Command3_Click()
num2 = Val(lblxs.Caption)
Select Case fh
Case "+"
num1 = num1 + num2
Case "-"
num1 = num1 - num2
Case "*"
num1 = num1 * num2
Case "/"
num1 = num1 / num2
End Select
lblxs.Caption = num1
End Sub
超级简单~!没有满意答案的话我再给你做吧
求个VB实现简单计数器的代码
新建一个VB工程,双击窗体把复制下面代码即可。Option Explicit Private sCount As Long Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)Select Case KeyCode Case 49 '加1 If Shift = 2 Then Form1.Caption = "CTRL + 1"sCount = sCount + 1 End If Case 50 '减1 If Sh...
用vb写一个最简单的计时器,可就是不成功,哪位大侠帮我看一下
Dim a As Integer a = 0 Dim b As Integer b = 0 这两句写在计时器的里面,每次计时器运行的时候都会调用,所以时间永远是 0:1 你试试下面的代码:Private Sub Command1_Click()Dim a As Integer a = 0 Dim b As Integer b = 0 Timer1.Enabled = True End Sub Private Sub Command2...
求精简VB公式
Next For i = 0 To 6 Me.lblhjysb.Caption = Me.lblhjysb.Caption + Val(Me.jfysb(i).Text)Next
vb制作一个简易的计时器,说明都用了什么控件和代(源)码
窗口里加一个label控件,一个timer控件。所有代码如下 Dim a, b Private Sub Form_Load()a = Now()End Sub Private Sub Timer1_Timer()Label1.Caption = Format(Now() - a, "Long Time")End Sub
求高人帮我写一段VB简单的倒计时代码!谢过
Dim i As Integer Private Sub Form_Load()Timer1.Enabled = True Timer1.Interval = 1000 i = 60 End Sub Private Sub Timer1_Timer()i = i - 1 Me.Caption = i End Sub
怎样用VB做一个倒计时工具
步骤\/方法打开 VB 软件,选择新建标准 EXE 工程。选择左侧工具箱里的 TextBox(文本框)控件,然后在窗口上画三个文本框,分别作为时、分、秒。然后再选择左侧的 CommandButton(按钮)工具,在时间的下方画两个按钮,分别作为起止和复位使用。接着选择左侧的 Label(标签)控件,在三个文本框的上方各...
如何用VB制作一个计数器
Long Private Sub Form_Load()MCount = 0 End Sub Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)If Button = 1 Then MCount = MCount + 1 ElseIf Button = 2 Then MCount = MCount - 1 End If Label1.Caption = MCount End Sub ...
帮我做一个VB实用小软件,要好看...也可以做一个小学生用的小软件_百...
用VB做倒计时,预设(可以修改)距离考试结束还有两个小时,有时分秒,有语音提示。很容易!建3个TEXT(分别显示时、分、秒),1个定时器(设1秒),2个按钮(开始倒计时和退出)。TEXT里分别写入预设的时间。如果是2小时,就分别写2、00、00。定时器响应里:秒-1,如果为负,置59,同时分钟位-1...
跪求vb多功能计时器代码 要有时钟 秒表 和倒计时的功能,秒表有暂停...
Dim SS As Boolean Dim TT As Single Private Sub Command1_Click() '开始 TT = 0 PP = False SS = False Command1.Enabled = False Command2.Enabled = True Command3.Enabled = True Command2.Caption = "暂停"tmr End Sub Private Sub tmr() '秒表 Dim t As Single t = Timer Do ...
如何用VB做一个计数器
text1 在通用中定义s,假设初值为23 鼠标左键为1,右键为2 Dim s As Integer Private Sub Command1_MouseDown(Button As Integer,Shift As Integer,X As Single,Y As Single)If Button = 2 Then s = s - 1 If Button = 1 Then s = s + 1 Text1.Text = s End Sub Private Sub Form...
谯兰茶苯: 这是刚看VB的时候写的一个简易计算器,可以看看Dim Num1, Num2 As SingleDim StrNum1, StrNum2 As StringDim FirstNum As Boolean '判断是否是数字开头Dim PointFlag As Boolean '判断是否已有小数点Dim Runsign As Integer '储存运算符...
平房区15250798257: 求VB简易计算器代码 - ?
谯兰茶苯: 建立14个按钮,按钮的名字都要Command1,设置Index属性,0~9分别对应数字0~9,10为小数点,11为等号,12~14分别对应+、-、*和/. 2、输入如下代码: Dim Num1, Num2 As Single Dim StrNum1, StrNum2 As String Dim FirstNum As ...
平房区15250798257: 用VB设计个简单计算器,个按钮的代码怎么编写啊?急 - ?
谯兰茶苯: Dim v As Boolean '是否第1次按运算符 Dim s As String '存放上次按的运算符 Dim x As Double '存放第1个操作数 Dim y As Double '存放第2个操作数 Private Sub Command2_Click() 'ok at 11-10-23 +Call JiSuan("+") End Sub Private Sub ...
平房区15250798257: 急求用VB编辑简易计算器代码 - ?
谯兰茶苯: 懒的写了,给你找了一个 Dim data Dim a Private Sub Command1_Click(index As Integer) Text1.Text = Text1.Text + "1" End Sub Private Sub Command2_Click(index As Integer) Text1.Text = Text1.Text + "2" End Sub Private Sub Command3...
平房区15250798257: 怎样用VB设计简单计算器 - ?
谯兰茶苯: Dim x As String Private Sub Command1_Click() Text1.Text = Text1.Text & "1" End Sub Private Sub Command10_Click() Text1.Text = Text1.Text & "6" End Sub Private Sub Command11_Click() Text1.Text = Text1.Text & "9" End Sub ...
平房区15250798257: 用VB程序设计一个简单计算器 - ?
谯兰茶苯: 界面:text11 2 3 +4 5 6 -7 8 9 *0 = AC / 代码:dim newnum as boolean dim n1,n2 op as integer private sub ac_click() text1.text="" end sub private sub command1_click(index as integer) if newnum=true then text1="" text1=text1&index ...
平房区15250798257: VB6.0中编辑一个简单的计算器的代码 - ?
谯兰茶苯: 在加法的按钮框输入 text3.text=val(text1.text)+val(text2.text) 在清除的按框输入 text3.text="" text2.text="" text1.text+=""
平房区15250798257: 求VB代码:设计一个简单计算器. - ?
谯兰茶苯: 我写的 一个简单的代码, Public h As Integer Public a As Single Public k As Integer Private Sub Command1_Click(Index As Integer) x = Index Select Case x Case 0 If h = 1 Then Text1.Text = "" h = 0 Text1.Text = Text1.Text + "0" Case 1 If h = ...
平房区15250798257: 用VB制作一个简单的计算器…求他的代码…最好是全部的代码…也可以是开头和结尾 - ?
谯兰茶苯: Option Explicit Dim a, b, c As Single, j As Integer, k As Boolean Private Sub cls_Click() xs.Text = "" dot.Enabled = True a = 0 End Sub Private Sub dot_Click() If k Then k = False: xs.Text = "0" xs.Text = xs.Text + "." dot.Enabled = False End ...
平房区15250798257: vb编辑简单计算器的代码 - ?
谯兰茶苯: Text1 计算数字 Text2 计算数字2 Label1 符号 + - * ÷ Text3 答案 代码----------------------------------------------------------------------------------------------------------------- If Label1.Caption = "-" Then Text3.Text = Val(Text1.Text) - Text2.Text If Label1.Caption = "+"...