問visual basic

2009-03-23 10:11 am
我係form1 Dim x係一個Integer


我想係form2既label1度讀返x出黎

個form1已經可以call到個form2出黎,但我唔識點可以係form2既
label1度讀返x出黎


我知道如果要係form2既label1度讀返from1 textbox1入面既野係
Form1.TextBox1.Text,於是我試過打form1.x,不過vb話
無法在此內容中存取 'WindowsApplication1.Form1.x',因為它是 'Private'。


可以點做,如果可以我盡量唔想係兩個form度加任何野


唔該各位!!

回答 (1)

2009-03-23 3:16 pm
✔ 最佳答案
Basically you will need communication between the two forms.
One way to do it is to create form2 from form1, thereby creating a reference of form2 in form1. While doing this, you will also create a back-reference to form1 from form2. This will allow a two-way communication. The reference to form2 inside of form1 must be created as a class variable, and outside of all sub-programs.
The integer variable x will not be accessible if it was created inside of a sub program. So, again, you would put x outside of subprograms to make it a class variable, and thus accessible to form2. Putting public instead of dim will ensure it is accessible by everyone.
Here is an example where form1 has a button to create form2, and form2 has a button to read the value of x in form1 and put it in textbox1.
If you need more information, PM me your e-mail address. Yahoo limits the size of supplementary answers and will probably not be sufficient.
Here's the example code for form1, hope it is not too modified by Yahoo:

Dim myForm As Form2
Public x As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If myForm Is Nothing Then

myForm = New Form2()
myForm.myParent = Me

End If

x = x + 1
TextBox1.Text = myForm.TextBox1.Text
myForm.Show()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
x = 10
End Sub

Here's the code for form2:

Public myParent As Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = "" & myParent.x
End Sub




收錄日期: 2021-04-13 16:31:16
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090323000051KK00143

檢視 Wayback Machine 備份