form1.vb

来自「《CSDN 开发高手》2004年第6期」· VB 代码 · 共 326 行

VB
326
字号
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写处置以清理组件列表。
    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

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意:以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
    Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents TabPage2 As System.Windows.Forms.TabPage
    Friend WithEvents txtX As System.Windows.Forms.TextBox
    Friend WithEvents txtY As System.Windows.Forms.TextBox
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents TabPage3 As System.Windows.Forms.TabPage
    Friend WithEvents txtRadius As System.Windows.Forms.TextBox
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Button4 As System.Windows.Forms.Button
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents txtWidth As System.Windows.Forms.TextBox
    Friend WithEvents txtHeight As System.Windows.Forms.TextBox
    Friend WithEvents Button5 As System.Windows.Forms.Button
    Friend WithEvents Button6 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TabControl1 = New System.Windows.Forms.TabControl()
        Me.TabPage1 = New System.Windows.Forms.TabPage()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.TabPage2 = New System.Windows.Forms.TabPage()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Button3 = New System.Windows.Forms.Button()
        Me.txtY = New System.Windows.Forms.TextBox()
        Me.txtX = New System.Windows.Forms.TextBox()
        Me.TabPage3 = New System.Windows.Forms.TabPage()
        Me.Button6 = New System.Windows.Forms.Button()
        Me.Button5 = New System.Windows.Forms.Button()
        Me.txtHeight = New System.Windows.Forms.TextBox()
        Me.txtWidth = New System.Windows.Forms.TextBox()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.Button4 = New System.Windows.Forms.Button()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.txtRadius = New System.Windows.Forms.TextBox()
        Me.TabControl1.SuspendLayout()
        Me.TabPage1.SuspendLayout()
        Me.TabPage2.SuspendLayout()
        Me.TabPage3.SuspendLayout()
        Me.SuspendLayout()
        '
        'TabControl1
        '
        Me.TabControl1.Controls.AddRange(New System.Windows.Forms.Control() {Me.TabPage1, Me.TabPage2, Me.TabPage3})
        Me.TabControl1.Location = New System.Drawing.Point(24, 56)
        Me.TabControl1.Name = "TabControl1"
        Me.TabControl1.SelectedIndex = 0
        Me.TabControl1.Size = New System.Drawing.Size(472, 248)
        Me.TabControl1.TabIndex = 2
        '
        'TabPage1
        '
        Me.TabPage1.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button2, Me.Button1})
        Me.TabPage1.Location = New System.Drawing.Point(4, 21)
        Me.TabPage1.Name = "TabPage1"
        Me.TabPage1.Size = New System.Drawing.Size(464, 223)
        Me.TabPage1.TabIndex = 0
        Me.TabPage1.Text = "组件组合调用"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(124, 99)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(216, 32)
        Me.Button2.TabIndex = 3
        Me.Button2.Text = "直接调用VB组件,间接调用C#组件"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(124, 51)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(216, 32)
        Me.Button1.TabIndex = 2
        Me.Button1.Text = "调用C#组件中的方法"
        '
        'TabPage2
        '
        Me.TabPage2.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label2, Me.Label1, Me.Button3, Me.txtY, Me.txtX})
        Me.TabPage2.Location = New System.Drawing.Point(4, 21)
        Me.TabPage2.Name = "TabPage2"
        Me.TabPage2.Size = New System.Drawing.Size(464, 223)
        Me.TabPage2.TabIndex = 1
        Me.TabPage2.Text = "组件继承"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(208, 24)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(32, 16)
        Me.Label2.TabIndex = 4
        Me.Label2.Text = "Y="
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(56, 24)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(24, 16)
        Me.Label1.TabIndex = 3
        Me.Label1.Text = "X="
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(120, 104)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(176, 24)
        Me.Button3.TabIndex = 2
        Me.Button3.Text = "调用组件功能显示计算结果"
        '
        'txtY
        '
        Me.txtY.Location = New System.Drawing.Point(240, 24)
        Me.txtY.Name = "txtY"
        Me.txtY.Size = New System.Drawing.Size(112, 21)
        Me.txtY.TabIndex = 1
        Me.txtY.Text = "0"
        '
        'txtX
        '
        Me.txtX.Location = New System.Drawing.Point(88, 24)
        Me.txtX.Name = "txtX"
        Me.txtX.Size = New System.Drawing.Size(104, 21)
        Me.txtX.TabIndex = 0
        Me.txtX.Text = "0"
        '
        'TabPage3
        '
        Me.TabPage3.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button6, Me.Button5, Me.txtHeight, Me.txtWidth, Me.Label5, Me.Label4, Me.Button4, Me.Label3, Me.txtRadius})
        Me.TabPage3.Location = New System.Drawing.Point(4, 21)
        Me.TabPage3.Name = "TabPage3"
        Me.TabPage3.Size = New System.Drawing.Size(464, 223)
        Me.TabPage3.TabIndex = 2
        Me.TabPage3.Text = "组件的多态使用"
        '
        'Button6
        '
        Me.Button6.Location = New System.Drawing.Point(80, 168)
        Me.Button6.Name = "Button6"
        Me.Button6.Size = New System.Drawing.Size(264, 24)
        Me.Button6.TabIndex = 8
        Me.Button6.Text = "利用多态使用接口来求几何图形的面积"
        '
        'Button5
        '
        Me.Button5.Location = New System.Drawing.Point(240, 96)
        Me.Button5.Name = "Button5"
        Me.Button5.Size = New System.Drawing.Size(176, 32)
        Me.Button5.TabIndex = 7
        Me.Button5.Text = "调用C#组件求长方形面积"
        '
        'txtHeight
        '
        Me.txtHeight.Location = New System.Drawing.Point(312, 56)
        Me.txtHeight.Name = "txtHeight"
        Me.txtHeight.Size = New System.Drawing.Size(96, 21)
        Me.txtHeight.TabIndex = 6
        Me.txtHeight.Text = "0"
        '
        'txtWidth
        '
        Me.txtWidth.Location = New System.Drawing.Point(312, 16)
        Me.txtWidth.Name = "txtWidth"
        Me.txtWidth.Size = New System.Drawing.Size(96, 21)
        Me.txtWidth.TabIndex = 5
        Me.txtWidth.Text = "0"
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(232, 56)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(56, 24)
        Me.Label5.TabIndex = 4
        Me.Label5.Text = "高"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(232, 16)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(64, 24)
        Me.Label4.TabIndex = 3
        Me.Label4.Text = "宽"
        '
        'Button4
        '
        Me.Button4.Location = New System.Drawing.Point(16, 96)
        Me.Button4.Name = "Button4"
        Me.Button4.Size = New System.Drawing.Size(184, 32)
        Me.Button4.TabIndex = 2
        Me.Button4.Text = "调用VB组件求圆面积"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(8, 24)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(56, 16)
        Me.Label3.TabIndex = 1
        Me.Label3.Text = "圆半径"
        '
        'txtRadius
        '
        Me.txtRadius.Location = New System.Drawing.Point(72, 16)
        Me.txtRadius.Name = "txtRadius"
        Me.txtRadius.Size = New System.Drawing.Size(128, 21)
        Me.txtRadius.TabIndex = 0
        Me.txtRadius.Text = "0"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(544, 349)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TabControl1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.TabControl1.ResumeLayout(False)
        Me.TabPage1.ResumeLayout(False)
        Me.TabPage2.ResumeLayout(False)
        Me.TabPage3.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub CalArea(ByVal obj As CSharpClassNameSpace.IMyShape)
        MessageBox.Show(Convert.ToString(obj.Area()))
    End Sub


   

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim VBObj2 As New VBComponent.ExtendsFromCSharp()
        Dim x, y As Integer
        Dim ret As Long
        x = Convert.ToInt32(txtX.Text)
        y = Convert.ToInt32(txtY.Text)
        ret = VBObj2.Add(x, y)
        VBObj2.SaySomething("X+Y=" + Convert.ToString(ret))

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim VBObj3 As New VBComponent.VBCircle()
        Dim radius As Single
        radius = Convert.ToSingle(txtRadius.Text)
        VBObj3.Radius = radius
        MessageBox.Show("圆面积是:" + Convert.ToString(VBObj3.Area()))
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim CSharpObj As New CSharpClassNameSpace.CSharpRect()
        Dim height, width As Single
        Dim area As Double
        CSharpObj.width = Convert.ToSingle(txtWidth.Text)
        CSharpObj.height = Convert.ToSingle(txtHeight.Text)

        area = CSharpObj.Area()
        MessageBox.Show("矩形面积是:" + Convert.ToString(area))
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim VBObj3 As New VBComponent.VBCircle()
        Dim radius As Single
        radius = Convert.ToSingle(txtRadius.Text)
        VBObj3.Radius = radius
        Dim CSharpObj As New CSharpClassNameSpace.CSharpRect()
        Dim height, width As Single
        Dim area As Double
        CSharpObj.width = Convert.ToSingle(txtWidth.Text)
        CSharpObj.height = Convert.ToSingle(txtHeight.Text)

        CalArea(VBObj3)
        CalArea(CSharpObj)

    End Sub


    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim obj As CSharpClassNameSpace.CSharpClass
        obj = New CSharpClassNameSpace.CSharpClass()
        obj.SaySomething("我是从VB中调用C#组件中的方法而出现的  ")
    End Sub

    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim VBobj As VBComponent.VBClass
        VBobj = New VBComponent.VBClass()
        VBobj.VBSaySomething("我在VB工程中调VB组件中的方法")
    End Sub
End Class

⌨️ 快捷键说明

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