|
-
Feb 27th, 2003, 11:53 PM
#1
Thread Starter
Frenzied Member
Data Across Two Forms
I think i've looked at every example on this site regarding this, and i can't seem to figure out why this doesn't work for me.
On Form1, i have this code:
VB Code:
Public Class Form1
Inherits System.Windows.Forms.Form
'Windows Form Designer generated code is here .....
Private Sub btnFormTwo_Click(.......) Handles btnFormTwo.Click
Dim xNewForm As New Form2()
xNewForm.Show()
End Sub
Public Property FormTextData() As String
Get
FormTextData = TextBox1.Text
End Get
Set(ByVal Value As String)
TextBox1.Text = Value
End Set
End Property
End Class
Then on Form2, i try to use this: TextBox1.Text = Form1.FormTextData
But it gives me the squiggles under Form1.FormTextData and says that Reference to a non-shared member requires an object reference.
I've done everything right as far as i can see, but it doesn't seem to want to work. What am i missing?
~Peter

-
Feb 28th, 2003, 05:54 AM
#2
Frenzied Member
Theorically as you are accessing a property in form1 you have to make a reference to it in form2:
VB Code:
Dim frm as New Form1
MessageBox.Show(frm.FormTextData)
But applying your method wont return a value in form2 unless you explicitly define the text property of textbox1 in form1 constructor. So you may change it in this way:
Form1:
VB Code:
Public Shared txt As String
Public Property FormTextData() As String
Get
Return txt
End Get
Set(ByVal Value As String)
txt = Value
End Set
End Property
Private Sub btnFormTwo_Click(.......) Handles btnFormTwo.Click
txt=Textbox1.Text
Dim xNewForm As New Form2()
xNewForm.Show()
End Sub
and use the previous code in form2
Alternativly you may define a nested class in form1 and refer to it in form2
VB Code:
Public Class txtdata
Public Shared txt As String
Public Property FormTextData() As String
Get
Return txt
End Get
Set(ByVal Value As String)
txt = Value
End Set
End Property
Private Sub btnFormTwo_Click(.......) Handles btnFormTwo.Click
Dim c_txt As New txtdata()
c_txt.FormTextData = TextBox1.Text
Dim xNewForm As New Form2()
xNewForm.Show()
End Sub
and in form2:
VB Code:
Dim frmtxt As New Form1.txtdata()
MessageBox.Show(frmtxt.FormTextData)
-
Feb 28th, 2003, 10:40 AM
#3
Thread Starter
Frenzied Member
Thanks for the help. I can get it to work if i want to get data from Form1 and show it on Form2. But changing the text box back on Form1 with data from Form2 is proving difficult.
I had a minor brain storm, and i tried this:
In a code module:
VB Code:
Module CodeModule
Public frmMainOne As System.Windows.Forms.Form
End Module
In Form1:
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
frmMainOne = Me
End Sub
In Form2:
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Gets the caption from the first form
txtBox6.Text = frmMainOne.Text
End Sub
It works fine if i just want to get values related to Form1, but i can't seem to access the controls on Form1 using this method. If i go this method, how can i see the various controls on the form?
~Peter

-
Feb 28th, 2003, 01:24 PM
#4
Frenzied Member
If you want to send values of form1 controls to form2 and then from form2 back to form1 you can do this:
make this changes in Form2
VB Code:
Dim frm As Form
Public Sub New(ByVal frm As Form)
MyBase.New()
Me.frm = frm
'This call is required by the Windows Form Designer.
.....
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show(CType(frm, Form1).TextBox1.Text)
CType(frm, Form1).TextBox1.Text = "Test"
' This first shows the value of textbox1 on form1 and then sets it to 'Test'
End Sub
And put this in form1 where you want to show form2:
VB Code:
Private Sub btnFormTwo_Click(.......) Handles btnFormTwo.Click
Dim xNewForm As New Form2(Me) 'Here you should pass form1 to form2
xNewForm.Show()
End Sub
-
Feb 28th, 2003, 05:06 PM
#5
Thread Starter
Frenzied Member
Now that is what i'm talking about! That is perfect.
Thanks Lunatic3. That works perfectly. It's exactly what i wanted, and it works smoothly.
~Peter

-
Mar 21st, 2003, 06:13 AM
#6
Addicted Member
Here's another very useful trick. I have a text box in form1 that I want to be filled with a value that is in a listbox in form2. When I double-click in the listbox, textbox in form1 should get that value.
Here's what I have in form1:
VB Code:
Dim f As New Form2
'Write this in the event you want it, mine is in a button_click
If (f.ShowDialog() = DialogResult.OK) Then
txtShortName.Text = f.Data
End If
And in form2:
VB Code:
Private _record_data As String
Private Sub lstList_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstList.DoubleClick
_record_data = lstList.Text
DialogResult = DialogResult.OK
End Sub
Public ReadOnly Property Data()
Get
Return _record_data
End Get
End Property
Get it? Instead of a f.show I write f.showdialog and give it the OK value myself once my procedures are complete. The end user never knows what's behind it!
-
Mar 21st, 2003, 07:36 AM
#7
Frenzied Member
I think that you have assumed the user is going to return values from form2 when she is closing that form, Am I right? If so its not always the case. You may want to have both forms active and pass data between them real time.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
-
Mar 21st, 2003, 07:55 AM
#8
Addicted Member
Yes that is correct. I don't need form2 open after I get the data I want int he textbox in form1.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|