0

Here is what my code looks like:

private void exportToExcelButton_Click(object sender, EventArgs e)
{   
    txtBox.Clear();

    txtBox.AppendText("Beginning Export...");
    ExportExcel(txtBox);
    txtBox.AppendText("Export complete...");                  
}

The problem I am having is that whenever the button is clicked (to execute the function above), only part of the current text in the TextBox (System.Windows.Forms.TextBox) is cleared, and replaced with the first line: "Beginning Export ...". However once the function ExportExcel(txtBox) is done executing, then the entire text is replaced by the new one generated in ExportExcel(txtBox).

Inside ExportExcel(txtBox); I have several txtBox.AppendText() statements explaining to the user the actions being made.

I have tried clearing the text with txtBox.Text = String.Empty; and txtBox.Text = "";and neither have worked.

Let me know if anything needs to be clarified, thanks.

ekad
  • 14,436
  • 26
  • 44
  • 46

3 Answers3

4

Looks like you're blocking the GUI thread, preventing the text box from redrawing itself. This is one reason why you shouldn't perform long-running tasks on the GUI thread. Use a background thread instead. That way you leave the GUI thread free to perform important operations like drawing, responding to mouse clicks, etc.

Community
  • 1
  • 1
TypeIA
  • 16,916
  • 1
  • 38
  • 52
  • Is it possible to access UI controls from withing BackgroundWorker? – Andres De la Barra Feb 12 '14 at 17:38
  • 1
    @AndresDelaBarra Not directly: you'll need to [use Invoke/BeginInvoke](http://stackoverflow.com/questions/3046245/whats-wrong-with-my-cross-thread-call-in-windows-forms) to do GUI operations. That link has a full discussion. – TypeIA Feb 12 '14 at 19:17
1

I agree with dvnrrs. However if you are unable to do this, try calling txtBox.Refresh();after adding each line of text.

There is another method called Application.DoEvents(); that has a similar behavior, but its use is not recommended since it sort of short-circuits the normal application flow and can cause your application to fail unexpectedly or do strange things.

Community
  • 1
  • 1
Andacious
  • 1,162
  • 10
  • 17
  • 3
    `Application.DoEvents();` is almost always the wrong suggestion. – LarsTech Feb 12 '14 at 17:02
  • `txtBox.Refresh()` seems do the trick, however I think that I might have to consider using a background thread eventually. @LarsTech would you mind explaining why using `Application.DoEvents()` is a bad idea, it seems to do be doing the same thing as `txtBox.Refresh()`. Thanks – Andres De la Barra Feb 12 '14 at 17:40
  • 2
    @AndresDelaBarra See [Use of Application.DoEvents()](http://stackoverflow.com/q/5181777/719186) – LarsTech Feb 12 '14 at 17:47
  • 1
    @LarsTech thanks for clarifying this. See edits above. – Andacious Feb 12 '14 at 23:07
1

Have you tried the textBox.Refresh , before calling txtBox.AppendText("Beginning Export...").

The method invalidates the control.

On the other hand, if you use a background thread, then you should update the UI only by overriding the Progress Changed event. Background threads are not meant for updating user interfaces. Try searching for Worker threads and UI threads. They correlate to MFC, but the concept is the same.

Also keep in mind the cross thread calls.

Community
  • 1
  • 1
Sujay Ghosh
  • 2,828
  • 8
  • 30
  • 47