Open ComBobox dropdown list when F12 pressed

A

Andrus

C# .NET 2 WinForms.

I need to open ComboBox dropdown list if user presses F12

I tried the code below (F4 opens dropdown list by default) but got compile
error

Property or indexer 'System.Windows.Forms.KeyEventArgs.KeyCode' cannot be
assigned to -- it is read only

How to fix ?

Andrus.

class MyComboBox : ComboBox {

protected override void OnKeyDown(KeyEventArgs e) {
if (e.KeyCode == Keys.F12)
e.KeyCode = Keys.F4;
base.OnKeyDown(e);
}
 
S

Simon Duvall

Try:

protected override void OnKeyDown(KeyEventArgs e) {
KeyEventArgs k = null;
if (e.KeyCode == Keys.F12)
k = new KeyEventArgs(Keys.F4);
else
k = new KeyEventArgs(e.KeyCode);
base.OnKeyDown(k);
}
 
C

christery

Try:

protected override void OnKeyDown(KeyEventArgs e) {
    KeyEventArgs k = null;
    if (e.KeyCode == Keys.F12)
        k = new KeyEventArgs(Keys.F4);
    else
        k = new KeyEventArgs(e.KeyCode);
    base.OnKeyDown(k);

}










- Visa citerad text -

A Little devil is playing in my head, could I use that system wide?
installing a service/program that took every 10´th char and subst that
with X...

My friend is going to borneo for a vacation... this would be a great
treat (not for him maybe?) to bless his computer with this feature
when he returns...

I did something like this in DOS 20 years ago but that was to
intercept ctrl-c,dont think hooking interrupt 14 (or whatewer it was)
is applyable nowdays...

Will google for a solution, but if u have one it would be nice....
hope he doesnt look in this group...

//CY
 
A

Andrus

Try:
protected override void OnKeyDown(KeyEventArgs e) {
KeyEventArgs k = null;
if (e.KeyCode == Keys.F12)
k = new KeyEventArgs(Keys.F4);
else
k = new KeyEventArgs(e.KeyCode);
base.OnKeyDown(k);
}

I tried but dropdown menu is not opened when I press F12.

Andrus.
 
S

Simon Duvall

You're right! I apologize...

Ok this works. You need a custom combobox control.

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
class Class1 : ComboBox
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.F12)
SendKeys.Send("{F4}");
base.OnKeyDown(e);
}
}
}

~simon
 
M

Marc Gravell

As an alternative strategy to SendKeys etc (following just using an
event-handler):


private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F12)
{
// suppress downstream handling
e.Handled = true;
// toggle drop
comboBox1.DroppedDown = !comboBox1.DroppedDown;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top