简体   繁体   中英

Odd form behaviour. Text only appearing in textbox sometimes after setting Text property

In vb6 you could see a textbox update immediately when the value was changed, but I've noticed in .net that it will not update until after the method you are in has been exited. A preliminary question I have is whether there is a way to make the textbox update before the method is completed.

The issue is that I have two textboxes whose Text properties are set and when the method completes, only one of them consistently updates on screen. The other usually does not, but sometimes does. It's very sporadic. I literally called someone over to verify I wasn't taking crazy pills and of course it started working when she came over (though I made no code changes). Then when the QA guy came around it stopped working again (again, didn't make any code changes).

All I've been doing is setting break points and stepping through code trying to figure out what's keeping this text box from being updated. At the end of the procedure, right before it exits, I can check on the Text property and it has the correct value, but as soon as the method finishes it disappears.

I will try to get a code snippet up soon, but in the meantime I would love to know if anyone else has had this problem and any good ideas on how to debug b/c I'm getting a bit frustrated! There is a timer on the form that is enabled at a certain point and disables itself when it's run... setting a breakpoint in this timer verifies that it is not the culprit b/c the breakpoint never gets hit. But I am wondering if there could be some other asynchronous process I might be missing... I don't think so but if you can tell me anything I should be looking for that I might not have thought of please do.

Edit: I would post a code snippet, but the snippet I wanted to post doesn't reproduce the problem and I have not isolated the problem to a small enough section of code for it to be practical to post. I will add a little more info though:

After the method that updates these textboxes is completed, control returns to the form. There are no other processes going on. I kept thinking maybe some code was getting run somewhere that was blanking out the textbox but a thorough look through the code has confirmed this is definitely not the case... when the method completes nothing else happens.

I noticed when I was debugging that sometimes it would work properly, and on a rare occasion it would even work properly when running normally. I added a DoEvents() to see if it would work and it did the first time... but then did not continue to work. Out of frustration I added repeated calls to set the textbox and DoEvents() after each one and that did not make it perfect. Lastly I added a call for the thread to Sleep() for 300 milliseconds and it seems to be better now.

Keep in mind that there is another almost identical textbox on the form that gets set in the same procedure and never has any problem updating whatsoever. I have searched for any difference between the two controls and aside from size, location, and name I cannot find any difference.

I suppose you could add: Application.DoEvents() after you set the textbox text value. Its usually frowned upon to use DoEvents, though it may solve your issue in the intermediate.

i think you better use Invalidate(); this will cause control to redraw yourtextbox.Invalidate();

you can use Threading for this purpose

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM