Results 1 to 10 of 10

Thread: [RESOLVED] Form sharing problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    Resolved [RESOLVED] Form sharing problem

    Form2 is not passing over information to Form1...

    Code for Form2:

    VB Code:
    1. Private m_myVar As String
    2.  
    3. Public Property listItem() As String
    4.   Get
    5.     Return Me.m_myVar
    6.   End Get
    7.   Set(ByVal Value As String)
    8.     Me.m_myVar = Value
    9.   End Set
    10. End Property
    11.  
    12. Private Sub Button1_Click(ByVal sender as System.Object, ByVal e As System.EventArgs)  Handles Button1.Click
    13.   Dim lv As ListViewItem = ListView1.SelectedItems(0)
    14.   Me.m_myVar = lv.SubItems(2).Text
    15.   Me.listItem = lv.SubItems(2).Text
    16.   Me.Close
    17. End Sub

    And for Form1 I have this:

    VB Code:
    1. Dim myForm As New Form2
    2. myForm.ShowDialogue()
    3. MessageBox.Show(myForm.listItem)

    on Form2 I get the correct text.
    on Form1 (which is loaded after Form2), I get a blank field.

    Any ideas?

    Cheers.
    Last edited by Cyber-Drugs; Jul 28th, 2005 at 09:46 AM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Form sharing problem

    Well, I may be misunderstanding, but the snippet of code you have labeled Form1 will create a NEW instance of Form2, and get the info from that.

    You state that form2 is loaded before form1, which I interpret to mean that there is an existing instance of Form2 at the time the code on Form1 is run. If you are expecting the code you listed to use the already existing instance of Form2, it is not. Instead, it is using myForm, which is a local instance of Form2 created in the line above the MessageBox line.

    To use the existing Form2, you will have to reference that instance. I don't know how you have it declared, so it may or may not be in scope for the instance of Form1 that you have.

    Keep in mind that a form is just like any other variable (ok, that's not true, but it is true for this example). Think of it as an integer. If you declared one instance in a module, and declared another instance in a sub on a form, you would not expect that second instance to take on the value of the first instance. The same thing is happening here.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    Re: Form sharing problem

    OK, so how do I call the information form Form2 into Form1? Is there a way of submitting the information or making a variable that can be read in any of the Forms in this project like Session Variables?

    Cheers.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Form sharing problem

    The form itself could be made as a session variable. After all, you must have declared that original form2 somewhere with a statement like:

    Dim mForm as New Form2.

    If that was done in a sub or function, then only that sub or function would be able to see mForm, however, if you were to put it outside the sub or function:

    Public mForm as New Form2

    Then it becomes a global instance of Form2, and is visible to all other variables in the application, including Form1. If you do that, you would simply need to get rid of the duplicate declaration that you have in Form1 (ok, I also have a slightly different name, but you get the idea), and it should work the way you want.

    Of course, then you have this global Form2 kicking around that you have to manage. That may be no big deal, or it may be that you will want to explicitly dispose of it at some time, or something like that. There are other, more complicated, ways of doing this if you want to encapsulate things more, but this is the easiest way, and one I use often when I have a form that other forms and functions need access to.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    Re: Form sharing problem

    OK, Im still having some troubles...

    Is someone willing to give me a hand over instant messenger, and then I can post the result once one is found?

    Cheers.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    Re: Form sharing problem

    Anybody?

  7. #7
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Form sharing problem

    if you want to get the value of the property in form2 then make the property as public shared.
    like this one
    VB Code:
    1. 'in your form2
    2. Shared s As String
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         mystring() = TextBox1.Text
    5.         Dim f As New Form1()
    6.         f.ShowDialog()
    7.     End Sub
    8.  
    9.     Public Shared Property mystring() As String
    10.         Get
    11.             Return s
    12.         End Get
    13.         Set(ByVal Value As String)
    14.             s = Value
    15.         End Set
    16.     End Property
    17. 'in form1
    18. 'no need to instantiate the form2
    19.  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    20.         MessageBox.Show(Form2.mystring())
    21.     End Sub
    edit: no need to put public in shared property because in vb.net shared methodd by default is public where as in c# static methods cannot be accessed outside of the class if it's not declared public.
    Last edited by mar_zim; Jul 29th, 2005 at 03:57 AM.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    Re: Form sharing problem

    Sorry for the confusing, let me explain a bit better...


    Form1 loads up
    Form1 shows Form2 with ShowDialogue()
    Form2 gets some information stored into a Public Property within Form2
    Form2 Closes
    Form1 tries to access Form2's Public Property but gets a blank

    How would I work around that?

  9. #9
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Form sharing problem

    ok if that is the case then no need to instantiate your form2 twice
    instatiate your form2 once and declared it globally
    just like this one
    VB Code:
    1. 'in your form1
    2. Dim f As New Form2()
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.  
    5.     End Sub
    6.  
    7.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    8.         f.ShowDialog()
    9.         MessageBox.Show(f.mystring)
    10.     End Sub
    11.  
    12. 'in your form2
    13. Dim s As String
    14.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    15.         mystring() = TextBox1.Text
    16.     End Sub
    17.  
    18.     Public Property mystring() As String
    19.         Get
    20.             Return s
    21.         End Get
    22.         Set(ByVal Value As String)
    23.             s = Value
    24.         End Set
    25.     End Property

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    Re: Form sharing problem

    OK, a friend of mine helped me fix this, basically on Form1 I had to change the code to this:

    VB Code:
    1. Dim csvFile As String
    2. Dim Form2 As New Form2
    3. If Form2.ShowDialog() = DialogResult.OK Then
    4.   csvFile = Form2.listItem
    5. End If

    and on Form2 I needed to add

    Me.DialogResult = DialogResult.OK

    just before closing the Form.


    wooooooooooo.

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