|
-
Jul 28th, 2005, 06:30 AM
#1
Thread Starter
Lively Member
[RESOLVED] Form sharing problem
Form2 is not passing over information to Form1...
Code for Form2:
VB Code:
Private m_myVar As String
Public Property listItem() As String
Get
Return Me.m_myVar
End Get
Set(ByVal Value As String)
Me.m_myVar = Value
End Set
End Property
Private Sub Button1_Click(ByVal sender as System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lv As ListViewItem = ListView1.SelectedItems(0)
Me.m_myVar = lv.SubItems(2).Text
Me.listItem = lv.SubItems(2).Text
Me.Close
End Sub
And for Form1 I have this:
VB Code:
Dim myForm As New Form2
myForm.ShowDialogue()
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.
-
Jul 28th, 2005, 09:34 AM
#2
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
 
-
Jul 28th, 2005, 09:45 AM
#3
Thread Starter
Lively Member
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.
-
Jul 28th, 2005, 09:52 AM
#4
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
 
-
Jul 28th, 2005, 10:05 AM
#5
Thread Starter
Lively Member
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.
-
Jul 29th, 2005, 03:27 AM
#6
Thread Starter
Lively Member
Re: Form sharing problem
Anybody?
-
Jul 29th, 2005, 03:48 AM
#7
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:
'in your form2
Shared s As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
mystring() = TextBox1.Text
Dim f As New Form1()
f.ShowDialog()
End Sub
Public Shared Property mystring() As String
Get
Return s
End Get
Set(ByVal Value As String)
s = Value
End Set
End Property
'in form1
'no need to instantiate the form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show(Form2.mystring())
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.
-
Jul 29th, 2005, 03:58 AM
#8
Thread Starter
Lively Member
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?
-
Jul 29th, 2005, 04:03 AM
#9
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:
'in your form1
Dim f As New Form2()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
f.ShowDialog()
MessageBox.Show(f.mystring)
End Sub
'in your form2
Dim s As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
mystring() = TextBox1.Text
End Sub
Public Property mystring() As String
Get
Return s
End Get
Set(ByVal Value As String)
s = Value
End Set
End Property
-
Jul 29th, 2005, 04:21 AM
#10
Thread Starter
Lively Member
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:
Dim csvFile As String
Dim Form2 As New Form2
If Form2.ShowDialog() = DialogResult.OK Then
csvFile = Form2.listItem
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|