mihax
❔ Matrix dynamic allocation
private void btnChange_Click(object sender, EventArgs e)
{
openFileDialog = new OpenFileDialog();
openFileDialog.Title = "Choose input";
openFileDialog.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
openFileDialog.Filter = "Text Files (*.in;*.txt)|*.in;*.txt|" +
"All Files (*.*)|*.*";
ReadDataChange();
CreateGUI();
tbl.Update();
}
private void ReadDataChange()
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
pathFileName = openFileDialog.FileName;
StreamReader sr = new StreamReader(pathFileName);
string[] s;
char[] sep = { ' ' };
string line = sr.ReadLine();
s = line.Split(sep, StringSplitOptions.RemoveEmptyEntries);
n = int.Parse(s[0]);
m = int.Parse(s[1]);
H = new int[n + 1, m + 1];
c = new int[n + 1, m + 1];
int i = 0;
while ((line = sr.ReadLine()) != null)
{
s = line.Split(sep, StringSplitOptions.RemoveEmptyEntries);
for (int j = 0; j < m; ++j)
H[i, j] = int.Parse(s[j]);
i++;
if (i == n)
break;
}
}
}
62 replies
❔ Matrix dynamic allocation
private void CreateGUI()
{
btn = new Button[n + 1, m + 1];
tbl = new TableLayoutPanel()
{
RowCount = n,
ColumnCount = m,
Dock = DockStyle.Fill
};
int W = panel.Width;
int H1 = panel.Height;
L = Math.Min(H1, W) / Math.Max(n, m);
for (int j = 0; j < m; ++j)
tbl.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, L));
for (int i = 0; i < n; ++i)
{
tbl.RowStyles.Add(new RowStyle(SizeType.Percent, L));
for (int j = 0; j < m; ++j)
{
btn[i, j] = new Button()
{
Dock = DockStyle.Fill,
BackColor = Color.Yellow,
Text = H[i, j].ToString(),
ForeColor = Color.Black,
Font = new Font("Arial", 14),
Margin = new Padding(0),
FlatStyle = FlatStyle.Flat
};
tbl.Controls.Add(btn[i, j], j, i);
}
}
panel.Controls.Add(tbl);
}
62 replies