Instead of using a pre-made TextBox control in design.cs, I am using a programmatically-added TextBox. First, this TextBox is filled by the user, and with a button onClick method, the content is processed inside a method in another class file called from the onClick method. After that, I want to remove whatever is in the TextBox and make it empty like its initial state, but it is not quite working.
/*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();
}
Can someone please help me find what is wrong with this code? I know that
EmptyTextBox()
method is called, but nothing happens.