M. Antoney
M. Antoney

Reputation: 88

Passing Values among Winforms in C#.net

I am trying to obtain a value back to the parent form and below is the code that I was using for this and it was working fine until started loading the child-form in a panel control to avoid popup windows.

Code in the mainform which contains the Panel

MainMovement child = new MainMovement(new_dat, required_time, number);
child.TopLevel = false;
this.pnlmain.Controls.Add(child);
child.Show();
child.BringToFront();
///Obtaining value back from the child form
string updatingc = child.updatestatus; //This is not working, I am proceeding with some more functions depending on this value, but code does not work here after

Child form has a public value as updatestatus and it sets the value before closing the child form.

Please advise how to obtain this value. I believe it is something to do with changing child.ShowDialog() to child.Show(). (In order to load the form into a panel I had to change this, before that this was working fine).

Upvotes: 2

Views: 253

Answers (2)

jparthj
jparthj

Reputation: 1646

You can pass the object of your main form into child form via constructor. If you pass your object, you will have access to all the methods of parent form in your child. You can call any public method of main class to update your value.

MainMovement child = new MainMovement(this,new_dat, required_time, number);
child.TopLevel = false;
this.pnlmain.Controls.Add(child);
child.ShowDialog();
child.BringToFront();

Put one public method in your main form,

Public void UpdateValue(String pString)
{
      // Update your value
}

In your child form, you have to catch "this" with global object.

private oMainForm as MainForm

public void MainMovement(MainForm pObject,String new_dat, String required_time, Int number)
{
     oMainForm = pObject; 
     // Your Code
}

Now you can simply call your 'UpdateValue' method from child form.

oMainForm.UpdateValue("Updated String");

Upvotes: 2

psubsee2003
psubsee2003

Reputation: 8751

The problem is .ShowDialog() waits for a DialogResult before continuing, whereas Show() just shows the form and continues. It is hard to say without knowing how your child form is working, but my guess is whatever updates or sets updatestatus in your child form doesn't update before your code reaches that line.

One possible solution involves a major refactoring of your code. You can add an event to your MainMovement form that is triggered when updatestatus is changed.
Note that I changed your updatestatus to UpdateStatus and turned it into a property

public MainMovement : Form
{
    public event EventHandler Updated;
    private void OnUpdateStatus()
    {
        if (Updated != null)
        {
            Updated(this, new EventArgs());
        }
    }

    private String updatestatus;
    public String UpdateStatus
    {
        get { return updatestatus; }
        private set 
        {
            updatestatus = value;
            OnUpdateStatus();
        }
    }

    // rest of your child form code 
}

public ParentForm : Form
{
    public void MethodInYourExample()
    {
        // other code?
        MainMovement child = new MainMovement(new_dat, required_time, number);
        child.Updated += ChildUpdated;
        child.TopLevel = false;
        this.pnlmain.Controls.Add(child);
        child.Show();
        child.BringToFront();
    }

    void ChildUpdated(object sender, EventArgs e)
    {
        var child = sender as MainMovement;
        string updatingc = child.UpdateStatus;
        //rest of your code
    }

}

Upvotes: 1

Related Questions