⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 Mastering VBNet Include Source Code
💻 VB
字号:
Option Strict On
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    Friend WithEvents NumericUpDown1 As System.Windows.Forms.NumericUpDown
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.Container

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.NumericUpDown1 = New System.Windows.Forms.NumericUpDown()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.Button2 = New System.Windows.Forms.Button()
        CType(Me.NumericUpDown1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'NumericUpDown1
        '
        Me.NumericUpDown1.Font = New System.Drawing.Font("Comic Sans MS", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
        Me.NumericUpDown1.Location = New System.Drawing.Point(400, 16)
        Me.NumericUpDown1.Minimum = New Decimal(New Integer() {1, 0, 0, 0})
        Me.NumericUpDown1.Name = "NumericUpDown1"
        Me.NumericUpDown1.Size = New System.Drawing.Size(48, 26)
        Me.NumericUpDown1.TabIndex = 0
        Me.NumericUpDown1.Value = New Decimal(New Integer() {1, 0, 0, 0})
        '
        'Button1
        '
        Me.Button1.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Button1.Location = New System.Drawing.Point(312, 182)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(136, 40)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Process Values (For ... Next)"
        '
        'Button2
        '
        Me.Button2.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Button2.Location = New System.Drawing.Point(312, 224)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(136, 40)
        Me.Button2.TabIndex = 1
        Me.Button2.Text = "Process Values (For Each ... Next)"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(456, 273)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button2, Me.Button1, Me.NumericUpDown1})
        Me.Name = "Form1"
        Me.Text = "Dynamic Form"
        CType(Me.NumericUpDown1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub TBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs)
        CType(sender, TextBox).BackColor = Color.LightCoral
    End Sub

    Private Sub TBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
        CType(sender, TextBox).BackColor = Color.White
    End Sub

    Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
        Dim TB As New TextBox()
        Dim LBL As New Label()
        Dim i, TBoxes As Integer
        '   Count all TextBox controls on the Form
        For i = 0 To Me.Controls.Count - 1
            If Me.Controls(i).GetType Is GetType(System.Windows.Forms.TextBox) Then
                TBoxes = TBoxes + 1
            End If
        Next
        ' Add new controls if number of controls on the Form is less
        ' than the number specified with the NumericUpDown control
        If TBoxes < NumericUpDown1.Value Then
            TB.Left = 100
            TB.Width = 120
            TB.Text = ""
            For i = TBoxes To CInt(NumericUpDown1.Value) - 1
                TB = New TextBox()
                LBL = New Label()
                If NumericUpDown1.Value = 1 Then
                    ' the first TextBox control is added to the 
                    ' Controls collection when the form is loaded
                    TB.Top = 20
                Else
                    ' If this is the first TextBox control added to the
                    ' Controls collection by increasing the value of the 
                    ' NumericUpDown control, set its Top coordinate explicitly
                    If NumericUpDown1.Value = 2 Then
                        TB.Top = 20 + TB.Height + 5
                    Else
                        ' all other TextBox controls on the form are placed
                        ' 5 pixels below the previous TextBox control
                        TB.Top = Me.Controls(Me.Controls.Count - 2).Top + 25
                    End If
                End If
                ' Set the trivial properties of the new controls
                LBL.Left = 20
                LBL.Width = 80
                LBL.Text = "Data Point " & i
                LBL.Top = TB.Top + 3
                TB.Left = 100
                TB.Width = 120
                TB.Text = ""
                ' add controls to the form
                Me.Controls.Add(TB)
                Me.Controls.Add(LBL)
                ' and finally connect their GotFocus/LostFocus events to the appropriate handler
                AddHandler TB.Enter, New System.EventHandler(AddressOf TBox_Enter)
                AddHandler TB.Leave, New System.EventHandler(AddressOf TBox_Leave)
            Next
        Else
            For i = Me.Controls.Count - 1 To Me.Controls.Count - 2 * (TBoxes - CInt(NumericUpDown1.Value)) Step -2
                Me.Controls.Remove(Controls(i))
                Me.Controls.Remove(Controls(i - 1))
            Next
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ctrl As Object
        Dim Sum As Double = 0, points As Integer = 0
        Dim iCtrl As Integer
        For iCtrl = 0 To Me.Controls.Count - 1
            ctrl = Me.Controls(iCtrl)
            If ctrl.GetType Is GetType(System.Windows.Forms.TextBox) Then
                If IsNumeric(CType(ctrl, TextBox).Text) Then
                    ' Take the value of the text on the control to use it in numeric calculations
                    ' Val(string) return the numeric value of the string
                    Sum = Sum + Val(CType(ctrl, TextBox).Text)
                    points = points + 1
                End If
            End If
        Next
        MsgBox("The sum of the " & points.ToString & _
                     " data points is " & Sum.ToString)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim TB As Control
        Dim Sum As Double = 0, points As Integer = 0
        For Each TB In Me.Controls
            If TB.GetType Is GetType(Windows.Forms.TextBox) Then
                If IsNumeric(CType(TB, TextBox).Text) Then
                    ' Take the value of the text on the control to use it in numeric calculations
                    Sum = Sum + Val(CType(TB, TextBox).Text)
                    points = points + 1
                End If
            End If
        Next
        MsgBox("The sum of the " & points.ToString & _
             " data points is " & Sum.ToString)
    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -