473,805 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Restricting textbox input

mdb
I'm trying to restrict the characters that are valid in a textBox, so I
derived from System.Windows. Forms.TextBox, and overrode WndProc, catching
the key up/down messages... Everything works except the arrow keys - the
text cursor never moves. Why won't the arrows work? I know I'm catching
the correct WParam values...

protected override void WndProc(ref Message m)
{
if ((m.Msg == 256) || (m.Msg == 258))
{
int v = (int)m.WParam;

if ((v > (int)'0') && (v < (int)'9'))
{
base.WndProc (ref m);
return;
}

switch(v)
{
case 8: // Backspace
case 13: // Enter
case 33: // PgUp
case 34: // PgDn
case 35: // End
case 36: // Home
case 37: // Left
case 38: // Up
case 39: // Right
case 40: // Down
case 46: // Delete
base.WndProc(re f m);
break;
default:
break;
}
}
else
{
base.WndProc(re f m);
}
}
--
-mdb
Nov 17 '05 #1
1 2425
HI,

Instead of creating a new class just use the TextBox.KeyPres s event, there
you will get two properties
1- Handled that indicate that the key was hanbdle and it should be igonred.
2- KeyChar which is a char, y ou can use Char.Is(Control |Digit|Letter|e tc )
as needed.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"mdb" <m_b_r_a_y@c_t_ i_u_s_a__d0t__c om> wrote in message
news:Xn******** *************** *****@207.46.24 8.16...
I'm trying to restrict the characters that are valid in a textBox, so I
derived from System.Windows. Forms.TextBox, and overrode WndProc, catching
the key up/down messages... Everything works except the arrow keys - the
text cursor never moves. Why won't the arrows work? I know I'm catching
the correct WParam values...

protected override void WndProc(ref Message m)
{
if ((m.Msg == 256) || (m.Msg == 258))
{
int v = (int)m.WParam;

if ((v > (int)'0') && (v < (int)'9'))
{
base.WndProc (ref m);
return;
}

switch(v)
{
case 8: // Backspace
case 13: // Enter
case 33: // PgUp
case 34: // PgDn
case 35: // End
case 36: // Home
case 37: // Left
case 38: // Up
case 39: // Right
case 40: // Down
case 46: // Delete
base.WndProc(re f m);
break;
default:
break;
}
}
else
{
base.WndProc(re f m);
}
}
--
-mdb

Nov 17 '05 #2

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

Similar topics

3
4664
by: volume | last post by:
Restricting a windows textbox (edit item) to digits only. Is there a windows option, using .NET C#, to only allow a user to enter digits ONLY? If so, what is the flag or setting? If no, what is the best method to manually and robustly do it? I have a windows form with an editbox that I only want user's to enter digits. Thanks in advance.
6
3727
by: Altramagnus | last post by:
I have searched throught the newsgroups on how to restrict entry in textboxes, for example, I only want the textbox to only accept numberic. The standard answer is to use the KeyPress event. However, this do not address the paste event User is still able to copy and paste any characters. How do I create a textbox so that regardless what I do ( including cut and paste ),
9
10122
by: Jerry | last post by:
In limiting textbox input to 500 characters I would like to include a dynamic count of characters input while the user is typing into a textbox. This would obviously be a client side control, possibly a custom validator with a function written in javascript. Has anyone done this? Does someone have an example? Regards
2
1251
by: W. Adam | last post by:
I have a form which displays info. On top of it there is a text box for input box which lets you put number from 1 thu 112.I need to verify that the number entered by the user is within the range of 1-12. If it isn't, notify the user and set the focus back to the text box. Do not allow processing to continue until a valid entry is entered in the textbox. How can i achive that Thanks in advance Here is my code whats wrong with it?
2
2212
by: hollykatong | last post by:
hello, is there a way to restricting a textbox input wherein it will only accept two integer with decimal places? for example if i enter 90 or 90.1234 it will allow me and when i try to type 890 or 890.7832 it will not accept it. any help will be greatly appreciated. :)
0
1036
by: lagu2653 | last post by:
I've made a custom textbox with a certain background. In order to the textbox higher than normal I set the property multiline to true. The row above and below are showed since the textbox is higher than normal. But I only want the users to be able to edit the middle row. Also the cursor is positioned on the top row. I don't want the users to be able to move it away from the middle row. How do I accomplish this. /Lars
8
8748
by: sneddo | last post by:
Ok I am trying to do the above, I have got a script that will restrict the length but it requires the user to enter the field and hit a key, before it will work. This would normaly be find, but the title field gets its information from a previouse page so its value can easily be over 40 chars. (I can not restrict the length on the previouse page.) The major dificulty is that there is no form on the aspx page, and I do not have access to...
2
3114
by: =?Utf-8?B?R3JlZw==?= | last post by:
I'm from an Access background and I'm working with VB.Net. In MS Access I can restrict data-entry into my fields on a form using the Format property. Is there an equivilant in VB.Net. I want to restrict data-entry into my text boxes to numeric only. 0-9 only. I don't want a decimal place. Thanks
1
2471
by: rakeshnair | last post by:
i wrote a code in jsp to create dynamic table..the problem is i need data base connection when cursor moves from one cell to other... eg...when i enter product id in the first cell, the product name and its rate should come in the next two cells...it should take from the data base... it's my code........ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <%@ page import="java.sql.*"...
0
9596
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
10614
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
10369
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
10109
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
9186
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...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5544
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
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3847
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.