开发者

Inherit from a windows form

This is homework so for those that dont like looking at someone asking questions about homework dont read this post.

I am to create a console application and a windows form in C#. When I run the console application it suppose to popup a windows form with two buttons开发者_如何学运维 on it Pressme and Exit. The part I am having a hard time with is. How do I do this? I think I need to inherit the windows form into the console application. Help would be great.


Since this is homework - I'll try to point you in the right direction (without completely answering).

I would recommend starting 2 projects, from scratch, to compare. Make a Windows Forms application, and a Console Application, and look at the differences between the two. This should show you what you need to change to add a form to your console application.

In particular, look at the differences in "Main" as well as the project references...


Nope, you don't need inheritance. You simply create a new console application. Right mouse-click on your new project and select Add- > New Item. Select Windows Form. Design your new form in the designer.

Then, in the console code, (supposing that you called the new form Form1) put

Form1 form1 = new Form1();
Form1.ShowDialog();

That should do it for you.

Just because this application runs as a console application doesn't mean that it's not a .net Windows application running managed code just like a Windows Forms application.

Edit, Bonus Fact: The ShowDialog() method will make the console wait until you close the form to end the program. If you use the Show() method, it won't wait. For a good learning exercise, I suggest you learn more about the difference between these two methods.


You can try following pointers.

1- Create a Blank Solution.

2- Add a Console Project .

3- Add a Windows Forms Project.

4- Add Reference of windows forms project in console project.

5- Say you have Form1 in Windows Forms Project.

6- use following code in main function of Console project.

        Form1 f = new Form1();
        f.ShowDialog();


The easy answer is to create a simple WinForms application that does what you need, and then copy all code and generated code to the console application. You still need to add the library references though, otherwise it won't compile.


Answer intentionally a bit vague since this is a homework assignment :)

There seem to be 2 problems that you are struggling with here

  1. How to display a Windows Form from a Console Application
  2. How to access a windows form instance from a Console Application

For the first one the key problem is you need to setup a message loop in the console app to ensure the form is properly responsive. Note that this also must be done in order to run a normal Windows Form application. I would create a WinForm application and dig through the code in the hidden files in order to see just how the main form is created and displayed there (hint: Application is the type you're looking for)

For the second problem the easiest solution is to create a WinForm DLL and reference it from the console application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜