Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi everyone,

 

im having serious problem with command buttons at the moment.

I have a form that has buttons on it, now the issue i have is that when i click on the button it fires the click event (which it should do), BUT when the button has the focus and i press Enter on the keyboard it ALSO fires off the click event and i dont want it to.

 

So how can i get it to fire off the button_keypress event when i press Enter instead of it firing off the button_click event?

 

Any help would be SOOOOO GRATEFUL :-)

 

Dan

Posted

Off the top of my head without much thought:) Have you tried putting a check in the click event that checks the status of the keypress? If it is Enter exit the sub?

 

must be a more elegant way.......

My website
  • *Experts*
Posted

You might be able to override the button and handle the EnterKey in a Preview type of event but... why would you want to? Unless you're doing something funny with the button (custom drawing it for some reason) I can't imagine why you wouldn't want Enter to trigger the click event. Anything *but* calling the click event would confuse a user.

 

-Ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

The reason is a bit long winded but here goes :-)

 

My system accepts barcodes, and at the end of a barcode is the enter key. Most of the time the form has the focus and everytime i scan a barcode it triggers the keydown event.

 

However if a button on my form has the focus (cause it has been pressed etc) then i scan a barcode, all the numbers of the barcode are handled through the forms keydown event, but the enter key at the end is misintuperated as the enter key on the button which fires the buttons click event.

 

Hope this makes sense

 

Dan

  • *Experts*
Posted

It's a bit unclear to me what's trigger the keydown. As far as I know, a barcode can be interpreted as an image or as a series of characters (usually from using a barcode font).

 

Are you saying you're reading in a string of characters including a barcode and an enter key and using something like VB's SendKeys to send the string to your app? Can you not strip off the Enter key in that case? Isn't there anything to let you know that you're scanning or accepting the barcode data so that you could prevent the Click? Or rather, is there any way that you could prevent the KeyDown (I'm not sure which bit of code - yours or someone else's is causing the event).

 

-Ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Sorry if i confused you. I will explain some more;

 

i scan in barcodes from the back of books;

They are handled EXACTLY as if you were typing them in by hand. By the forms_keydown event.

Each character of the barcode is handled this way.

 

However when a button on the form has the focus and a barcode is scanned in the following happens:

 

The characters of the barcode are handled by the forms_keydown event;

BUT the last character of the barcode, which is the enter key/line feed (whatever u call it) is read NOT by the forms_keydown event like the other characters but by the buttons_click event. Which in turn that activates the code behind it.

 

Hope this helps u all :-)

 

Dan

Posted

Before you execute anything on that button, check to see if you barcode is filled. If not, set the focus on the barcode input box and request for re-scan.

For better way to handle this by using button_KeyPress method. If the KeyPressed is not an enter Chr(13) key then re-direct the input to your barcode box. That way, you get what they scanned in.

  • *Gurus*
Posted
You might have to derive your own class from Button and override one of its key handling procedures (or even WndProc if you want the lowest level) and decide whether or not to call the base method depending on if its the enter key pressed.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...