如何将整数值传递给其他表单?

时间:2017-04-24 18:45:14

标签: vb.net

是否可以使用标签?我打算显示一个评分系统,就像每个正确的答案一样,分数会增加10分。

3 个答案:

答案 0 :(得分:1)

在表单中使用类的实例。 Form可以调用Class的方法。班级可以向表格提出活动。不要直接从课堂谈话到表格。或者从一种形式到另一种形式。

Form1 with TextBox1:

Public Class Form1

    Private myClass1 As Class1
    Private myForm2 As Form2

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        myClass1 = New Class1()
        myForm2 = New Form2(myClass1)
        myForm2.Show()

    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        myClass1.SetText(TextBox1.Text)
    End Sub

End Class

带有Label1的Form2:

Public Class Form2

    Private WithEvents myClass1 As Class1

    Public Sub New(instance As Class1)
        InitializeComponent()
        myClass1 = instance
    End Sub

    Private Sub myClass1TextSet(value As String) Handles myClass1.TextSet
        Me.Label1.Text = value
    End Sub

End Class

的Class1:

Public Class Class1

    Private text As String = ""

    Public Event TextSet(value As String)

    Public Sub SetText(value As String)
        Me.text = value
        RaiseEvent TextSet(value)
    End Sub

End Class

当您在Form1.TextBox1中键入时,Form2.Label1将更新。您可以根据需要调整应用程序进行更改,但请尝试保留此结构。

表格>>实例>>类

类>>事件>>表格

实例化另一种形式的表单是为了简化此示例。较大规模的项目可以有一个表单加载器工厂负责制作表单。

答案 1 :(得分:0)

在Form2中添加一个公共共享整数

 Public Shared score As Integer = 0

 Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

 End Sub

然后在Form1中通过添加来增加它

 Form2.score += 50

答案 2 :(得分:-1)

例如,

添加两个表单到项目 在Form2中定义Label1控件 并在Form1

Public Class Form1
    Private intScore As Integer = 0
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Form2.Show()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Form2.Label1.Text = intScore.ToString
    End Sub
End Class