Results 1 to 8 of 8

Thread: Data Across Two Forms

  1. #1

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Question 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:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.  
    4.     'Windows Form Designer generated code is here .....
    5.  
    6.     Private Sub btnFormTwo_Click(.......) Handles btnFormTwo.Click
    7.         Dim xNewForm As New Form2()
    8.         xNewForm.Show()
    9.     End Sub
    10.  
    11.     Public Property FormTextData() As String
    12.         Get
    13.             FormTextData = TextBox1.Text
    14.         End Get
    15.         Set(ByVal Value As String)
    16.             TextBox1.Text = Value
    17.         End Set
    18.     End Property
    19.  
    20. 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


  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Theorically as you are accessing a property in form1 you have to make a reference to it in form2:
    VB Code:
    1. Dim frm as New Form1
    2. 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:
    1. Public Shared txt As String
    2.         Public Property FormTextData() As String
    3.             Get
    4.                 Return txt
    5.  
    6.             End Get
    7.             Set(ByVal Value As String)
    8.                 txt = Value
    9.             End Set
    10.         End Property
    11.  
    12.     Private Sub btnFormTwo_Click(.......) Handles btnFormTwo.Click
    13.         txt=Textbox1.Text
    14.         Dim xNewForm As New Form2()
    15.         xNewForm.Show()
    16.     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:
    1. Public Class txtdata
    2.         Public Shared txt As String
    3.         Public Property FormTextData() As String
    4.             Get
    5.                 Return txt
    6.  
    7.             End Get
    8.             Set(ByVal Value As String)
    9.                 txt = Value
    10.             End Set
    11.         End Property
    12.  
    13.    Private Sub btnFormTwo_Click(.......) Handles btnFormTwo.Click
    14.         Dim c_txt As New txtdata()
    15.         c_txt.FormTextData = TextBox1.Text
    16.         Dim xNewForm As New Form2()
    17.         xNewForm.Show()
    18.     End Sub

    and in form2:
    VB Code:
    1. Dim frmtxt As New Form1.txtdata()
    2.        MessageBox.Show(frmtxt.FormTextData)

  3. #3

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277
    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:
    1. Module CodeModule
    2.  
    3.     Public frmMainOne As System.Windows.Forms.Form
    4.  
    5. End Module
    In Form1:
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         frmMainOne = Me
    3.     End Sub
    In Form2:
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         'Gets the caption from the first form
    3.         txtBox6.Text = frmMainOne.Text
    4.     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


  4. #4
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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:
    1. Dim frm As Form
    2.     Public Sub New(ByVal frm As Form)
    3.  
    4.         MyBase.New()
    5.         Me.frm = frm
    6.         'This call is required by the Windows Form Designer.
    7.        .....
    8.  
    9.      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    10.         MessageBox.Show(CType(frm, Form1).TextBox1.Text)
    11.         CType(frm, Form1).TextBox1.Text = "Test"
    12.          ' This first shows the value of textbox1 on form1 and then sets it to 'Test'
    13.     End Sub

    And put this in form1 where you want to show form2:

    VB Code:
    1. Private Sub btnFormTwo_Click(.......) Handles btnFormTwo.Click
    2.         Dim xNewForm As New Form2(Me) 'Here you should pass form1 to form2
    3.         xNewForm.Show()
    4.     End Sub

  5. #5

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up

    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


  6. #6
    Addicted Member Zealot's Avatar
    Join Date
    Jul 2002
    Location
    Lisboa, Portugal
    Posts
    206
    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:
    1. Dim f As New Form2
    2.         'Write this in the event you want it, mine is in a button_click
    3.         If (f.ShowDialog() = DialogResult.OK) Then
    4.             txtShortName.Text = f.Data
    5.         End If
    And in form2:
    VB Code:
    1. Private _record_data As String
    2.  
    3. Private Sub lstList_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstList.DoubleClick
    4.         _record_data = lstList.Text
    5.         DialogResult = DialogResult.OK
    6. End Sub
    7.  
    8. Public ReadOnly Property Data()
    9.         Get
    10.             Return _record_data
    11.         End Get
    12. 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!

  7. #7
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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

  8. #8
    Addicted Member Zealot's Avatar
    Join Date
    Jul 2002
    Location
    Lisboa, Portugal
    Posts
    206
    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
  •  



Click Here to Expand Forum to Full Width