473,835 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Combobox with KeyPress and e.Handled

Tom
I have a ComboBox that I wrote a KeyPress event for. Basically, it looks to
make sure that a numeric key has been pressed; if not, then it does a Beep()
and sets e.Handled to True. This should, as I understand it, cause the
character to 'go away' and not be displayed in the box. I have tried this
on a plain old TextBox and it functions as it should. However, with a
ComboBox the character stays in the box! The code (via the debugger) works
fine - if I enter an 'a', it detects it is not a numeric, sets e.Handled =
TRUE, then exits the subroutine. But no matter what I do, the non-numeric
'a' (or whatever character) displays and stays in the combobox.

Anyone explain this? And is there a workaround? Thanks.

Tom
Nov 20 '05 #1
4 7563
Hi Tom,

It's a bit klugy, but this, inside the keypress event, will work:
If e.KeyChar = "a" Then

SendKeys.Send(" {BACKSPACE}")

End If

HTH,

Bernie Yaeger

"Tom" <to*@nospam.com > wrote in message
news:uv******** ******@TK2MSFTN GP12.phx.gbl...
I have a ComboBox that I wrote a KeyPress event for. Basically, it looks to make sure that a numeric key has been pressed; if not, then it does a Beep() and sets e.Handled to True. This should, as I understand it, cause the
character to 'go away' and not be displayed in the box. I have tried this
on a plain old TextBox and it functions as it should. However, with a
ComboBox the character stays in the box! The code (via the debugger) works fine - if I enter an 'a', it detects it is not a numeric, sets e.Handled = TRUE, then exits the subroutine. But no matter what I do, the non-numeric
'a' (or whatever character) displays and stays in the combobox.

Anyone explain this? And is there a workaround? Thanks.

Tom

Nov 20 '05 #2
Hi Tom,

Does your ComboBox's KeyPress handler actually work?

I plonked a ComboBox on a Form and added handlers for KeyDown and
KeyPress. I put a breakpoint in each one and ran the program.

Neither of them stopped at the breakpoint.

Regards,
Fergus
Nov 20 '05 #3
Hi Fergus,

Mine worked fine - see my code above (even a 'messagebox.sho w' responded as
you would expect).

Bernie

"Fergus Cooney" <fi*****@post.c om> wrote in message
news:uX******** ******@TK2MSFTN GP12.phx.gbl...
Hi Tom,

Does your ComboBox's KeyPress handler actually work?

I plonked a ComboBox on a Form and added handlers for KeyDown and
KeyPress. I put a breakpoint in each one and ran the program.

Neither of them stopped at the breakpoint.

Regards,
Fergus

Nov 20 '05 #4
Hi Tom, Bernie,

I had been experimenting with Release and Debug and was still compiled
Release versions. Lol. There ain't no breakpoints in a Release! ;-)

Unfortunately I discovered a problem with the SendKeys method. This is
what I used:
If e.KeyChar <= "0"c Or e.KeyChar >= "9"c Then
If e.KeyChar <> Chr(8) Then
SendKeys.Send ("{BACKSPACE }")
End If
End If

The inner If is to stop it sending BackSpace forever (as I found out!).
But when I typed Ctrl-C into the editbox, all hell broke loose, but I don't
know why.

Tom, have you had any joy yet?

Regards,
Fergus
Nov 20 '05 #5

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

Similar topics

2
13422
by: Fred Sawtelle | last post by:
I'm trying to trap the Enter key in any of the key events (keypress, keydown, keyup) of the Windows .Net combobox control. Try as I might, I cannot get the event to respond to an Enter key press; nor can I find anything documenting this situation. Help is appreciated. Here's my code. This pops up a messagebox for normal alphanumeric keys, but does nothing at all when Enter is pressed.
0
12291
by: Microsoft | last post by:
Hi All, Any suggestions appreciated. I am trying to do a string search through a comboBox list, have created a simple test which is listed below. The problem I am having is, every time a key is pressed it echoes the char back to the combo twice. For example, if I type D in the combo I get back Dd. The following app works OK if I use KeyPress instead of KeyDown/KeyUp on my computer, but if I run it on another computer (with .NET...
2
4536
by: ross kerr | last post by:
Hi all, I have a control that extends the ComboBox object. It updates the selected item based on what the user enters in the text area. In the OnLeave event of the combobox, the selected index is set to the proper item. However, when accessing its selectedindex later it has reverted to the previous value. The selectedindex value is always one behind what
1
6198
by: Rene | last post by:
Hi, I am running is some problems with the KeyPreview and KeyPress events. The KeyPress event is only triggered when there this an focusable control on the form. When all controls are disabled the Form.KeyPress event does not trigger anymore. A way to 'solve' this is to set the focus to the form, but during testing it happens ofter the keypress event is not getting triggered on keys like
5
2156
by: ross kerr | last post by:
Hi All, I am extending the combobox to create a control that selects an item based on the text the user is typing into the text area of the control. I have an issue that occurs only when i drop down the combo box as the users typing. When the on leave event is fired the value in the selected
0
1068
by: M | last post by:
Hi there, If the Keypreview set to true. How should the form's KeyPress event procedure intercepting keystrokes entered in a ComboBox control. I find in my computer if I set ComboBox's DropdownStyle to Dropdown The form's KeyPress event will not intercepting keystrokes. Only comboBox's KeyPress event procedure be called But some other computers the Form's form's KeyPress event procedure will be called before comboBox's KeyPress Event.
10
9161
by: Tosch | last post by:
I have a combobox where a user can select a zoom factor or enter a zoom factor. I tried to limit entry into the combobox to numbers only by catching the keydown event and setting e.handled = true if any non number characters are entered. But those characters still appear in the combobox. Is this a combobox bug? If yes any workarounds? Tosch
1
2102
by: fiaolle | last post by:
Hi The first set of source code is the class for a combobox in a grid, hopefully. In the second set of code we try to use the combobox class, but the grid is empty. I don't understand how this works. The first set of code I downloaded from internet and when i tried it, it worked fine. But when I changed the second part of the code it started to give me trouble. Before I changed the code they used Datacolumns,Datarows and Datatables and...
0
2062
by: Randy | last post by:
Hi, I have some comboboxes that are created dynamically at run time based on user actions. I found a procedure on a message board to autofill the combobox text from the dataview that the comboboxes are bound to. It works perfectly on the comboboxes that I have created at design time, but the ones that I create runtime behave differently. The problem is that when the user tabs onto the combobox, the box stays blank, which is fine. ...
0
9803
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9652
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10808
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10560
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10233
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9344
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6963
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5636
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5804
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.