473,325 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Blank tab page when switching tab pages inside a thread

The following is a bug I have discovered using tab pages and threads
and I am looking for a workaround.

Create a new Windows Forms application and add a tab control with two
tab pages. Add a label to each tab page. On the first tab page add a
button. When the button is pressed the code behind the button creates
a thread and starts that thread. The only thing that the thread is to
do is to switch from the first tab page to the 2nd tab page. This is
done by setting the SelectedTab property of the tab control to the 2nd
tab page.

When you run the application, do not click on the 2nd tab page.
Instead, click on the button. The tab page will switch to the 2nd tab
page but it will be blank (or empty). The label that you placed on the
2nd tab page is not visible. It's Visible property is in fact set to
true if you were to interrogate the control's properties.

Now try the whole thing again. Stop the program and start it again.
This time, before clicking the button, use the mouse and select the
2nd tab page. Now go back to the first tab page and press the button.
The label on the 2nd tab page now shows up.

This problem only occurs when the switching is within a thread. If you
do the switching within the button's click event without using a
thread, this problem never occurs. It appears therefore that when the
thread dies, it wipes the label off of the 2nd tab page. All the
methods I've tried to redraw the control fail to redraw it again. In
fact, if you put a message box inside the thread just prior to exiting
the thread, the label will get redrawn but as soon as you click the
message box's OK button and the thread terminates, the label will be
erased from the form. The label still exists, at least in code, but
nothing visual exists.

Any clues how to get around this?

Thanks,
Johann
Nov 16 '05 #1
2 2373

"Johann Blake" <jo*********@yahoo.com> wrote in message
news:d8************************@posting.google.com ...
The following is a bug I have discovered using tab pages and threads
and I am looking for a workaround.

Create a new Windows Forms application and add a tab control with two
tab pages. Add a label to each tab page. On the first tab page add a
button. When the button is pressed the code behind the button creates
a thread and starts that thread. The only thing that the thread is to
do is to switch from the first tab page to the 2nd tab page. This is
done by setting the SelectedTab property of the tab control to the 2nd
tab page.
[snip] This problem only occurs when the switching is within a thread. If you
do the switching within the button's click event without using a
thread, this problem never occurs. It appears therefore that when the
thread dies, it wipes the label off of the 2nd tab page. All the
methods I've tried to redraw the control fail to redraw it again. In
fact, if you put a message box inside the thread just prior to exiting
the thread, the label will get redrawn but as soon as you click the
message box's OK button and the thread terminates, the label will be
erased from the form. The label still exists, at least in code, but
nothing visual exists.

Any clues how to get around this?


Yes. Stick to the first golden rule of Windows Forms threads programming:
never access a control or a form directly from a non-UI thread. See Jon
Skeet's FAQ at
http://www.yoda.arachsys.com/csharp/...#windows.forms for
more information.

Regards,
Sami
Nov 16 '05 #2
I ran into this problem, but I have to use multiple threads due to other
functions of our system. The fix is to make the method that switches the
tabs threadsafe by creating a delegate and using the Forms.Invoke method.

"Sami Vaaraniemi" wrote:

"Johann Blake" <jo*********@yahoo.com> wrote in message
news:d8************************@posting.google.com ...
The following is a bug I have discovered using tab pages and threads
and I am looking for a workaround.

Create a new Windows Forms application and add a tab control with two
tab pages. Add a label to each tab page. On the first tab page add a
button. When the button is pressed the code behind the button creates
a thread and starts that thread. The only thing that the thread is to
do is to switch from the first tab page to the 2nd tab page. This is
done by setting the SelectedTab property of the tab control to the 2nd
tab page.

[snip]
This problem only occurs when the switching is within a thread. If you
do the switching within the button's click event without using a
thread, this problem never occurs. It appears therefore that when the
thread dies, it wipes the label off of the 2nd tab page. All the
methods I've tried to redraw the control fail to redraw it again. In
fact, if you put a message box inside the thread just prior to exiting
the thread, the label will get redrawn but as soon as you click the
message box's OK button and the thread terminates, the label will be
erased from the form. The label still exists, at least in code, but
nothing visual exists.

Any clues how to get around this?


Yes. Stick to the first golden rule of Windows Forms threads programming:
never access a control or a form directly from a non-UI thread. See Jon
Skeet's FAQ at
http://www.yoda.arachsys.com/csharp/...#windows.forms for
more information.

Regards,
Sami

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: connoisseur.infotech | last post by:
We have a website we need to open popup under following conditions 1) When user close the browser either via X button or alt + f4, file -> Close 2) when he changes URL and goes to some other site....
6
by: noway | last post by:
I have greated a report and have included a page break in it. The report breaks were it is supposed to but then it creates a blank page between the two pages. Since this report will print out...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
4
by: Jeremy Holt | last post by:
Hi, In a windows.forms application I would BeginInvoke a delegate on the UI thread to collect data from a database. When the call returns to the AsyncCallback, if the Control.InvokeRequired =...
2
by: Amir | last post by:
I really hope someone can explain this behavior. I have spend quite a bit of time and tried a bunch of things, so far nothing has worked. I have a page on which I have a datagrid with template...
2
by: JohnGates | last post by:
In VB.net, I have win form programs that do database update operations that take several minutes or longer. Several actions such as switching windows or clicking a control during these operations...
5
by: hman | last post by:
Forgive me if this is the wrong group to post this in, it's my first post. I have a web site that uses session variables and cookies. In-house and at most clients the web sites functions great,...
1
by: mbruyns | last post by:
i have been trying (and sometimes succeeding) to use the modalpopupextender to show various panels of controls on my asp pages. the strange problem that i keep on running into is that sometimes it...
3
by: Andy B | last post by:
I have a web form that has controls to be modified on it. The class that will modify the controls is not contained inside the page code. How would I best pass the controls to the external class?
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.