Selected Category: VB程式範例 (11)

View Mode: Post List Post Summary
今天做了 簡易型計算機程式

需要 textbox*3 button*4 label*1

程式碼如下

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
txtOutput.text = val (textbox1.text) +val (textbox2.text)
end sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
txtOutput.text = val (textbox1.text) -val (textbox2.text)
end sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
txtOutput.text = val (textbox1.text) *val (textbox2.text)
end sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
txtOutput.text = val (textbox1.text) /val (textbox2.text)
end sub

其他可自行更改 與修正

完成後 如下圖


Posted by ip612256374 at 痞客邦 PIXNET 留言(0) 引用(0) 人氣()

今天做了 印星星 的程式

需要 button*1 textbox*1

程式碼如下
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For i As Integer = 1 To 10
For j As Integer = 1 To i
txtOutput.Text &= "★"
Next
txtOutput.Text &= vbCrLf
Next
End Sub
End Class

一些可自行修改

完成後 如下圖


Posted by ip612256374 at 痞客邦 PIXNET 留言(0) 引用(0) 人氣()

今天做了 解一元二次方程式

需要 button*2 textbox*4 label*3

程式碼 如下



Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a, b, c, check As Integer
Dim ans1, ans2 As Double
a = Val(TextBox1.Text)
b = Val(TextBox2.Text)
c = Val(TextBox3.Text)
check = b * b - 4 * a * c
If check = 0 Then
ans1 = -b / (2 * a)
txtOutput.Text = "有兩個等根:" & ans1
ElseIf check < 0 Then
txtOutput.Text = "有兩個虛根"
Else
ans1 = (-b + Math.Sqrt(check)) / (2 * a)
ans2 = (-b - Math.Sqrt(check)) / (2 * a)
txtOutput.Text = "有兩個相異的實根:" & ans1 & "," & ans2
End If

End Sub

Posted by ip612256374 at 痞客邦 PIXNET 留言(1) 引用(0) 人氣()

上一次 做了 字體改變應用程式

這次 多了 功能表 與 字體大小

需要
menustrip*1 label*1

程式碼 如下

Public Class Form1

Private Sub 文字大小ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 文字大小ToolStripMenuItem.Click

End Sub

Private Sub mniType1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mniType1.Click
lblShow.Font = New Font("新細明體", lblShow.Font.Size, lblShow.Font.Style)
End Sub

Private Sub mniType2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mniType2.Click
lblShow.Font = New Font("標偕體", lblShow.Font.Size, lblShow.Font.Style)
End Sub

Private Sub mniSize1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mniSize1.Click
lblShow.Font = New Font(lblShow.Font.Name, 9, lblShow.Font.Style)
End Sub

Private Sub mniSize2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mniSize2.Click
lblShow.Font = New Font(lblShow.Font.Name, 12, lblShow.Font.Style)
End Sub

Posted by ip612256374 at 痞客邦 PIXNET 留言(0) 引用(0) 人氣()

今天做了 X~X之間3的倍數 實際來講
叫做 列出區間內所有三的倍數

需要
Button * 1 → TextBox * 3 → Label * 2

程式碼如下

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a, b As Integer
a = Val(TextBox1.Text)
b = Val(TextBox2.Text)
For i As Integer = a To b
If i Mod 3 <> 0 Then Continue For
txtOutput.Text &= i & " "
Next
End Sub
End Class

一些可自行修改

完成後 如下圖


Posted by ip612256374 at 痞客邦 PIXNET 留言(0) 引用(0) 人氣()

今天做了 文字編輯程式 (類似記事本)

需要 2個 Dialog物件 分別為 openfiledialog1及 savefiledialog1
一個textbox

程式碼如下

Public Class Form1

Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked

End Sub

Private Sub 檔案FToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 檔案FToolStripMenuItem.Click

End Sub

Private Sub 儲存檔案SToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 儲存檔案SToolStripMenuItem.Click
If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, _
TextBox1.Text, False)
End If
End Sub

Private Sub 開啟檔案OCtrloToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 開啟檔案OCtrloToolStripMenuItem.Click
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
TextBox1.Text = My.Computer.FileSystem.ReadAllText _
(OpenFileDialog1.FileName)
End If
End Sub

Posted by ip612256374 at 痞客邦 PIXNET 留言(1) 引用(0) 人氣()

今天做了 圖片預覽程式

需要 一個Button 一個 PictureBox 一個 OpenFileDialog

程式碼如下

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
picShow.ImageLocation = OpenFileDialog1.FileName
End If
End Sub
End Class

一些細微可自行調整

完成後 如下圖




放圖後


Posted by ip612256374 at 痞客邦 PIXNET 留言(0) 引用(0) 人氣()

今天做了 小時鐘

需要 1個 Label 1個 Timer

程式碼 如下
Public Class Form1

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = Now '在Timer1上點滑鼠左鍵兩下進入此程式區塊
End Sub
End Class

完成後 如下圖


Posted by ip612256374 at 痞客邦 PIXNET 留言(0) 引用(0) 人氣()

今天 做了 簡單的字體改變應用程式
需要
一個 TextBox 三個 GroupBox 其中兩個 GroupBox 內各有 兩個 RadioButton 另一個GroupBoxs
內有三個 RadioButton

這樣做

以下為程式碼

Public Class Form1

Private Sub rbtType1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtType1.CheckedChanged
txtShow.Font = New Font("新細明體", 20, txtShow.Font.Style)

End Sub

Private Sub rbtType2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtType2.CheckedChanged
txtShow.Font = New Font("標偕體", 20, txtShow.Font.Style)
End Sub

Private Sub rbtColorRed_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtColorRed.CheckedChanged
txtShow.ForeColor = Color.Red
End Sub

Private Sub rbtColorGreen_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtColorGreen.CheckedChanged
txtShow.ForeColor = Color.Green
End Sub

Private Sub rbtColorBlue_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtColorBlue.CheckedChanged
txtShow.ForeColor = Color.Blue

Posted by ip612256374 at 痞客邦 PIXNET 留言(0) 引用(0) 人氣()

今天用VB 做了一個 猜數字的小程式

需要 一個 按鈕 就夠了 其他可自行附加


以下為程式碼
Public Class 猜數字

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Guess As Integer
Guess = Val(InputBox("請輸入數字1-100"))
Do Until Guess = 66 '66為答案
If Guess > 66 Then
MsgBox("猜小一點") '猜太大了,提示使用者猜小一點
Guess = Val(InputBox("請輸入數字1-100"))
Else
MsgBox("猜大一點") '猜太小了,提示使用者猜大一點
Guess = Val(InputBox("請輸入數字1-100"))
End If
Loop
MsgBox("恭喜,猜中了")
MsgBox("浤珉說,很強歐")
End Sub
End Class

密碼預設為 66

Posted by ip612256374 at 痞客邦 PIXNET 留言(0) 引用(0) 人氣()

今天第一天 學 VB 看書做了簡單的程式
我是 用 VB 2005 Express
簡單的加減運算

需要 3個標籤 3個文字方塊 2個按鈕 分別作 加法與 減法的計算

以下為程式碼

Public Class Form1

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

End Sub

Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click

End Sub

Posted by ip612256374 at 痞客邦 PIXNET 留言(0) 引用(0) 人氣()