|
GUI and application logic not so strictly separated
|
bet | 7/8 | Sana | 20.02.2024 | Hajmi | 1,28 Mb. | | #159175 |
Bog'liq www rkaiser de c-winforms-tutorial
In the version created under 1. to 4. the application logic is strictly separated from the user interface: The access to the user interface with C/CLI is exclusively done in Form1.h. In Header1.h, however, only standard C is used. This has in particular the advantage that one can use this header also in other platforms (e.g. console applications, Qt, Mac).
However, in the early stages of a project, when there is still a lot of experimenting going on, it can be a bit cumbersome if you have to change the calls in another file every time you change a parameter list. And for applications that are not intended to be used for other platforms at all, this strict separation doesn’t help much. This often applies to exercise tasks as well.
This jumping back and forth between different files can be avoided by relaxing the strict separation between the application logic and the user interface by including access to the controls in the header file as well.
Accessing controls in a header file is enabled by inserting
using namespace System;
using namespace System::Windows::Forms;
into the header file at the beginning. Then you can also use the types of the controls in the header file (e.g. as parameters) and include the statements that were in the buttonClick function in Form1.h under 4.
This procedure is implemented in the Header2.h file:
#pragma once using namespace System;
using namespace System::Windows::Forms;
int plus_2(int x)
{ return x + 2;
}
void plus_2_Click(TextBox^ in_textBox, TextBox^ out_textBox)
{
int n = Convert::ToInt32(in_textBox->Text); int result = plus_2(n);
out_textBox->AppendText(String::Format("plus_2({0})=
{1}\r\n", n, result));
}
Here you pass a parameter for the control to the function. Please note that you must specify a ^ after the name of a .NET type (e.g. TextBox, Button). In the function you then address the control under the name of the parameter.
This function can be called when a button is clicked:
private: System::Void
button_plus_1_Click(System::Object^ sender,
System::EventArgs^ e)
{ plus_2_Click(in_textBox, out_textBox);
}
|
| |