我是个vb.net的新手,我想用点机form1中的button1,然后form2出现,然后在点机form2的button1使form1中的textbox1的text等于“你好”,请问怎么做?
小弟在这先谢谢了!
这个问题比较棘手,一般情况下人的的常识是去直接调用from1的textbox1的.text属性
但是那样是不行的,那样是没结果,关键在于不能直接调用.text属性是一个局部函数
将它设为全局的,就可以调用了
当我刚接到这个问题是是这样解决的
form1中的 Button1_Click
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm2 As New Form2
frm2.Show()
End Sub
form2中的 Button1_Click
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myform2 As New Form1
form1.textbox1.text = "你好!"
End Sub
这样做是一个没有结果的程序,form2到是调用了,但是text没显示
正确的结果是一个调用全局函数的窗体,代码如下:(全局函数我用红色表明!)
form1中的代码
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm2 As New Form2
frm2.Show()
Me.Hide()
End Sub
Public Property CustomerName() As String
Get
Return TextBox1.Text
End Get
Set(ByVal Value As String)
TextBox1.Text = Value
End Set
End Property
Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
Application.Exit()
End Sub
form2中的代码
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myform2 As New Form1
myform2.CustomerName = ""
myform2.Show()
myform2.CustomerName = "你好!"
End Sub
[此贴子已经被作者于2006-5-15 22:22:07编辑过]