1 Attachment(s)
Textbox update from other form
Here is 3 forms, form3, form1, form2.
form1 has textbox.
A. Start form3, form3 load form1, form1 load form2 and form2 try updating form1.textbox but failed.
B. But start form1, form1 load form2 and form2 try updating form1.textbox and then textbox is normally updated.
I don't know why B succeed but A fail.
How do I do to succeed in A?
(I attached project to show this.)
Re: Textbox update from other form
I havent looked at your project but you can't load something from something that has not been created yet until you load form1 with the textbox it doesnt exist
So if i understand correctly in scenario A you are trying to load form 3 first, then form2 and have form2 get information form form1 textbox?
You have to load form 1 first
perhaps a more senior programmer will correct me or offer a better idea but i beleive that is your problem
Why do you want to load something from a textbox that hasnt been created yet. in theory the user wouldnt have put any information in the textbox yet, so you must have a default value in the textbox, why not just use that value when you load form2?
Re: Textbox update from other form
Because you are creating new instances(as you should using New keyword)
Code:
Dim frm As New Form1 frm.ShowDialog()
then trying to update the default instance of form1
Code:
Form1.TextBox1.Text = "AAAA"
Read Johns excellent tutorial on http://jmcilhinney.blogspot.co.uk/ Managing Data Among Multiple Forms (Part 2)
How ever it should be noted as mentioned it's a lazy approach. The controls Modifier should be private. If access is needed we should handle this correctly.
Re: Textbox update from other form
Your form that updates the textbox should look a little some thing like
TextBox is private not friend.
vb Code:
Public Class MainForm
Public WriteOnly Property UpdateDisplayValue As String
Set(ByVal value As String)
If value.Trim <> String.Empty Then
Me.PrivateTextBox.Text = value
End If
End Set
End Property
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm As New UpdateForm(Me)
frm.ShowDialog()
End Sub
End Class
The form that updates the textbox looks a little some thing like
vb Code:
Public Class UpdateForm
Private m_mainForm As MainForm
Public Sub New(ByVal MainForm As MainForm)
InitializeComponent()
Me.m_mainForm = MainForm
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.m_mainForm.UpdateDisplayValue = "Some value"
End Sub
End Class
All exe removed
http://www.freefilehosting.net/windowsapplication1