How to get access from a class to dynamically created form elements (in my case, textBox)?

This is how I create the elements:

for (int i = 1; i <= Convert.ToInt32(input); i++) { panel1.Controls.Add(new TextBox() { Name = i.ToString(), Location = new Point(54, yTextBox_n = yTextBox_n + 35), Font = new Font("Times New Roman", 14.25f) }); panel1.Controls.Add(new Label() { Name = i.ToString(), Location = new Point(8, yLabel_n = yLabel_n + 35), Text = "n" + i, Font = new Font("Times New Roman", 14.25f, FontStyle.Italic) }); } 

I have an algorithm. I enter the necessary data from the form and these dynamically generated elements are displayed. then I enter the data in them and I need to pass this data to the class algorithm how to pass them?

Here is a class. It is not completed at all. It should receive dynamically created elements.

 class Class_Algorithm { int[,] Matrix; int L; int N; public Class_Algorithm(int[,] matrix, int l, int n) { Matrix = matrix; L = l; N = n; } public void Algorithm() { int k = 1; int q = 1; for (int d = 0; d < L; d++) { int m = 1; int z = 1; int[] r = new int[N]; for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { r[i] += Matrix[i, j]; } } } } } 
  • 2
    Pass them by parameter)) In general - IMHO - it’s not nice to do that. If such a need arises - it means problems with architecture. If the last speech does not go - read the first sentence again. - isnullxbh
  • in any other class in which for example? - Grundy
  • What is the class algorithm? where is its variable declared? What parameters does the function take to call? - Grundy
  • for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) - Igor

2 answers 2

 private Dictionary<int, TextBox> dynamicTextboxes = new Dictionary<int, TextBox>(); private string TextByIndex(int index) { string result = ""; if (dynamicTextboxes.ContainsKey(index)) result = dynamicTextboxes[index].Text; return result; } dynamicTextboxes.Clear(); for (int i = 1; i <= Convert.ToInt32(input); i++) { TextBox box = new TextBox() { Name = "textbox_" + i.ToString(), Location = new Point(54, yTextBox_n = yTextBox_n + 35), Font = new Font("Times New Roman", 14.25f) }; dynamicTextboxes[i] = box; panel1.Controls.Add(box); panel1.Controls.Add(new Label() { Name = "label_" + i.ToString(), Location = new Point(8, yLabel_n = yLabel_n + 35), Text = "n" + i, Font = new Font("Times New Roman", 14.25f, FontStyle.Italic) }); } 
  • You can find out what question you answer? - user227049
  • @FoggyFinder I answer the question "how to transfer them?" - Igor
  • I will not say for the author of the question, but the answer is not clear to me. Moreover, I think that transferring form elements to other classes is a bad practice. - user227049
  • @FoggyFinder I got the impression that the vehicle has difficulty getting values ​​from dynamically created textboxes. I suggest storing references to them in a container (a member of a form class). - Igor
  • Thanks, now I understand. Then, I suppose you need to supplement the answer by adding a method that converts the information from dynamicTextboxes to int[,] - user227049

This is in the form where this element is added.

 this.Controls.Find("ControlName", false); 

If from another class, then in the parameter pass the Ui component to which is added dynamically control. This is actually a bad way, post the code!

In your case, I do not see anything wrong if I add a function to the form class that will pass the desired value to the algorithm's constructor.

 public void GetTextByDynamicCreateTextBox(string name) { string param = panel1.Controls.Find(name, false).FirstOrDefault().Text; Algorytm alg = new Algorytm(param); }