導航:首頁 > 編程語言 > vb計算器編程代碼

vb計算器編程代碼

發布時間:2023-09-03 01:38:16

❶ 用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——網路接

❷ vb編輯簡單計算器的代碼

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);
}
}
}
}

❸ 怎樣用vb編程一部簡單的計算器呢

1、打開VB新建一個EXE。

閱讀全文

與vb計算器編程代碼相關的資料

熱點內容
解壓文件電腦上哪裡找 瀏覽:447
linuxcutc 瀏覽:173
金穗蜀道通etc用什麼app辦理 瀏覽:123
阿雲伺服器系統盤與數據盤的區別 瀏覽:213
gcc編譯器可以用於單片機嗎 瀏覽:259
xmanagerlinux配置 瀏覽:664
文件夾視頻沒有聲音怎麼回事 瀏覽:83
閃閃app是什麼軟體 瀏覽:206
win7下引導linux 瀏覽:793
陝西bgp伺服器雲主機 瀏覽:934
ug編程有幾種加工方式 瀏覽:447
錘子手機如何添加桌面文件夾 瀏覽:465
公司早會拍照用哪個app好 瀏覽:424
學習打卡聲音解壓視頻 瀏覽:824
如何使用代理伺服器加速上網 瀏覽:266
找企業負責人電話用什麼app 瀏覽:427
linux創建文本文件命令 瀏覽:390
計算機中文檔加密保護操作步驟 瀏覽:387
地暖解壓管 瀏覽:465
貪心演算法dijkstra 瀏覽:38