Download the text from the text box to Form1 when you call the function on Form2

advertisements

Background/Context

So I am building a program that take 2 Excel Files and compares them highlight the differences. This is working fine. Now i am developing a second form, which does a very similar thing but essentially "Applies" the changes. Now in the first form I have two text boxes which contain the file locations, however on the second form which appears after the changes are highlighted there is only an Apply button hence I need to pull down the text box path for the file from Form1 however this doesn't seem to work in mt code:

CODE

public partial class Form2 : Form
{
    Form1 form1 = new Form1();

    public Form2()
    {
        InitializeComponent();
        btnApply1.Click += new EventHandler(this.btnApply_Click);
        btnCancel1.Click += new EventHandler(this.btnCancel1_Click);
    }

    private void btnApply_Click(object sender, EventArgs e)
    {
        foreach (Process clsProcess in Process.GetProcesses())
        {
            if (clsProcess.ProcessName.Equals("EXCEL"))
            {
                clsProcess.Kill();
                break;
            }
        }

        new CRCCompareWorksheets.CompareHelper().ApplyChanges(
            form1.ExcelPath1.Text, form1.ExcelPath2.Text, "CRC");
    }

    private void btnCancel1_Click(object sender, EventArgs e)
    {
        new CRCCompareWorksheets.CompareHelper().CancelApplication();
    }
}

The Problem

So at the line where i call the function for Applying the changes, the variables form1.ExcelPath1.Text and form1.ExcelPath1.Text are both blank hence the file locations are not being pulled through and nothing works :(


The problem here is, that you create a new Instance of Form1. I guess, that Form2 is opened by Form1. In that case I would provide a reference of the calling form to the newly generated Form2. This could look something like the following:

Form2

public partial class Form2 : Form
{

    Form1 form1 = null;

    public Form2(Form1 form1)
    {
        InitializeComponent();
        this.form1 = form1;
        btnApply1.Click += new EventHandler(this.btnApply_Click);
        btnCancel1.Click += new EventHandler(this.btnCancel1_Click);
    }

    private void btnApply_Click(object sender, EventArgs e)
    {
        foreach (Process clsProcess in Process.GetProcesses())
        {
            if (clsProcess.ProcessName.Equals("EXCEL"))
            {
            clsProcess.Kill();
            break;
            }
        }

        new CRCCompareWorksheets.CompareHelper().ApplyChanges(form1.ExcelPath1.Text, form1.ExcelPath2.Text, "CRC");
    }
    private void btnCancel1_Click(object sender, EventArgs e)
    {
        new CRCCompareWorksheets.CompareHelper().CancelApplication();
    }
}

In Form1 you would then need to change the calling of Form2 to something like this:

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

Additionally it would be good practice to create properties for the values you want to read from the TextBoxes instead of making the controls public:

// Properties in Form1
public string ExcelPath1Text
{
    get
    {
        return this.ExcelPath1.Text;
    }

    set
    {
        this.ExcelPath1.Text = value;
    }
}

public string ExcelPath2Text
{
    get
    {
        return this.ExcelPath2.Text;
    }

    set
    {
        this.ExcelPath2.Text = value;
    }
}

And then use the properties in Form2:

CRCCompareWorksheets.CompareHelper().ApplyChanges(form1.ExcelPath1Text, form1.ExcelPath2Text, "CRC");