tags:

views:

192

answers:

3

I'm not entirely sure this is possible.

I have a legacy GUI application written in C#. Using AttachConsole(-1) (from kernel32.dll) I can add a console window if it is called from the command line. I've even been able to write a module to run through basic use cases and make it look as if it is a console application. I don't show the window, and if I do it doesn't update or work properly (because I didn't use Application.Run)

However, another person in my team wants to do something similar where input arguments will set off a series of GUI actions (like buttonExample.PerformClick(), update text boxes, etc) to simulate a user. Is it possible to do this using C# entirely or will he have to use something like AutoIt?

We're using VS2008/.NET Framework v3.5.

+3  A: 

You can add string array args to your static void Main() and pass them up to the GUI class to perform such actions.

  static class Program
  {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
      ...
      //TODO: Stuff with your args
    }
  }
Quintin Robinson
Of course. I didn't even clue in that you could add arguments to the main class being run as an application. I was stuck thinking that it had to be void with no arguments.
cgyDeveloper
Yeah sometimes it's very easy to overlook.
Quintin Robinson
A: 

The answer to your question is 'sort-of'... You can do some things but for the most part you'll need to expose a lot of detail from your form. Here is a simple example:

class Program
{
 public static void Main(string[] args)
 {
  Form2 frm = new Form2();
  if( args.Length >= 2 && args[0] == "something" )
  {
   frm.txtTextArea.Text = args[1];
   frm.btnOk_OnClick(frm.btnOk, EventArgs.Empty);
  }
 }
}

public class Form2 : Form
{
 public TextBox txtTextArea;
 public Button btnOk;

 public void btnOk_OnClick(object sender, EventArgs e)
 {
  //... code ...
 }
}

I really, really, really, do not recommend doing this. You need to refactor the form's implementation and get your code out of the form into a business logic class.

csharptest.net
You could also transform and pass the pertinent arguments up to the form object and let it handle the data as well, which would likely be preferred to exposing the form controls publicly.
Quintin Robinson
A: 

You can use the SendInput function from user32.dll

(Of course, this should only be done if changing the GUI application is not possible.)

erikkallen