VB.Net入门版学习笔记

目录

VS中创建VB.Net项目

生成桌面应用程序exe

变量的声明

常量的声明

Case条件选择语句

If条件判断

数据类型

数据类型转换

运算符号集

函数

计算器实战练习


VS中创建VB.Net项目

打开VS→文件→新建→项目 

生成桌面应用程序exe

第一个程序

控件:Label  Button

页面:

代码:

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Label1.Text = "这是我的第一个VB.Net程序"

    End Sub

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

    End Sub
End Class

 运行效果:

生成桌面文件exe

 

变量的声明

Public Class Form1
    Dim a As String '变量
    Private b As Integer = 50 '变量
    Public c As Integer '变量
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        b = 80
        b *= 100
        TextBox1.Text = b
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim F, I, B As Integer '变量
        F = 100
        I = 200
        B = 300
        TextBox3.Text = (F + I) * B
    End Sub
End Class

常量的声明

Public Class Form1
    Const str As Integer = 1 'Integer常量
    Const he As String = "Hello World" 'String常量
    Private a As Integer = 2
    Public b As String = "你好"
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TextBox1.Text = str
        TextBox3.Text = he
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    End Sub
End Class

Case条件选择语句

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim a As Integer = 2
        Dim b As Integer
        Select Case a '条件情况为xxx 
            Case 1 '情况为1
                b = 1
            Case 2 '情况为2
                b = 2
            Case 3 '情况为3
                b = 3
            Case Else '否则
                b = 0
        End Select
        TextBox1.Text = a '赋值
    End Sub

End Class

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Dim a As Integer = 2
        Dim a As Integer = TextBox1.Text
        Dim result As String
        Select Case a '条件情况为xxx 
            Case 1 '情况为1
                result = "差"
            Case 2 '情况为2
                result = "及格"
            Case 3 '情况为3
                result = "良"
            Case Else '否则
                result = "优"
        End Select
        TextBox3.Text = result '赋值
    End Sub
End Class

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim a As Integer = TextBox1.Text
        Dim result As String
        Select Case a '条件情况为xxx 
            Case 100 '情况为1
                result = "满分"
            Case 80 To 90 '情况为2
                result = "优"
            Case 70 To 80 '情况为3
                result = "良"
            Case 60 To 70
                result = "及格"
            Case Else '否则
                result = "不及格"
        End Select
        TextBox3.Text = result '赋值
    End Sub
End Class

If条件判断

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim a As Integer = 2 '条件
        Dim result As String = ""
        If a = 2 Then '如果a=2则...
            result = "OK"
        End If
        TextBox1.Text = result '赋值
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim score As Integer = 60 '条件
        Dim result As String = "" '定义一个空字符串
        If score > 60 Then
            result = "及格"
        ElseIf score >= 90 Then '否则如果...则...
            result = "优"
        ElseIf score > 80 Then '否则如果...则...
            result = "良"
        Else '否则
            result = "不及格"
        End If
        TextBox3.Text = result '赋值
    End Sub
End Class

数据类型

数据类型摘要,链接地址:数据类型摘要 - Visual Basic | Microsoft Docs 

公共语言运行时类型结构Visual Basic 类型标称存储分配取值范围
Boolean布尔依赖于实现平台True 或 False
ByteByte1 个字节0到 255 (无符号)
CharChar (单个字符)2 个字节0到 65535 (无符号)
DateTime日期8 个字节0:00:00 (0001 年1月1日午夜) 9999,年12月1日到 11:59:59 PM
Decimal十进制16 个字节0到 +/-79228162514264337593543950335 (+/-7.9...E + 28) † ,无小数点;0到 +/-7.9228162514264337593543950335,小数点右侧有28个位置;

最小非零数字为 +/-0.0000000000000000000000000001 (+/-1E-28) †
Double 精度 (双精度浮点)8 个字节-1.79769313486231570 e + 308 到-4.94065645841246544 E-324 † 表示负值;

4.94065645841246544 e-324 到 1.79769313486231570 E + 308 † 用于正值
Int32Integer4 个字节-2147483648 到 2147483647 (有符号)
Int64Long(长整型)8 个字节-9223372036854775808 到 9223372036854775807 (9.2. E + 18 †) (签名)
Object (类)对象32位平台上的4个字节

64位平台上的8个字节
任何类型都可以存储在类型的变量中 Object
SByte        SByte1 个字节-128 到 127 (有符号)
Int16Short (短整型)2 个字节-32768 到 32767 (有符号)
Single单个 (单精度浮点)4 个字节-3.4028235 e + 38 到-1.401298 E-45 † 表示负值;

1.401298 e-45 到 3.4028235 E + 38 † 用于正值
String (类)字符串 (长度可变的)依赖于实现平台0到约 2000000000 Unicode 字符
UInt32UInteger4 个字节0到 4294967295 (无符号)
UInt64ULong8 个字节0到 18446744073709551615 (1.8 ... E + 19 †) (无符号)
(继承自 ValueType)用户定义的 (结构)依赖于实现平台结构的每个成员都有一个由其数据类型确定的范围,并与其他成员的范围无关
UInt16UShort2 个字节0到 65535 (无符号)

数据类型转换

转换函数返回值释义
CBool (exp)Boolean将参数转换成Boolean类型并返回来
CChar (exp)Char将参数转换成Date类型,参数须0~65536
CDate (exp)Date将参数转换成Date类型,参数须为有效日期时间
CDbl (exp)Double将参数转换成Double类型
CDec (exp)Decimal将参数转换成Decimal类型
CInt (exp)Integer将参数转换成Integer类型,小数部分四舍五入
CLngLong将参数转换成Long类型,小数部分四舍五入
CObj (exp)Object将参数转换成Object类型,参数须为有效的表达式
CShort (exp)Short将参数转换成Short类型,小数部分四舍五入
CSng (exp)Single将参数转换成Single类型
CStr (exp)String将参数转换成String类型,要注意的是若参数为Boolean类型,则返回的字符串为“True”或“False”;若为Date类型,返回短日期格式显示
CType/这个函数的调用形式为:Type (expression, typename),也就是将第一个参数转换为第二个参数所指定类型后返回

运算符号集

链接地址:vb vb.net 运算符号集_Linyee编程专栏-CSDN博客_vb运算符

运算符描述Example
And它是逻辑以及按位AND运算符。 如果两个操作数都为真,则条件为真。 此运算符不执行短路,即,它评估两个表达式。(A And B) is False.
Or它是逻辑以及按位或运算符。 如果两个操作数中的任何一个为真,则条件为真。 此运算符不执行短路,即,它评估两个表达式。(A Or B) is True.
Not它是逻辑以及按位非运算符。 用于反转其操作数的逻辑状态。 如果条件为真,则逻辑非运算符将为假。Not(A And B) is True.
Xor它是逻辑以及按位逻辑异或运算符。 如果两个表达式都为True或两个表达式都为False,则返回True; 否则,它返回False。 该运算符不会执行短路,它总是评估这两个表达式,并且没有该运算符的短路对应。A Xor B is True.
AndAlso它是逻辑AND运算符。 它只适用于布尔数据。 它执行短路。(A AndAlso B) is False.
OrElse它是逻辑或运算符。 它只适用于布尔数据。 它执行短路。A OrElse B) is True.
IsFalse它确定表达式是否为False。/
IsTrueIt determines whether an expression is True.
它确定表达式是否为True。
/

函数

链接地址:VB.Net - 函数_w3cschool

计算器实战练习

源码地址:

链接:https://pan.baidu.com/s/13aJVp3h6SNC7ugCx86ElZQ 
提取码:39a1 

页面:

代码:

Public Class Form1
    Dim txtVal As Double = 0 '定义一个全局变量用来记录每次输入的值
    Dim flag As String = "+" '定义一个运算符变量标记位
    '按钮1
    Private Sub BtnOne_Click(sender As Object, e As EventArgs) Handles BtnOne.Click
        InputVal(BtnOne.Text)
    End Sub
    '按钮2
    Private Sub BtnTwo_Click(sender As Object, e As EventArgs) Handles BtnTwo.Click
        InputVal(BtnTwo.Text)
    End Sub
    '按钮3
    Private Sub BtnThree_Click(sender As Object, e As EventArgs) Handles BtnThree.Click
        InputVal(BtnThree.Text)
    End Sub
    '按钮4
    Private Sub BtnFour_Click(sender As Object, e As EventArgs) Handles BtnFour.Click
        InputVal(BtnFour.Text)
    End Sub
    '按钮5
    Private Sub BtnFive_Click(sender As Object, e As EventArgs) Handles BtnFive.Click
        InputVal(BtnFive.Text)
    End Sub
    '按钮6
    Private Sub BtnSix_Click(sender As Object, e As EventArgs) Handles BtnSix.Click
        InputVal(BtnSix.Text)
    End Sub
    '按钮7
    Private Sub BtnSeven_Click(sender As Object, e As EventArgs) Handles BtnSeven.Click
        InputVal(BtnSeven.Text)
    End Sub
    '按钮8
    Private Sub BtnEight_Click(sender As Object, e As EventArgs) Handles BtnEight.Click
        InputVal(BtnEight.Text)
    End Sub
    '按钮9
    Private Sub BtnNine_Click(sender As Object, e As EventArgs) Handles BtnNine.Click
        InputVal(BtnNine.Text)
    End Sub
    '按钮0
    Private Sub BtnZero_Click(sender As Object, e As EventArgs) Handles BtnZero.Click
        If TbVal.Text = "0" Then

        Else
            TbVal.Text &= BtnZero.Text
        End If
    End Sub
    '按钮点
    Private Sub BtnDot_Click(sender As Object, e As EventArgs) Handles BtnDot.Click
        TbVal.Text &= BtnDot.Text
    End Sub
    '按钮清除
    Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
        TbVal.Text = 0
        txtVal = 0
        flag = "+"
    End Sub

    Private Sub InputVal(inVal As String)
        If TbVal.Text = "0" Then
            TbVal.Text = inVal
        Else
            TbVal.Text &= inVal
        End If
    End Sub
    '按钮加
    Private Sub BtnJia_Click(sender As Object, e As EventArgs) Handles BtnJia.Click
        '判断输入的内容是否为数字,否则返回空
        If Not IsNumeric(TbVal.Text) Then
            MsgBox("输入的内容无法计算")
            Return
        End If
        txtVal = TbVal.Text
        TbVal.Text = 0
        flag = "+"
    End Sub
    '按钮等于
    Private Sub BtnEqual_Click(sender As Object, e As EventArgs) Handles BtnEqual.Click
        '判断输入的内容是否为数字,否则返回空
        If Not IsNumeric(TbVal.Text) Then
            MsgBox("输入的内容无法计算")
            Return
        End If
        '此时输入框的值=上一次记录的值+输入框当前输入的值,这里要对数据做类型转换
        Select Case flag
            Case "+"
                TbVal.Text = txtVal + CDbl(TbVal.Text)
            Case "-"
                TbVal.Text = txtVal - CDbl(TbVal.Text)
            Case "*"
                TbVal.Text = txtVal * CDbl(TbVal.Text)
            Case "/"
                TbVal.Text = txtVal / CDbl(TbVal.Text)
        End Select
        txtVal = TbVal.Text
    End Sub
    '按钮乘
    Private Sub BtnChen_Click(sender As Object, e As EventArgs) Handles BtnChen.Click
        '判断输入的内容是否为数字,否则返回空
        If Not IsNumeric(TbVal.Text) Then
            MsgBox("输入的内容无法计算")
            Return
        End If
        txtVal = TbVal.Text
        TbVal.Text = 0
        flag = "*"
    End Sub
    '按钮除
    Private Sub BtnChu_Click(sender As Object, e As EventArgs) Handles BtnChu.Click
        '判断输入的内容是否为数字,否则返回空
        If Not IsNumeric(TbVal.Text) Then
            MsgBox("输入的内容无法计算")
            Return
        End If
        txtVal = TbVal.Text
        TbVal.Text = 0
        flag = "/"
    End Sub
    '按钮减
    Private Sub BtnJian_Click(sender As Object, e As EventArgs) Handles BtnJian.Click
        '判断输入的内容是否为数字,否则返回空
        If Not IsNumeric(TbVal.Text) Then
            MsgBox("输入的内容无法计算")
            Return
        End If
        If TbVal.Text = "0" Then
            TbVal.Text = "-"
        Else
            txtVal = TbVal.Text
            TbVal.Text = 0
            flag = "-"
        End If
    End Sub
End Class

 运行效果:

循环

For循环

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        '循环 i 从 0 到 9
        For i = 0 To 9
            System.Console.WriteLine(i) '打印出循环的值
        Next
    End Sub
End Class

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        '循环 i 从 0 到 9 步长 2
        For i = 0 To 9 step 2
            System.Console.WriteLine(i) '打印出循环的值
        Next
    End Sub
End Class

  • 1
    点赞
  • 3
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
评论

打赏作者

梦想天涯~路在脚下

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值