0

In my program I have a registration page where a user enters all his details to register to the system. On this page I have a button that when selected should open up a windows form that is located on another project, however when I select this button nothing is happening.

The code behind the windows form application:

 public GenerateToken()
    {
        InitializeComponent();
    }

    public void Main()
    {
        Application.Run(GenerateToken.ActiveForm);
    }

The above code is found in a project on its own. In another project this is the code for the controller that calls this windows form:

 public ActionResult generateToken()
    {
        new TokenGenerator.GenerateToken().Main();
        return RedirectToAction("RegisterPage");
    }

The code in the view where the button is located for this button is:

    <input id="GenToken" type="button" value="generateToken" onclick = "generateToken" />

Is there something that I am doing wrong regarding the button or is the way that I am trying to link them together wrong?

Maeglin77
  • 173
  • 1
  • 14
  • WindowsForms and Web Application are such different concepts. You can not do what you want by this approach. Can you tell more about what exactly you need to happen? – Abolfazl Hosnoddin Apr 16 '14 at 15:49
  • In my mvc project I have a wcf application where I have the views and controllers for the registration process. In view for the registration I must place a button that when selected should pop up a windows form application that would allow the user to enter a password and pin that will then use an encryption method to generate a key code. The encryption process works however I cant seem to get the windows form app to open on button click. – Maeglin77 Apr 16 '14 at 16:57
  • Does every client have this win app on his machine? Will this work as local mechanism or on network? It can be done but it seems that this way is not a proper way to do such things. – Abolfazl Hosnoddin Apr 16 '14 at 17:04
  • yes every client has it and it will work as a local mechanism. I know it is not the proper way however the task has to be done in this manner. – Maeglin77 Apr 16 '14 at 18:30

2 Answers2

0

As you insist on this way, you have to create a communication channel between these two apps. You can use modern technologies for this purpose such as Named Pipes, .NET remoting or WCF hosted on IIS (better one). If you choose Named Pipes or .NET remoting, you have to create windows service project and use it as server application. As shown in this sample, create a host object and register a channel in your windows service solution. You have to define a method in this hosted class as a runner of your windows application. Be noticed that you have to run it as process not by the way you did in your code. Your web app must call that method from server hosted object which will cause your windows app to run. How to start process is explained here. Let me know if you understand what to do.

Community
  • 1
  • 1
  • Where would the code for the creation of the process be entered though? I presume that it would not be entered in the view itself. My problem is that I cant seem to get a button/link to go the the controller in order to access the method leading to the execution of the windows app. Once I manage to do this the button would lead to the actionresult method above that would call the main method above it in order to bring up the windows form? Linking the button as follows given a resource not found error: @Html.ActionLink("Generate Token", "generateToken", "AccountController") – Maeglin77 Apr 17 '14 at 23:02
  • You can not run windows forms app from web application any kind of it! As I said you have to create Windows Service Project and handle running of windows app there. – Abolfazl Hosnoddin Apr 18 '14 at 14:20
0

I managed to solve this by creating a class in my windows application project and linking this class to my main mvc project. The class contained the following method:

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        SomeForm form1 = new SomeForm();
        Application.Run(form1);

By linking this to a button in my main MVC project I was able to load the windows form on button click.

Maeglin77
  • 173
  • 1
  • 14