而不是在design.cs中使用预先创建的TextBox控件,我使用的是编程式添加的TextBox。首先,这个TextBox由用户填充,并且通过一个onClick方法的按钮,内容在另一个由onClick方法调用的类文件中的方法内处理。之后,我想删除文本框中的任何内容,并使其像初始状态一样变空,但它不起作用。无法访问以编程方式添加的控件(C#)
/*MainForm.cs*/
private TextBox tb;
private SubForm sf = new SubForm();
private void initTextBox(){
tb = new TextBox();
preExistingPanel.Controls.Add(tb); //attach the textbox onto a panel
}
private void MainForm_Load(object sender, EventArgs e){
initTextBox();
}
private void button_Click(object sender, EventArgs e){
string tbContent = tb.Text;
sf.processData(tbContent);
}
public void EmptyTextBox(){
tb.Text = ""; //This does not work, and nothing happens
}
/*SubForm.cs*/
public void processData(string tbContent){
/*Do something with tbContent*/
...
...
/*Here, I want to empty the textBox*/
MainForm mf = new MainForm();
mf.EmptyTextBox();
}
可有人请帮我看看什么是错的代码?我知道
EmptyTextBox()
方法被调用,但没有任何反应。
什么是preExistingPanel,主要形式之间的关系子表单?没有这些信息,我不确定任何人都可以帮到您 – wnvko