C# - Load Text from Textbox in Form1 into Textbox in Form2

时间:2015-07-28 15:39:08

标签: c# forms textbox

Just a quick one!

I've had to add a second form to my windows form application due to not physically having the space for more textboxes. Some of the textboxes have ended up being the same as on the original form (I know this isn't ideal, but the two forms each write to separate text files, so it works out easier overall)

With this being the case, I'd like the values in the textboxes from the original form to be copied into their duplicate textboxes on the second form (trying to prevent double data entry and reduce risk of errors).

So, I have a button click on the first (Form1) form that calls the .Show() function to load a new version of the second form (PreAnaestheticChecklist).

    public void btnPreOpChecklist_Click(object sender, EventArgs e)
    {
        //create secondary form for pre-anaesthetic checklist
        PreAnaestheticChecklist checklistForm = new PreAnaestheticChecklist();

        //load pre-anaesthetic checklist form to screen
        checklistForm.Show();
    }

This works fine, and the form loads up as blank. I wrote a load of small string functions that return strings that are comprised of the text in the textboxes from form1. These are called in the PreAnaestheticChecklist_Load event. An example is shown below using one of the transfers as an example.

    public string getProcedure()
    {
        //load value from textbox in IOconsole
        string proc = main.txtProcedure.Text;
        //return this to textbox on Checklist
        return proc;
    }

    public void PreAnaestheticChecklist_Load(object sender, EventArgs e)
    {
        //load any values already on main form into respective textboxes
        txtProcName.Text = getProcedure();
        txtPlannedProc.Text = getProcedure();
    }

This is done for another few textboxes, but even with all this, the second form loads as blank.

I read up and was advised to try putting all of the textbox assignments from the _Load event into the button click event that loads form2, and still nothing. I also changed the Modifiers property for all forms envolved to 'Public', and still nothing!

Not sure where to look next, so any help with the matter is very much appreciated!

Thanks in advance, Mark

1 个答案:

答案 0 :(得分:2)

Pass in Form1 as the Owner when you call Show():

public void btnPreOpChecklist_Click(object sender, EventArgs e)
{
    //create secondary form for pre-anaesthetic checklist
    PreAnaestheticChecklist checklistForm = new PreAnaestheticChecklist();

    //load pre-anaesthetic checklist form to screen
    checklistForm.Show(this); // <-- passing in the Owner
}

Now, in the Load() event of your PreAnaestheticChecklist Form, cast the .Owner property to Form1 and store it in your "main" variable:

public void PreAnaestheticChecklist_Load(object sender, EventArgs e)
{
    this.main = (Form1)this.Owner;

    //load any values already on main form into respective textboxes
    txtProcName.Text = getProcedure();
    txtPlannedProc.Text = getProcedure();
}