VB.Net 入门

1.计算器

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As Integer
        Dim y As Integer
        x = Convert.ToUInt32(TextBox1.Text)
        y = Convert.ToUInt32(TextBox2.Text)
        Label1.Text = (x + y).ToString()            ‘执行x+y的加法运算
    End Sub                                                   ‘子过程不能通过过程名来返回变量值
End Class

 

2.条件语句(If…Then…Else If…Then…Else…End If)

If x >= 0 Then
            y = 2 * x
Else
            y = x

End If

 

3.循环语句

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        For i = 0 To 9
            TextBox1.Text += i.ToString()          ‘显示0123456789
        Next
End Sub

 

4.MsgBox制作对话框

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim t As Short
        t = MsgBox("确定?", MsgBoxStyle.Question + MsgBoxStyle.YesNoCancel, "MsgBox对话框")
        If t = MsgBoxResult.Yes Then               ‘按下“是”
            TextBox2.Text = "yeah"
        ElseIf t = MsgBoxResult.No Then          ‘按下“否”
            TextBox2.Text = "nope"
        End If
End Sub

image

5. Form跳转

Dim f2 As New Form2

f2.Show()

 

6.OpenFileDialog组件的使用

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.Filter = "Bitmaps(*.bmp)|*.bmp"   ‘打开的文件类型
        Me.Text = OpenFileDialog1.FileName                     ‘标题栏显示图片路径
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then                      ‘弹出对话框
            PictureBox1.Image = System.Drawing.Image.FromFile(OpenFileDialog1.FileName)     ‘按下“打开(O)”后装入图片
        End If
End Sub

 

7.SaveFileDialog组件的使用

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        SaveFileDialog1.Filter = "Bitmaps(*.bmp)|*.bmp"                                             ‘保存的文件类型
        If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then       ‘弹出对话框
            PictureBox1.Image.Save(SaveFileDialog1.FileName)                                    ‘保存图片
        End If
End Sub

 

8.ColorDialog组件的使用

If ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then              ‘显示选择颜色的对话框
            Label1.BackColor = ColorDialog1.Color                                               ‘所选颜色作为label1的背景色
End If

 

9.播放音频

法一:

Dim i As Integer

For i=1 To 1000

     Beep()

Next i

法二:添加Windows Media Player控件,右击控件在属性框里添加音频。

 

10. 读写文件

Imports System.IO

Public Class Form1

    Private Sub ButtonWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim wr As StreamWriter
        Dim str As String
        wr = New StreamWriter("C:\textFile.txt", True)
        str = TextBox1.Text
        Do
            If Trim(str) = "" Then
                Exit Do
            End If
            wr.Write(str.Substring(0, 1))
            str = str.Substring(1, str.Length – 1)    ‘每次写一个字符,直到str为空
        Loop Until str = ""
        wr.WriteLine()
        wr.Close()
    End Sub

    Private Sub ButtonReader_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim rd As StreamReader
        Dim str As String
        rd = New StreamReader("C:\textFile.txt")  ’读取文件
        str = rd.ReadToEnd                                      ’读到底
        Label1.Text = str
    End Sub
End Class

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s