-1

I need to be able to run a Windows Forms function or event (for example a button click) from a command line. How could I do this?

I can't use a console or windows service due to certain constraints.

Justin
  • 80,106
  • 47
  • 208
  • 350
Varun
  • 406
  • 3
  • 7
  • 18
  • 2
    What exactly do you mean with *windows form function*? – sloth Aug 20 '12 at 10:26
  • Guess he means manual raising certain events of UI controls on his WinForms app from cmd? – Oleg Aug 20 '12 at 10:32
  • I guess you could create a new program with the code you want to execute in the `Main` function. Would that work? – Default Aug 20 '12 at 10:35
  • I have window form, I need to run the form using command file. Is there any way. That is my question...? – Varun Aug 20 '12 at 10:36
  • well, a form is part of the *program*. You can start the program from a command file. A program always starts executing in the `Main` function. There's no way to start execution in another part of the code. Not to my knowledge anyway. – Default Aug 20 '12 at 10:39
  • I can keep wahtever I need to do in one fy – Varun Aug 20 '12 at 11:24

2 Answers2

0

If you mean re-use a method in your Windows Forms application in a command line (Console) application, then simply reference the assembly containing the class and call that method, a bit like this

static void Main()
{
    Form1 form = new Form1();
    form.SomeButton_Click();
}

Note that for this to work the SomeButton_Click method and the Form1 class must be public. If this is what you want to do then this is a good indication that the logic contained in SomeButton_Click should be refactored to some common utility class.

If alternatively you want to simulate a button click in a running Windows Forms application then you have a couple of options

There are many different ways to achieve either of these however both are more complex than just calling a method - it would help if you provided more detail on your problem.

Community
  • 1
  • 1
Justin
  • 80,106
  • 47
  • 208
  • 350
0

You may be able to do this with a Powershell script: UI Automation with Windows PowerShell

Patrick Cuff
  • 26,370
  • 11
  • 63
  • 93