|
-
May 9th, 2007, 10:21 AM
#1
Thread Starter
Lively Member
[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
-
May 9th, 2007, 11:16 AM
#2
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;
}
}
-
May 9th, 2007, 02:28 PM
#3
Thread Starter
Lively Member
Re: Maybe an easy question
Well basically I am wanting to call the form_load event of one page from another.
-
May 9th, 2007, 03:47 PM
#4
Re: Maybe an easy question
 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
-
May 9th, 2007, 04:02 PM
#5
Thread Starter
Lively Member
Re: Maybe an easy question
how then can i reload a form from another one?
-
May 9th, 2007, 05:45 PM
#6
Hyperactive Member
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
-
May 9th, 2007, 05:50 PM
#7
Re: Maybe an easy question
 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
-
May 9th, 2007, 05:55 PM
#8
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.
-
May 9th, 2007, 06:03 PM
#9
Re: Maybe an easy question
It should look like this:
Parent form:
VB.NET Code:
Dim record As DataRow 'Get the desired record and store it here.
Using child As New ChildForm(record)
child.ShowDialog()
End Using
Child form:
VB.NET Code:
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 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|