【问题标题】:Textbox Array does not update in userControl WIndows Forms文本框数组不会在 userControl WINdows 窗体中更新
【发布时间】:2020-05-09 12:21:27
【问题描述】:

我想知道是否有人可以帮助我。

我正在尝试使用一组文本框构建用户控件。这个想法是能够显示信息的集合。

我可以获取一个要更新的文本框,但我无法获取一个数组来执行相同的操作。

函数 _aTextBox() 是从我的主程序上的 2 个按钮调用的,如下所示:

private void button1_Click(object sender, EventArgs e)  
{  
 ucTextBox1._aTextBox("Hello");  
}  
private void button2_Click(object sender, EventArgs e)  
{  
    ucTextBox1._aTextBox("World");  
}

My User Control code is below:

Namespace ucTextBox  
{  
public partial class ucTextBox: UserControl  
{  
public ucTextBox()  
  {  
                 InitializeComponent();  
  }  
                 TextBox[] txt = new TextBox[2];  
private void DisplayTextBoxes()  
  {  
     txt[0] = new TextBox();  
     txt[0].Location = new System.Drawing.Point(20, 48);  
     this.Controls.Add(txt[0]);  
  }  
public void _aTextBox(string text)  
   {  
     DisplayTextBoxes();  
     txt[0].Visible = false;  
     txt[0].Text = text; // *********************  
     txt[0].Visible = true;  
     txt[0].Invalidate();  
     txt[0].Update();  
     txtValue.Visible = true;  
     txtValue.Text = text;  
     txt[0].ResumeLayout(true);  
    }  
  }  
}

*在标有 // ********************* 的行,txt[0].Text 保存正确的字符串。但是,它从不显示它!!!*

欢迎提出任何建议。非常感谢您抽出宝贵时间阅读本文。

约翰

【问题讨论】:

    标签: c# textbox user-controls


    【解决方案1】:

    我认为您在同一位置绘制所有文本框。你必须用 x 和 y 坐标求和。

    最后绘制的文本框是最上面的,并隐藏了第一个插入的文本框。

    【讨论】:

      【解决方案2】:

      因为每次调用 _aTextBox(string text)
      该函数调用 DisplayTextBoxes();
      你总是用 new TextBox();
      重写你的 txt[0] 可能你会想要使用 List 而不是 Array 并将文本框添加到它的末尾。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-07
        • 1970-01-01
        • 1970-01-01
        • 2022-08-23
        • 2013-05-31
        • 2019-06-07
        • 2018-03-01
        • 1970-01-01
        相关资源
        最近更新 更多