WindowsForms 与简单控制台应用程序的区别
我目前开始将我的控制台项目“移植”到 WinForms,但似乎我在这方面做得很失败。
我只是习惯了控制台结构:
我的类根据来自控制台的输入相互交互。 一个简单的流程:
Input -> ProcessInput -> Execute -> Output -> wait for input
现在我得到了这个大的 Form1.cs (等)和“Application.Run(Form1);” 但我真的不知道我的类如何与表单交互并创建如上所述的流程。
我的意思是,对于表单中的每个“项目”,我只有这些“...._Click(object sender...)”。 现在我不知道在哪里放置/开始我的流程/循环,以及我的类如何与表单交互。
I currently started to "port" my console projects to WinForms, but it seems I am badly failing doing that.
I am simply used to a console structure:
I got my classes interacting with each other depending on the input coming from the console.
A simple flow:
Input -> ProcessInput -> Execute -> Output -> wait for input
Now I got this big Form1.cs (etc.) and the "Application.Run(Form1);"
But I really got no clue how my classes can interact with the form and create a flow like I described above.
I mean, I just have these "...._Click(object sender....)" for each "item" inside the form.
Now I do not know where to place / start my flow / loop, and how my classes can interact with the form.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,非常简单(尽管我可以理解你的困惑)...
1。输入
有一个
TextBox
和一个Button
。当用户单击按钮时,将TextBox
中的任何内容视为输入。2.处理输入
在控制台应用程序中,当您处理输入时,用户无法执行任何操作。 Windows 窗体应用程序中的类似情况是禁用用户可以提供输入的机制。因此,请设置
TextBox.Enabled = false
和Button.Enabled = false
。3.执行
运行您想要执行的任何方法。
4.输出
在表单上显示某种消息。这可能只是另一个
TextBox
或RichTextBox
...无论您想要什么。5.等待输入
执行第 3 步中的方法后,您已在第 4 部分中显示了输出,您可以继续并重新激活接受输入的机制:
TextBox.Enabled = true
和Button .Enabled = true
。所以基本上你的代码应该像这样:
Pretty straightforward, actually (though I can sympathize with your confusion)...
1. Input
Have a
TextBox
and aButton
. When the user clicks on the button, treat whatever's in yourTextBox
as your input.2. Process Input
In a console app, the user is unable to do anything while you're processing input. The analogue to this in a Windows Forms app is to disable the mechanism by which the user can supply input. So, set your
TextBox.Enabled = false
andButton.Enabled = false
.3. Execute
Run whatever method you want to execute.
4. Output
Have some kind of message displayed on the form. This could be simply another
TextBox
, or aRichTextBox
... whatever you want.5. Wait for Input
Once your method from step 3 has executed, you've displayed the output in part 4, you can go ahead and re-activate your mechanism for accepting input:
TextBox.Enabled = true
andButton.Enabled = true
.So basically your code should like something like this:
基本上,您可以让表单提供一组用于输入数据的控件(即:一个或多个 TextBox 控件)。如果您有一个用户单击的按钮,并且您想要处理,只需双击该按钮即可。这将为您提供一个事件处理程序,如下所示:
就是这样 - “循环”消失了,因为标准 Windows 消息泵取代了它的位置。
Basically, you can have your form provide a set of controls for inputting data (ie: one or more TextBox controls). If you have a button that the user clicks, and you want to process, just double click on teh button. This will give you an event handler like:
That's it - the "loop" goes away, since the standard Windows message pump takes its place.