473,569 Members | 2,844 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Arrow Keys neglected on User Control

/*
I have problems detecting the Arrow Keys on a User Control. A control
derived from System.Windows. Forms.Control neglects 'bare' Arrow Keys
but does react on the combination <Altor <Ctrl+ Arrow Key. The code
below shows what I mean. How can I cure this? (excuse me for the line
breaks)
*/


//class KeyTest
class KeyTest:System. Windows.Forms.C ontrol
{
//data member label
System.Windows. Forms.Label label=new System.Windows. Forms.Label();
//constructor
KeyTest()
{
Controls.Add(la bel);
KeyDown+=OnKeyD own;
Text="Arrow Keys are Special?";
label.Text="\nP ress <1>...";
Dock=System.Win dows.Forms.Dock Style.Fill;
label.Dock=Syst em.Windows.Form s.DockStyle.Fil l;
}
//OnKeyDown
void OnKeyDown(objec t a,System.Window s.Forms.KeyEven tArgs b)
{
switch(b.KeyCod e)
{
case System.Windows. Forms.Keys.D1:
label.Text+="\n You pressed 1."+
"\n\nNow hold down <Altor <Ctrland press the Arrow Up Key...";
break;
case System.Windows. Forms.Keys.Up:
label.Text+="\n You pressed the Arrow Up Key."+
"\n\nNow release <Altor <Ctrland press the Arrow Up Key
again..."+
"\n\nIt doesn't work!"+
"\nPress <Escor <Spaceto Quit";
break;
case System.Windows. Forms.Keys.Esca pe:
System.Windows. Forms.Applicati on.Exit();
break;
case System.Windows. Forms.Keys.Spac e:
System.Windows. Forms.Applicati on.Exit();
break;
}
}
//Main
[System.STAThrea d]
static void Main()
{
System.Windows. Forms.Form form=new System.Windows. Forms.Form();
form.Controls.A dd(new KeyTest());
System.Windows. Forms.Applicati on.Run(form);
}
}

Oct 26 '06 #1
1 10130
override IsInputKey and do something like this:

protected override bool IsInputKey(Syst em.Windows.Form s.Keys
keyData)
{
switch (keyData & Keys.KeyCode)
{
case Keys.Up:
return true;
case Keys.Down:
return true;
case Keys.Right:
return true;
case Keys.Left:
return true;
default:
return base.IsInputKey (keyData);
}

}

--

Regards, Peter

"Martijn Mulder" <i@mskrev i meddelandet
news:45******** *************** @news.wanadoo.n l...
/*
I have problems detecting the Arrow Keys on a User Control. A control
derived from System.Windows. Forms.Control neglects 'bare' Arrow Keys
but does react on the combination <Altor <Ctrl+ Arrow Key. The code
below shows what I mean. How can I cure this? (excuse me for the line
breaks)
*/


//class KeyTest
class KeyTest:System. Windows.Forms.C ontrol
{
//data member label
System.Windows. Forms.Label label=new System.Windows. Forms.Label();
//constructor
KeyTest()
{
Controls.Add(la bel);
KeyDown+=OnKeyD own;
Text="Arrow Keys are Special?";
label.Text="\nP ress <1>...";
Dock=System.Win dows.Forms.Dock Style.Fill;
label.Dock=Syst em.Windows.Form s.DockStyle.Fil l;
}
//OnKeyDown
void OnKeyDown(objec t a,System.Window s.Forms.KeyEven tArgs b)
{
switch(b.KeyCod e)
{
case System.Windows. Forms.Keys.D1:
label.Text+="\n You pressed 1."+
"\n\nNow hold down <Altor <Ctrland press the Arrow Up Key...";
break;
case System.Windows. Forms.Keys.Up:
label.Text+="\n You pressed the Arrow Up Key."+
"\n\nNow release <Altor <Ctrland press the Arrow Up Key
again..."+
"\n\nIt doesn't work!"+
"\nPress <Escor <Spaceto Quit";
break;
case System.Windows. Forms.Keys.Esca pe:
System.Windows. Forms.Applicati on.Exit();
break;
case System.Windows. Forms.Keys.Spac e:
System.Windows. Forms.Applicati on.Exit();
break;
}
}
//Main
[System.STAThrea d]
static void Main()
{
System.Windows. Forms.Form form=new System.Windows. Forms.Form();
form.Controls.A dd(new KeyTest());
System.Windows. Forms.Applicati on.Run(form);
}
}

Oct 27 '06 #2

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

Similar topics

2
8097
by: Darren Oakey | last post by:
ok - the problem - I made a simple breakout game out of a form, just painting the background - and using keydown for left and right arrow keys to control the bat - worked fine. I then moved all the code into a user control, put it on the form. Now I don't get keydown events when I press an arrow key - on either the form or the usercontrol...
7
5763
by: Seash | last post by:
Hi friends , here is the sample code private void txtbox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if(e.KeyChar == 13) //enter { txtbox2.Focus(); }
4
5848
by: Neil Wallace | last post by:
Hi there, I have an application in which a grid of 100 or more buttons are put on a form in columns of 10. All the buttons are within a panel. They are added in runtime, and so they adopt a sensible tab value. The tab key moves the focus down the column one by one, and the up and down arrow keys work well.
11
2783
by: Rlrcstr | last post by:
How can you detect when an arrow key gets pressed? Doesn't seem to trigger a KeyPress or KeyDown event. Thanks. Jerry
2
5496
by: Vincent | last post by:
Hi, I have a user control that needs to trap the arrow keys to move items around internally. However, using the arrow keys will move the focus to another control on the form hosting the user control. How do I stop this? Vincent.
0
2907
by: Martijn Mulder | last post by:
/* I override IsInputKey() to direct the Arrow Keys (Cursor Keys) to my custom System.Windows.Forms.Control. But, holding down the Shift-Key prevents the Arrow Keys from coming through. How can I intercept the Arrow Keys when holding down the Shift Key? */
4
3269
by: boopsboops | last post by:
Hi thescripts people, I hope I'm in the right forum for Visual Basic Dotnet (VS 2005). I am trying to make a custom control in which you can nudge a point around using the arrow keys. Actually, the control is meant to be a simple drawing program. To test it out I have put the control on a Windows form which also contains several buttons. I...
2
3310
by: Charles Law | last post by:
I'll kick myself when you tell me, but ... I have a user control on a form, and I want the user control to see the arrow keys when I press them. If I press just about any other key the control's KeyDown event is fired, but not when I press a direction key. I want to see them in the KeyDown event so that I can respond as soon as the key is...
2
4434
by: Artie | last post by:
Hi, I want to force the user to only use the drop down and calendar to select a date in the DatetimePicker control. I do NOT want them to select the control, then use the up and down arrow keys to change the date. How can I prevent them using the arrow keys?
0
7695
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...
1
7668
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...
0
7964
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...
1
5509
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...
0
5218
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...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.