Search code examples
winformstextbox

Issue with clearing multiline textbox in WinForms application


I have a multiline TextBox in my app whose text I want to clear.

The problem is that when I clear the text using

textBox.Text = "" 
or 
textBox.Clear()

only the first line is cleared. The other lines are still there but cannot be deleted, and if I start typing into the textbox, the other lines will be overwritten, which leads me to think that it is a re-paint issue, but can't figure out why this happens.

The textbox is in a custom User Control inside a form which can be minimized. The issue appears after the form is minimized then restored.

I tried calling Refresh on the control and on the textbox to trigger repainting but it doesn't work.

Is there another way to trigger the repaint event to solve this issue ? Not sure what I'm doing wrong here.

EDIT:

Here is a sample of the code. The ShowForm method is called to restore the minimized form.

    public static void ShowForm()
    {
       if (!myForm.Visible)
       {
          myForm.PopulateForm();
          myForm.Show();
          myForm.Activate();
          myForm.WindowState = FormWindowState.Normal;
       }
    }

and inside PopulateForm (which is a method of myForm), I clear the text of the textbox:

   public void PopulateForm()
   {
       this.myControl.myTextBox.Clear(); 
   }

this.myControl is the user control which contains the textbox


Solution

  • As I suspected, this was due to a repaint issue. It was caused by the DoubleBuffered property being set to false for the user control.

    After setting it to true, the textbox is being rendered properly.