Difference of WindowsForms with a Simple Console Application
Currently I have started to "port" my console projects to WinForms, but I seem to have a hard time tolerating it.
I just use the console structure:
I have connected my classes to each other depending on the input coming from the console. Simple flow:
Input -> ProcessInput -> Execute -> Output -> wait for input
Now I got this big Form1.cs (etc) and "Application.Run (Form1)"; But I really didn't understand how my classes can interact with the form and create a flow like I described above.
I mean I only have these "...._ Click (object sender ....)" for each "item" inside the form. Now I don't know where to place / start my thread / loop and how my classes can interact with the form.
a source to share
Quite simple, actually (although I can sympathize with your confusion) ...
1. Entering
Have TextBox
and Button
. When the user clicks on the button, treat everything in yours TextBox
as your input.
2. Entering the process
In a console application, the user cannot do anything while the input is being processed. The analog to this in a Windows Forms application is to disable the mechanism by which the user can provide input. So, install TextBox.Enabled = false
and Button.Enabled = false
.
3. Execute
Execute any method you want to execute.
4. Exit
Enter some message in the form. It can be just another one TextBox
or RichTextBox
... whatever you want.
5. Wait for input
Once your method from step 3 is done, you display the output in part 4, you can proceed and reactivate your mechanism to accept input: TextBox.Enabled = true
and Button.Enabled = true
.
So, basically your code should look 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;
}
}
a source to share
Basically, you can have your own form that provides a set of controls for entering data (for example, one or more TextBox controls). If you have a button that the user clicks and you want to process, just double click on the 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 it - the "cycle" is gone as the standard Windows service pump takes its place.
a source to share