开发者

passing something from child form to parent

so i have this form and on it is a combo box populated from a database via a SQL method.

and i have another form which allows us to maintain the database table etc.

so i make a new instance of the second form doing:

Form1 frm = new Form2;
frm.show();

once i have done what ever i wanted to do on the second form and i close it, i need to somehow trigger an event or something which will refresh the combo box and the code behind it.

i was thinking of some onchange or focus event for the开发者_运维技巧 whole form, the problem is i have 5 of these combo boxes and running all the SQL again.

i also thought of passing somesort of variable thro but then i would still need an event for that.

any ideals would be awesome


I think you had your answer in the question... Use an event / handler to refresh.

E.g.

public class Form2 : Form
{
    public event EventHandler DbChanged;

    protected virtual void OnDbChanged()
    {
        ... // Raise event
    }

    // On OK button/FormClosing/Closed whatever, be sure to call OnDbChanging
}

Then in your Form1 code

var form2 = new Form2();
form2.DbChanged += new EventHandler(Form2_DbChanged); // Add method to handle change and update the appropriate combo box
form2.Show();


If you've assigned these forms with the first form owning the second, like this:

Form2 frm2 = new Form2();
//assuming that you're launching this form from within the first form
frm2.Owner = this;

then you can get a reference to the first form through the Owner property, and thus call methods on it.

Form2_FormClosed(object o, FormClosedEventArgs e)
{
    this.Owner.updateComboBox();
}

Note that you'll need the FormClosing event if you want to send data from the form's controls back, though.

Note that the Owner property has some other special characteristics. Notably, the child form will remain showing (on top) when the parent form is selected.


You can use delegate and event here.

Your parent class will create an object of child class and will also subscribe to the event if child class.

Whenever child class need to pass something/ signal parent class, it will raise an event. As parent has subscribed to these event, it it will get that data and do the required operation.

Hope this helps you.


Forms are classes. Like any other class, they can have properties and events.

Your Form2 can expose a "DatabaseChanged" event. Form1 or any other form that cares can subscribe to that event. When the database changes, Form2 can fire that event, Form1 will see it and update the combo box.


Lots of options, but a simple one is passing the main form as a reference to the second form.

Form2 frm = new Form2( this ); 
frm.Show();

Then when Form2 finishes its work, it can call some method on the main form to update

public class Form2 : Form 
{
    public Form2( Form1 form1 )
    { 
        this.form1 = form1;
    }

    /// ...
    public void Work()
    {
        // ...
        form1.Update( someData );
    }
}

Not necessarily ideal from a maintenance perspective, but workable for small apps.


Suppose your combo name id cmb1 and

cmb1.DataSource = ds1;

and you need to call a new window do some work there & whenever you close that window your parent will refresh or cmb1 will have the latest data.

from you parent window call your chlid window like this

if (NameOfWindow.RequestAction(ref ds1)) // let RequestAction is a method
{
       ///refresh your data source of combo                    
}

and in the child window the method should be like this:

public static bool RequestAction(ref ds1)
        {
            NameOfWindow frm = new NameOfWindow();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                //do what ever you want to do and update the ds1               
                return true;
            }
            else
            {
                return false;
            }                     

        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜