简体   繁体   中英

User Control in a winform

I have 2 user controls inside panel1 and panel2 on a winform and the form has next page | previous page and close buttons. The close hides the winform and I want to save the user controls inputed data to an object that I can use later to save to a database on another form behind the forms that popup. So MainForm opens a form that opens the form with the 2 user controls. I'm trying to save the data to an object with the close like this:

private void btnClose_Click(object sender, EventArgs e)
    {
        UC1 ui1 = new UC1();
        UC2 ui2 = new UC2();
        ui1.SetPage1();
        ui2.SetPage2();

        this.Close();
    }

And in user control 1 i have SetPage1 and user control 2 has SetPage2 that look like:

public void SetPage1()
    {
        UIModel1 uiModel1 = new UIModel1();
        uiModel1.StoppedDate = txtStoppedDate.Text;
        uiModel1.StoppedTime = txtStoppedTime.Text;
        uiModel1.ArrivedNo = txtArrivedNo.Text;
        //etc

This isn't working and I'm not sure why it isn't. I put a MessageBox inside the SetPage1 for the txtArrivedNo.Text and it's empty. Any help would be appreciated.

I assume that UIModel1 is a model to store the control data. You create it locally in SetPage1 function which is wrong. You need to make this model as a member of your MainForm class and pass it to SetPage1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM