Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / June 2007

Tip: Looking for answers? Try searching our database.

Invoking MessageBox from Scroll event of HScrollBar

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alexander Smirnov - 20 Jun 2007 10:03 GMT
Hello!
I need at some circumstances to show a message box from the Scroll
event handler of an HScrollBar component. But when I invoke
MessageBox.Show() the scroll event occurs again and again and I can
have up to about 50 message boxes on the screen. Something  with
message queue, I suspect.
How to prevent this?

Thanks in advance.
Peter Duniho - 20 Jun 2007 17:48 GMT
> Hello!
> I need at some circumstances to show a message box from the Scroll
[quoted text clipped - 3 lines]
> message queue, I suspect.
> How to prevent this?

The simple answer is "don't do that".

It's not a good idea to execute complex UI code, such as displaying a  
dialog box, in event handlers that deal with real-time, on-going user  
interaction, such as scrolling a scroll bar.  Why you think it would make  
sense to the user to display a dialog box in response to scrolling, I  
don't know (it sounds like very poor UI to me), but assuming you feel you  
simply must do this, you need to do a couple of things:

    * Instead of showing the dialog box immediately in the event handler,  
use some mechanism to defer the action until after you've returned from  
the scroll bar handling.  For example, use Control.BeginInvoke() with the  
form and a delegate that will show the dialog box.

Doing this will ensure that all of the click-and-drag sort of stuff in the  
scroll bar handling sequence can complete before control gets passed off  
to the dialog box's message pump.

    * Disable the scroll bar before you show the dialog box and if  
necessary, put an internal flag into your class that you can set to ensure  
that you only show the dialog box once.  I suspect that disabling the  
scroll bar would be sufficient, but if you have trouble with that, the  
flag should definitely work if used correctly.  Set the flag in the scroll  
bar event handler when you call Control.BeginInvoke() to show the dialog,  
check the flag before calling Control.BeginInvoke() and only show the  
dialog if it's cleared, and then clear the flag after returning from the  
actual call to Form.ShowDialog() in your delegate responsible for showing  
the dialog.

This is the key part that will ensure that the dialog box is only  
displayed once.  You could probably get away with just doing this part,  
but IMHO it's a bad idea to display a dialog box from within the scroll  
bar handling code, thus the first point above.

Pete

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.