Results 1 to 4 of 4

Thread: Textbox update from other form

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2012
    Posts
    433

    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.)
    Attached Files Attached Files

  2. #2
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    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?

  3. #3
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    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.

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    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:
    1. Public Class MainForm
    2.  
    3.     Public WriteOnly Property UpdateDisplayValue As String
    4.         Set(ByVal value As String)
    5.             If value.Trim <> String.Empty Then
    6.                 Me.PrivateTextBox.Text = value
    7.             End If
    8.         End Set
    9.     End Property
    10.  
    11.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    12.         Dim frm As New UpdateForm(Me)
    13.         frm.ShowDialog()
    14.     End Sub
    15.  
    16. End Class

    The form that updates the textbox looks a little some thing like

    vb Code:
    1. Public Class UpdateForm
    2.  
    3.     Private m_mainForm As MainForm
    4.  
    5.     Public Sub New(ByVal MainForm As MainForm)
    6.         InitializeComponent()
    7.         Me.m_mainForm = MainForm
    8.     End Sub
    9.  
    10.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    11.         Me.Close()
    12.     End Sub
    13.  
    14.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    15.         Me.m_mainForm.UpdateDisplayValue = "Some value"
    16.     End Sub
    17. End Class

    All exe removed

    http://www.freefilehosting.net/windowsapplication1

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