将我的代码包含到 Windows 窗体应用程序中
主要问题如下:我在控制台应用程序中编写了代码,但我不知道如何将我的代码包含到 WinForms 应用程序中。
我正在使用 Visual studio 2008。
我需要知道应该将代码粘贴到哪里。 谢谢。
The main problem is as follows: I did my code in console application and I don't know how can I include my code to WinForms application.
I am using Visual studio 2008.
I need to know where I should paste my code. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要向 Thaier 提供更多信息。
您当前编写的代码到底是做什么的。 例如,如果您的控制台应用程序使用
Console.WriteLine();
写出文本,您可能需要更改它以写入 GUI 版本中的文本框或列表框。同样,控制台应用程序中是否有任何用户输入,或者命令行参数(字符串 [] args)是否传递给 main 方法。
如果是这样,您可能想要更改 WinForms 应用程序接受此用户输入的方式。
如果您只有一个需要在应用程序启动时立即运行的代码片段,请查看
Form.Loaded
事件,该事件将在应用程序启动后立即执行您的代码。You'll need to provide a lot more information Thaier.
What exactly does you code currently do. For example, if your console application writes out text using,
Console.WriteLine();
you'll probably want to change that to write to a textbox or listbox in the GUI version.Like wise, is there any user input in the console application, or command line arguments (string [] args) being passed to the main method.
If so you'll probably want to change the way this user input is accepted in your WinForms app.
If you just have a snippet that you need to run immediately when the application launches, look at the
Form.Loaded
event which will execute your code as soon as the application starts.转到项目属性并将输出类型从控制台应用程序更改为 Windows 应用程序。 您还需要查看 Eoin Campbell 提到的内容,并确保选择了正确的启动对象。
如果我正确理解你的问题,这应该是你需要做的。
Go to project properties and change the output type from Console Application to Windows Application. You'll also want to look at the things Eoin Campbell mentioned, and make sure you have the right startup object selected.
If I'm understanding your question correctly, this should be all you need to do.
您可以将这两个项目添加到 VS 中的单个解决方案中。 然后,只需从 Winforms 项目引用控制台项目即可。 这样,您的控制台和 Winforms 项目都可以共享代码。
您还可以将代码从控制台项目复制到 Winforms 项目。 您可以简单地在 Visual Studio 中复制并粘贴,或者右键单击解决方案资源管理器中的项目,选择“添加”,然后选择“现有项目”。 导航到选择器中的 .vb 或 .cs 文件,然后选择旧文件。
如果您要查找“代码”在 winforms 应用程序中的位置,只需双击表单上的空白区域即可。 “代码隐藏文件”将会出现。 Form.Loaded 事件处理程序几乎等同于控制台应用程序中的 main 方法。
You can add both projects to a single solution in VS. Then, simply reference your console project from your Winforms project. This way, both your console and your Winforms project can share code.
You could also copy your code to your Winforms project from the console project. You can simply copy and paste within visual stuio, or right click on the project in the Solution Explorer, select Add, then select Existing Item. Navigate to the .vb or .cs file in the selector and pick your old files.
If you're looking for where the "code" is in a winforms application, just double click a blank space on the form. The "code-behind file" will come up. The Form.Loaded event handler is almost equivalent to the main method in your console application.