0

I have two forms. MainForm and ProductDetailForm

Inside MainForm there is txtSearchBox input box and some radio values to filter search. When search is performing progressbar is displayed. Initially progress bar is set to Visible = false;

On btnSearch click event I'm calling background_worker to perform search. Immediatly progressbar is set to Visible = true.

When data is found and new window (ProductDetailForm) is opened on the MainForm progressBar is still visible, so my question is how to set Visible to false when new window is opened.

private void bntSearch_Click(object sender, EventArgs e) { progressBarControll.Visible = true; backgroundWorker1.RunWorkerAsync(); } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { MyData data = repository.GetData(1); // tried here progressBarControll.Visible = false; // but it raises an exception var detailsForm = new ProductDetailForm(data); } private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { progressBarControll.Visible = false; } 
1
  • 1
    It raises an exception because you can only access properties of a UI element from the thread that control was created on. Take a look at Form.Invoke. Commented Dec 3, 2013 at 11:07

1 Answer 1

1

You can move both opening the new form and hiding the progress bar to RunWorkerCompleted method:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { MyData data = repository.GetData(1); } private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { progressBarControll.Visible = false; var detailsForm = new ProductDetailForm(data); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.