VB如何实现打开窗体 填写后返回计算

2024-11-22 19:01:25
推荐回答(2个)
回答1:

在Form1里声明变量a、b、c
添加Button1、Button2、TextBox1
并且再Button1的Click事件中写出计算方法
Button2的Click事件是Form2的Show
Public Class Form1

Friend a, b, c As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = a + b + c
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Form2.Show()
End Sub

End Class

添加windows窗体Form2,然后在Form2里添加TextBox1、TextBox2、TextBox3
在Form2的FormClosing事件中将TextBox里的值传递给Form1的变量a、b、c
Public Class Form2

Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Form1.a = CInt(Me.TextBox1.Text)

Form1.b = CInt(Me.TextBox2.Text)

Form1.c = CInt(Me.TextBox3.Text)
End Sub
End Class

回答2:

在一个地方声明全局变量:
public a as string
然后调用的时候:
form1.a = text1.text(把form1改成你声明的地方)。