Answer the question
In order to leave comments, you need to log in
How to implement data transfer from one form to another?
There is a code that makes 100 textboxes on form2. The boxes are filled with '+', then the number of given pluses is calculated for each line. The line number and the number of pluses are the initial data for form3, which will contain the Chart. I don’t understand how to transfer data to form3 in order to use it for the chart.
namespace VKR
{
public partial class Form2 : Form
{
public int Nx = 40, Ny = 28;
public int Loc_x = 3, Loc_y = 3, Sw = 35, Sh = 20;
public static int iLimit = 10, iVariable = 10;
public int counter = 0;
public int[] count;
public TextBox[,] tbArray;
public Label[] lbArray_exp;
public Label[] lbArray_V;
public Form2()
{
InitializeComponent();
Create(iLimit, iVariable);
}
public void Create(int lim, int var)
{
tbArray = new TextBox[lim, var];
lbArray_exp = new Label[var];
lbArray_V = new Label[var];
//
for (int i = 0; i < lim; i++, Loc_y += Ny)
{
Loc_x = 3;
//
lbArray_V[i] = new Label();
lbArray_V[i].Name = "label_V" + i;
lbArray_V[i].Text = string.Format("{0}", (i + 1) * 10);
lbArray_V[i].TextAlign = ContentAlignment.TopLeft;
lbArray_V[i].Location = new Point(0, Loc_y);
//
panel3.Controls.Add(lbArray_V[i]);
//
for (int j = 0; j < var; j++, Loc_x += Nx)
{
tbArray[i, j] = new TextBox();
tbArray[i, j].Name = "TextBoxx" + j;
tbArray[i, j].Size = new Size(Sw, Sh);
tbArray[i, j].Location = new Point(Loc_x, Loc_y);
panel1.Controls.Add(tbArray[i, j]);
//
lbArray_exp[j] = new Label();
lbArray_exp[j].Name = string.Format("exp_", j);
lbArray_exp[j].Text = string.Format("Экс.{0}", j + 1);
lbArray_exp[j].TextAlign = ContentAlignment.MiddleRight;
lbArray_exp[j].Location = new Point(Loc_x - 65, 0);
panel2.Controls.Add(lbArray_exp[j]);
}
}
}
private void Form2_Load(object sender, EventArgs e)
{
Button btn = new Button();
//
btn.Name = "btn1";
btn.Text = "Обработать";
btn.Size = new Size(80, 25);
btn.Location = new Point(this.ClientSize.Width - btn.Width - 5, this.ClientSize.Height - btn.Height - 5);
this.Controls.Add(btn);
//
foreach (TextBox tb in panel1.Controls.OfType<TextBox>())
{
tb.KeyPress += textBox_KeyPress;
}
btn.Click += btn1_Click;
}
private void btn1_Click(object sender, EventArgs e)
{
Form3 form3 = new Form3();
//
string[] str = new string[iLimit];
int[] count = new int[iLimit];
//
for (int i = 0; i < iLimit; i++, counter = 0)
{
for (int j = 0; j < iVariable; j++)
{
if (tbArray[i, j].TextLength >1)
{
tbArray[i, j].Text = "+";
}
if (tbArray[i, j].Text == "+")
{
counter++;
}
}
count[i] = counter/iLimit;
}
form3.ShowDialog();
this.Hide();
}
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != '+' && e.KeyChar != (char)8)
{
MessageBox.Show("ошибка ввода");
e.KeyChar = new char();
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Well as a variant the general static class.
In one form, data was written into it, in another it was shown.
There are many options, although the simplest static class, but you can use the event mechanism and subscribe to changes on form2;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question