Pass value from One form to another
-
Hi i have a form1 with one textbox1 and button1 when i click the button another form2 loads which has one textbox1 and button one when i type alfanumeric values in the textbox of form2 and press the button1 of form2 it is not displaying the value in form1 textbox1. I am using property approach. My code is as: Form One:
Public Class Form1
Public \_form2 As Form2 = New Form2 Private Sub btnList\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click \_form2.Show() End Sub Public WriteOnly Property \_textBox() As String Set(ByVal Value As String) tbxVal.Text = Value End Set End Property
End Class
Form Two
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim frm As Form1 = New Form1 Value = \_textBox1 Me.Close() End Sub Public ReadOnly Property \_textBox1() As String Get Return tbxValue1.Text End Get End Property Private Sub Form2\_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Dim fm As Form1 = New Form1 fm.tbxVal.Text = Value End Sub
-
Hi i have a form1 with one textbox1 and button1 when i click the button another form2 loads which has one textbox1 and button one when i type alfanumeric values in the textbox of form2 and press the button1 of form2 it is not displaying the value in form1 textbox1. I am using property approach. My code is as: Form One:
Public Class Form1
Public \_form2 As Form2 = New Form2 Private Sub btnList\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click \_form2.Show() End Sub Public WriteOnly Property \_textBox() As String Set(ByVal Value As String) tbxVal.Text = Value End Set End Property
End Class
Form Two
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim frm As Form1 = New Form1 Value = \_textBox1 Me.Close() End Sub Public ReadOnly Property \_textBox1() As String Get Return tbxValue1.Text End Get End Property Private Sub Form2\_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Dim fm As Form1 = New Form1 fm.tbxVal.Text = Value End Sub
.NetDeveloper09 wrote:
Dim frm As Form1 = New Form1
You are actually creating a new instance of form1 here. This instance is not aware of the value you set in the first instance. Create a property in form2 and then set it in the form1 btnList_Click. You should then be able to get your textbox value in form2.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick - Silverlight *.XCP files. Visit the Hindi forum here.
-
.NetDeveloper09 wrote:
Dim frm As Form1 = New Form1
You are actually creating a new instance of form1 here. This instance is not aware of the value you set in the first instance. Create a property in form2 and then set it in the form1 btnList_Click. You should then be able to get your textbox value in form2.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick - Silverlight *.XCP files. Visit the Hindi forum here.
hi i under stand, some one has replied to me which i believe is the answer but i have forgot how to do it. "You need to retrieve the contents of the form2 TextBox control before form2 is disposed. To do that, you have to handle the Closing event for form2 inside form1. In that event handler, you simply use your property to retrieve the contents of the Form2.TextBox, and do whatever you need to with it."
-
.NetDeveloper09 wrote:
Dim frm As Form1 = New Form1
You are actually creating a new instance of form1 here. This instance is not aware of the value you set in the first instance. Create a property in form2 and then set it in the form1 btnList_Click. You should then be able to get your textbox value in form2.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick - Silverlight *.XCP files. Visit the Hindi forum here.
I tried your way its not working, any other suggestion.
-
I tried your way its not working, any other suggestion.
:doh: ... you are allocating ("New") another instance in your Ok and Closed-Handler - this is a simple mistake. Your code smells like "beginner-style" maybe you want to learn about "variable scope"? Solution: 1. Make a public property for Form2 which will give you the textbox.Text (you did that) 2. Read the property after showing the Form -> Simple, isn't it? 3. Sit down and read a book about programming before you do further coding... (variable scope is always a thing you need to know, in any language)
Public Class Form1
Public \_form2 As Form2 = New Form2 Private Sub btnList\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click \_form2.Show() tbxVal.Text = \_form2.textBox1 End Sub Public WriteOnly Property \_textBox() As String Set(ByVal Value As String) tbxVal.Text = Value End Set End Property
End Class
-
Hi i have a form1 with one textbox1 and button1 when i click the button another form2 loads which has one textbox1 and button one when i type alfanumeric values in the textbox of form2 and press the button1 of form2 it is not displaying the value in form1 textbox1. I am using property approach. My code is as: Form One:
Public Class Form1
Public \_form2 As Form2 = New Form2 Private Sub btnList\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click \_form2.Show() End Sub Public WriteOnly Property \_textBox() As String Set(ByVal Value As String) tbxVal.Text = Value End Set End Property
End Class
Form Two
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim frm As Form1 = New Form1 Value = \_textBox1 Me.Close() End Sub Public ReadOnly Property \_textBox1() As String Get Return tbxValue1.Text End Get End Property Private Sub Form2\_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Dim fm As Form1 = New Form1 fm.tbxVal.Text = Value End Sub
This is a really common problem, and it's almost always answered incorrectly. Never, ever, attempt to use the instance scope to control the passing backwards and forwards of data between two modeless forms. Look to use delegate/events instead, and have the first form subscribe to an event on the second form which will be fired when it needs to return the data.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
-
:doh: ... you are allocating ("New") another instance in your Ok and Closed-Handler - this is a simple mistake. Your code smells like "beginner-style" maybe you want to learn about "variable scope"? Solution: 1. Make a public property for Form2 which will give you the textbox.Text (you did that) 2. Read the property after showing the Form -> Simple, isn't it? 3. Sit down and read a book about programming before you do further coding... (variable scope is always a thing you need to know, in any language)
Public Class Form1
Public \_form2 As Form2 = New Form2 Private Sub btnList\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click \_form2.Show() tbxVal.Text = \_form2.textBox1 End Sub Public WriteOnly Property \_textBox() As String Set(ByVal Value As String) tbxVal.Text = Value End Set End Property
End Class
Knowledge has no boundaries any one who struggles for it he gains the knowledge, one should not underestimate the person on the other side and should not pass remarks that hurt other poeple or discourage people. You should show respect to people who are in your type of field. If you donot like to answer any question you are free to do that. For your information i have already done it without your support. Kind Regards Mirza
-
Knowledge has no boundaries any one who struggles for it he gains the knowledge, one should not underestimate the person on the other side and should not pass remarks that hurt other poeple or discourage people. You should show respect to people who are in your type of field. If you donot like to answer any question you are free to do that. For your information i have already done it without your support. Kind Regards Mirza
I suggest you take the advice given to you, even when at first sight you don't like it much. As for your problem, there is at least a dozen ways to "solve" it, unfortunately only one or two of these ways are decent, all others are bad, i.e. they work in a simple situation and will bite you when things become more complex. It takes time and experience to learn it all, and most of all an open mind. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
I suggest you take the advice given to you, even when at first sight you don't like it much. As for your problem, there is at least a dozen ways to "solve" it, unfortunately only one or two of these ways are decent, all others are bad, i.e. they work in a simple situation and will bite you when things become more complex. It takes time and experience to learn it all, and most of all an open mind. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
<Thank you for the advice, i always appriciate good advice, you are right there are and its true also time is the only teacher, and most of all with an open mind is true and will to strugle in life and always give your thoughts a possitive direction, if one do that ones mind will always be open. >