Matran Winform
Matran Winform
5 điểm)
https://www.bestprog.net/en/2016/04/29/011-c-an-example-of-creating-of-two-dimensional-
matrix-on-the-form-the-analogue-of-tstringgrid-component-in-delphi/#contents
Contents:
Instructions
four controls of type Button. Automatically, four objects (variables) with names “button1”, “button2”, “button3”,
“button4” will be created;
three controls of type “Label”, which are named as “label1”, “label2”, “label3”;
control of TextBox type, which is named “textBox1”.
You need to form the properties of controls of types “Button” and “Label”:
To set up the view and behavior of form you need to do following actions:
set the title of form. To do this property Text = “The product of matrices”;
property StartPosition = “CenterScreen” (the form is placed to the center of screen);
property MaximizeBox = “false” (hide the button of maximize of form).
In the opened window select “Windows Form“. The name of le leave as proposed “Form2.cs“.
Place on the form, in any position, the control of “Button” type (Figure 2). As a result, the new object named
“button1”, will be given.
In the text of module “Form1.cs” you need to add the following code:
...
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
const int MaxN = 10; // the maximum allowable dimension of the matrix
int n = 3; // The current dimension of the matrix
TextBox[,] MatrText = null; // The matrix of TextBox type elements
double[,] Matr1 = new double[MaxN, MaxN]; // The matrix 1 of floating point numbers
double[,] Matr2 = new double[MaxN, MaxN]; // The matrix 1 of floating point numbers
double[,] Matr3 = new double[MaxN, MaxN]; // The matrix of results
bool f1; // flag, which indicates about that the data were entered into the matrix
Matr1
bool f2; // flag, which indicates about that the data were entered into the matrix
Matr1
public Form1()
{
InitializeComponent();
}
}
}
...
// 3. Memory allocation for each cell of the matrix and its setting
for (i = 0; i < MaxN; i++)
for (j = 0; j < MaxN; j++)
{
// 3.1. Allocate memory
MatrText[i, j] = new TextBox();
The event “Load” is generated (called) when form is loading. Since there Form1 is the main form of the application,
the “Load” event of “Form1” will be called immediately after the application starts to run. So, here it is expedient to
introduce the initial initialization of global controls and internal variables of the program. These controls can be
called from other methods of the class.
In the event handler Form1_Load() the memory is allocated for two-dimensional matrix MatrText of strings only
one time. This memory will be automatically freed upon completion of the application.
The memory is allocated in two stages:
After allocating memory, for any object is carried out the setting of main internal properties (position, size, text
and visibility).
Also, every cell, which is created, is added (placed) on the form “Form2” using method Add() from class “Controls”.
Every new cell can be added on the any other form of application.
In the listing above, the value of n is read. After that, is carry out the setting of cells of matrix MatrText.
Based on the inputted value of n are formed the sizes of form “form2” and position of button “button1”.
If, into the form “Form2”, user is pressed on the button “OK” (button2) then the rows from MatrText are moved
into the two-dimensional matrix “Matr1” of oating point numbers. Converting from string to the corresponding
real number is performed by the method Paste() from the class Double.
Also, is formed the variable f1, which points that data were inputted into matrix “Matr1”.
⇑
You can control the changing of value n using the event “Leave” of control textBox1. The event “Leave” is
generated in time when control “textBox1” leaves the input focus (Figure 4).
First of all, the product of these matrices will be formed in the matrix Matr3. After that, the value from Matr3 is
moved in “MatrText” and is displayed on the form Form2.
The class FileStream is described in the namespace System.IO. Therefore, in the beginning of module “Form1.cs”
you need to add the following code:
using System.IO;
⇑
(0.5 điểm)
In above program, use the following code to use operator +. -. * by adding 3 more buttons
Add, Sub and Mul