Residualfail
Residualfail

Reputation: 77

send argument from one winform application to another and populate a textbox

I have 2 separate applications with Winforms. Form1 has a textbox and a button. When I click the button, I want to pass the textbox.text to my second application as a command line argument, then populate another textbox on Form2.

I can currently pass the arguments from one application to another just fine, however, I'm not sure how to populate the textbox in my Form2 with the argument from Form1.

Form1:

    private void bMassCopy_Click(object sender, EventArgs e)
    {
        string shortcutName = string.Concat(Environment.GetFolderPath(Environment.SpecialFolder.Programs), "\\", "MassFileCopy", "\\", "MassFileCopy", ".appref-ms");
        Process.Start(shortcutName, " " + tHostname.Text.ToString());
    }

Form2:

    [STAThread]
    static void Main(string[] args)
    {

        try
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            formMain mainForm = new formMain();
            Application.Run(mainForm);

            if (args.Length > 0)
            {
                foreach (string str in args)
                {
                    mainForm.tWSID.Text = str;
                }
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {                


        }
    }

The code above accepts the arguments just fine, however, the application loads and the textbox isn't populated. A breakpoint seemed to show that the foreach loop doesn't run until after Form2 closes but I can't run the foreach loop prior to Form2 loading because there isn't a textbox to interact with.

Any ideas?

Upvotes: 2

Views: 1127

Answers (2)

StevieB
StevieB

Reputation: 1000

You can handle the Form's Load event and populate the TextBox as so:

private void formMain_Load(object sender, EventArgs e)
{
    if (Environment.GetCommandLineArgs().Length > 1)
    {
        // The first command line argument is the application path
        // The second command line argument is the first argument passed to the application
        tWSID.Text = Environment.GetCommandLineArgs()[1];
    }
}

The first argument in the command line arguments is the name of the Form application, hence the reason we use the second command line argument - i.e. pass 1 to the indexer - in the code above.

Upvotes: 0

Steve
Steve

Reputation: 216293

Your code that tries to populate the textbox runs only after the closing of the Form2 because Application.Run is modal and doesn't return until the form is closed.
However you could change the constructor of your Form2 to accept a string as a parameter

public class Form2 : Form
{
    public void Form2(string textFromOtherApp)
    {
         InitializeComponent();

         // Change the text only after the InitializeComponent
         tWSID.Text = textFromOtherApp;
    }
}

and pass it from the Main method

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
string initValue = (args != null && args.Length > 0 ? args[0] : string.Empty);
formMain mainForm = new formMain(initValue);
Application.Run(mainForm);

Upvotes: 1

Related Questions