PDA

Click to See Complete Forum and Search --> : [RESOLVED] Maybe an easy question


zdavis
May 9th, 2007, 10:21 AM
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

axion_sa
May 9th, 2007, 11:16 AM
You don't. Rather pass the information you need from the "parent" to the "child" via properties, e.g.:


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;
}
}

zdavis
May 9th, 2007, 02:28 PM
Well basically I am wanting to call the form_load event of one page from another.

ComputerJy
May 9th, 2007, 03:47 PM
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.

zdavis
May 9th, 2007, 04:02 PM
how then can i reload a form from another one?

JenniferBabe
May 9th, 2007, 05:45 PM
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

ComputerJy
May 9th, 2007, 05:50 PM
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

jmcilhinney
May 9th, 2007, 05:55 PM
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.

jmcilhinney
May 9th, 2007, 06:03 PM
It should look like this:

Parent form:Dim record As DataRow 'Get the desired record and store it here.

Using child As New ChildForm(record)
child.ShowDialog()
End UsingChild form:Private data As DataRow

Public Sub New(ByVal data As DataRow)
' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.

Me.data = data

'Get the current values from the record and display them.
Me.TextBox1.Text = data("SomeColumn").ToString()
End Sub

Private Sub okButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles okButton.Click
'Get the new values and update the record.
data("SomeColumn") = Me.TextBox1.Text

'Dismiss the dialogue.
Me.DialogResult = Windows.Forms.DialogResult.OK
End SubAs 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.