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 / Windows Forms / WinForm General / September 2004

Tip: Looking for answers? Try searching our database.

MsgBox problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jason - 20 Sep 2004 22:16 GMT
I have a form which is used to add items (stock symbols) to a list. On the form is a listbox, textbox, and a button. The list contains the current list of items, the textbox is for entering a new symbol, and the button does the actual adding. Before adding, I check to see if the is already in the list. If it is, I display a message using the MsgBox class and set the focus back to the textbox. To make data entry easier, I set a handler to the textbox's KeyUp event and listen for the Enter key. When pressed, I call the PerformClick event of the button.

Here is the problem. If I use the enter key to dismiss the MsgBox (when a duplicate symbol was entered) and the focus is set to the textbox, the KeyUp event goes off again with the KeyCode being enter. The only way to get out of the cycle is to use the mouse and click the MsgBox's Ok button.

Is there any way to clear the keyboard buffer after calling the MsgBox class?

You can reproduce the problem by following these directions (my code is VB.NET):

* Create new WIndows Forms project
* Put a TextBox control and a Button control on the form.
* Go into Code view and add the following code:

Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
   If e.KeyCode = Keys.Enter Then Button1.PerformClick()
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
   MsgBox("Hello")
   TextBox1.Focus()
End Sub

- Thanks
mavisahdas - 21 Sep 2004 04:43 GMT
Hi Jason,

Try with the following code remove KeyUp event of the textbox and use
KeyPress event...

try the following code...

Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
       MsgBox("Hello")
       TextBox1.Focus()
   End Sub

   Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
       If Asc(e.KeyChar) = 13 Then
           Button1_Click(Button1, New EventArgs)
       End If
   End Sub

with regards,

Sadha Sivam S,
Microsoft Community Star,
http://answerindotnet.blog.com
Jason - 21 Sep 2004 18:09 GMT
Thanks Sadha. Works great.

Follow up question is why? Why does the keyup event get passed back to the
textbox from the msgbox?

- Jason

> Hi Jason,
>
[quoted text clipped - 21 lines]
> Microsoft Community Star,
> http://answerindotnet.blog.com

Rate this thread:







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.