John Woodiwiss

John Woodiwiss

  • NA
  • 21
  • 639

Textbox array does not update in a User Control

Jan 21 2020 5:54 PM
Hello everyone,
 
I am attempting to build a User Control with a collection of TextBoxes. The idea is to be able to display a collection of information.
 
I can get a single Textbox to update, but I just can't get an array to do the same thing.
 
The function _aTextBox() is called from 2 buttons on my main program as follows:
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3. ucTextBox1._aTextBox("Hello",0);  
  4. }  
  5. private void button2_Click(object sender, EventArgs e)  
  6. {  
  7. ucTextBox1._aTextBox("World",1);  
  8. }  
Any and all suggestions welcomed . Many thanks for your time reading this.
 
My User Control code is below:
 
At the line marked with // *********************, txt[0].Text holds the correct string. However, it never displays it!!!
  1. namespace ucTextBox  
  2. {  
  3. public partial class ucTextBox: UserControl  
  4. {  
  5. public ucTextBox()  
  6. {  
  7. InitializeComponent();  
  8. }  
  9. TextBox[] txt = new TextBox[2];  
  10. private void DisplayTextBoxes()  
  11. {  
  12. txt[0] = new TextBox();  
  13. txt[0].Location = new System.Drawing.Point(20, 48);  
  14. this.Controls.Add(txt[0]);  
  15. }  
  16. public void _aTextBox(string text)  
  17. {  
  18. DisplayTextBoxes();  
  19. txt[0].Visible = false;  
  20. txt[0].Text = text; // *********************  
  21. txt[0].Visible = true;  
  22. txt[0].Invalidate();  
  23. txt[0].Update();  
  24. txtValue.Visible = true;  
  25. txtValue.Text = text;  
  26. txt[0].ResumeLayout(true);  
  27. }  
  28. }  
  29. }  

Answers (1)