Results 1 to 9 of 9

Thread: [RESOLVED] Maybe an easy question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    122

    Resolved [RESOLVED] Maybe an easy question

    I have a parent child form setup. I call one form from another to input data into. How can i update the data on the other form. Basically how do you call the form_load() of an open form from another form?

    thanks

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: Maybe an easy question

    You don't. Rather pass the information you need from the "parent" to the "child" via properties, e.g.:

    Code:
    public class ParentForm() {
    	private void OpenChild(string param1, string param1) {
    		ChildForm child = new ChildForm();
    		child.Param1 = param1;
    		child.Param2 = param2;
    	}
    }
    
    public class ChildForm() {
    	public string Param1 {
    		//TODO: Complete functionality.
    		get {}
    		set {}
    	}
    
    	public string Param2 {
    		get {}
    		set {}
    	}
    
    	private void UpdateInterface() {
    		txtParam1.Text = this.Param1;
    		txtParam2.Text = this.Param2;
    	}
    }

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    122

    Re: Maybe an easy question

    Well basically I am wanting to call the form_load event of one page from another.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Maybe an easy question

    Quote Originally Posted by zdavis
    Well basically I am wanting to call the form_load event of one page from another.
    You can't do that.
    Because Events, event handlers in .NET are private.
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    122

    Re: Maybe an easy question

    how then can i reload a form from another one?

  6. #6
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: Maybe an easy question

    Why would you want to do that in the first place? You won't be able to refresh the page like a web page would.

    If lets say you have mainform, subform, and subform updates lets say something in the database, and the mainform has a listbox displaying values from the database, and you want to have these values refreshed which is contained in the load method, then you could actually call it by passing dummy objects as parameters e.g.

    Mainform:

    public void Form1_Load(object sender, System.EventArgs e)
    {
    // clear listbox
    //populate list box.

    }


    Subform:

    calls load method as:

    Form1_load(Type.missing, Type.missing)
    {

    }



    I'm not sure if this will work, if it doesnt, then just create an empty method that does the same thing Form1_load does and simply call it.

    Hope this helps.

    Jennifer

  7. #7
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Maybe an easy question

    Quote Originally Posted by JenniferBabe
    I'm not sure if this will work, if it doesnt, then just create an empty method that does the same thing Form1_load does and simply call it.
    Actually, that's the best thing to do in his case
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Maybe an easy question

    The parent form has a whole bunch of records. If you want to edit a record you simply pass that record to the child form. The child form gets the values from the record and displays them. The user will then edit them. If the user presses the OK button then the child form gets the new values from the controls and assigns them to the fields of the record. That's it. The parent form never has to even know what happened.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Maybe an easy question

    It should look like this:

    Parent form:
    VB.NET Code:
    1. Dim record As DataRow 'Get the desired record and store it here.
    2.  
    3. Using child As New ChildForm(record)
    4.     child.ShowDialog()
    5. End Using
    Child form:
    VB.NET Code:
    1. Private data As DataRow
    2.  
    3. Public Sub New(ByVal data As DataRow)
    4.     ' This call is required by the Windows Form Designer.
    5.     InitializeComponent()
    6.  
    7.     ' Add any initialization after the InitializeComponent() call.
    8.  
    9.     Me.data = data
    10.  
    11.     'Get the current values from the record and display them.
    12.     Me.TextBox1.Text = data("SomeColumn").ToString()
    13. End Sub
    14.  
    15. Private Sub okButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles okButton.Click
    16.     'Get the new values and update the record.
    17.     data("SomeColumn") = Me.TextBox1.Text
    18.  
    19.     'Dismiss the dialogue.
    20.     Me.DialogResult = Windows.Forms.DialogResult.OK
    21. End Sub
    As I said before, the parent form gets the record and passes it to the child form, which gets the field values and displays them. If the user presses OK the child form gets the new values and updates the record. The parent form doesn't have to do anything because if the row has changed data-binding will take care of updating the UI.

    EDIT: Ah, b*gger. I forgot we're in C# land. Hopefully the idea is clear from my code but if you want a C# version then just ask.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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