简体   繁体   中英

Form_Load() - passing parameter

I am adding a functionality to the code developed by my colleague who has left my employer. I'll try to explain the solution in a simple case -

I have 2 forms A and B.

On A form I take the folder path from the user and I click a button on A.

On button click on Form AI need to pass the path to method M of form B. I made the method M public and have written the following code in button_click of form A.

private void startButton_Click(object sender, EventArgs e)
{
    startButton.Enabled = false;
    pathTextBox.Enabled = false;
    using (Form1 form1 = new Form1())
    {
        // This is what I am trying to do. Initially start() did not had any input parameters, now I have added a single input parameter to it. 
        form1.start(pathTextBox.Text);
    }

    //this.Close();
}

Now, this works except that FormA_Load() is defined like this -

private void FormA_Load(object sender, EventArgs e)
{
    start();
}

The question is how do I pass the pathBox.Text to FormA_Load() as it throws an error

No overload for method 'start' takes 0 arguments

public void start(string selectedPath)
{
    try
    {
        this.Cursor = Cursors.WaitCursor;
        SMSManager smsManager = new SMSManager (selectedPath);
        smsManager .CopyCompletedHandler += new SMSManager .CopyCompleted(smsManager_CopyCompletedHandler);
        smsManager .CopyLogFiles();
    }
    catch (Exception ex)
    {
        WriteLog(ex);

        smsManager _CopyCompletedHandler("Error :" + ex.Message, false);
        this.Cursor = Cursors.Default;
        MessageBox.Show(ex.Message);
    }
}

void smsManager_CopyCompletedHandler(string data, bool isFullyCompleted)
{
    Invoke((MethodInvoker)delegate
    {
        this.Text = "SMS Collector- Copying...";
        txtStatus.AppendText(stepNo + ". " + data + Environment.NewLine + Environment.NewLine);
        txtStatus.ScrollToCaret();
        stepNo++;
        if (isFullyCompleted)
        {
            this.Cursor = Cursors.Default;
            this.Text = "SMS Collector- Completed";
            MessageBox.Show(this, data, "Message", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
        }
    });

}

First change required by your code.
Pass the info in the textbox via the constructor of the called form then SHOWDIALOG the form

private void startButton_Click(object sender, EventArgs e)
{
    startButton.Enabled = false;
    pathTextBox.Enabled = false;
    using (Form1 form1 = new Form1(pathTextBox.Text))
    {
         // ShowDialog is required to stop the execution here 
         // Otherwise the code exits immediately and the using destroys the form1 instance
         form1.ShowDialog();
    }
}

Now in the form called save the passed path in a global variable

public class Form1 
{
     private string _selectedPath = string.Empty;
     public Form1(string path)
     {
        InitializeComponents();
        _selectedPath = path;
     }
     .....
}

Now you could call the initialization of the SMS system in your form load event (or better overridin the OnLoad event.) This is now safe becase in the OnLoad override the controls of the form are fully initialized and ready to be used

protected override void OnLoad(EventArgs e)
{
  // The code here will be executed before the Form.Load event
  start(_selectedPath);
  base.OnLoad(e);
  // The code here will be executed after the Form.Load event
}

You can't, and shouldn't, pass parameters around using event delegates.

There are a few possibilities though:

  1. Pass the data through the constructor;
  2. If the form is already alive and the other screen is reacting on user feedback:
    • use an event handler;
    • call a custom method and pass that value.

To go with the constructor:

public class FormA : Form
{
    public void start()
    {
        FormB b = new FormB(this.textBox.Text);
    }
}

public class FormB : Form
{
    public FormB(string s)
    {
        // use variable s
    }
}

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