WindowsForms 与简单控制台应用程序的区别

发布于 2024-08-31 13:56:19 字数 392 浏览 5 评论 0原文

我目前开始将我的控制台项目“移植”到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烂柯人 2024-09-07 13:56:19

实际上,非常简单(尽管我可以理解你的困惑)...

1。输入
有一个 TextBox 和一个 Button。当用户单击按钮时,将 TextBox 中的任何内容视为输入。

2.处理输入
在控制台应用程序中,当您处理输入时,用户无法执行任何操作。 Windows 窗体应用程序中的类似情况是禁用用户可以提供输入的机制。因此,请设置 TextBox.Enabled = falseButton.Enabled = false

3.执行
运行您想要执行的任何方法。

4.输出
在表单上显示某种消息。这可能只是另一个 TextBoxRichTextBox...无论您想要什么。

5.等待输入
执行第 3 步中的方法后,您已在第 4 部分中显示了输出,您可以继续并重新激活接受输入的机制:TextBox.Enabled = trueButton .Enabled = true

所以基本上你的代码应该像这样:

void myButton_Click(object sender, EventArgs e) {
    try {
        myInputTextBox.Enabled = false;
        myButton.Enabled = false;

        var input = ParseInput(myInputTextBox.Text);

        var output = ExecuteMethodWithInput(input);

        myOutputTextBox.Text = FormatOutput(output);

    } finally {
        myInputTextBox.Enabled = true;
        myButton.Enabled = true;
    }
}

Pretty straightforward, actually (though I can sympathize with your confusion)...

1. Input
Have a TextBox and a Button. When the user clicks on the button, treat whatever's in your TextBox 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 and Button.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 a RichTextBox... 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 and Button.Enabled = true.

So basically your code should like something like this:

void myButton_Click(object sender, EventArgs e) {
    try {
        myInputTextBox.Enabled = false;
        myButton.Enabled = false;

        var input = ParseInput(myInputTextBox.Text);

        var output = ExecuteMethodWithInput(input);

        myOutputTextBox.Text = FormatOutput(output);

    } finally {
        myInputTextBox.Enabled = true;
        myButton.Enabled = true;
    }
}
沙与沫 2024-09-07 13:56:19

基本上,您可以让表单提供一组用于输入数据的控件(即:一个或多个 TextBox 控件)。如果您有一个用户单击的按钮,并且您想要处理,只需双击该按钮即可。这将为您提供一个事件处理程序,如下所示:

private void button1_Click(object sender, EventArgs e)
{
     // Process Input from TextBox controls, etc.
     // Execute method
     // Set output (To other controls, most likely)
}

就是这样 - “循环”消失了,因为标准 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:

private void button1_Click(object sender, EventArgs e)
{
     // Process Input from TextBox controls, etc.
     // Execute method
     // Set output (To other controls, most likely)
}

That's it - the "loop" goes away, since the standard Windows message pump takes its place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文