How can be triggered Keydown event of a Button?

G

Guest

I ttry this:

Button MyButton...

MyButton.KeyDown();

but I get this

"The event 'System.Windows.Forms.Control.KeyDown' can only appear on the
left hand side of += or -="

Thanks

Frankie
 
S

Simon Hart

You need to subscribe to the KeyDown event, you can't call it, ie:-
myButton.KeyDown += new KeyEventHandler(myhandler);

myhandler needs the KeyDown event signature.

Simon.
 
S

Sergey Bogdanov

I don't know why do you want to call it but take a look at this sample:

InvokeKeyEventHandler(button1, "KeyDown", Keys.A);

....

private void InvokeKeyEventHandler(Control c, string eventName, Keys k)
{
FieldInfo fi = null;
Type typeControl = c.GetType();
// find correct FieldInfo for passed eventName
while (typeControl != typeof(System.Object))
{
fi = typeControl.GetField(eventName, BindingFlags.NonPublic |
BindingFlags.Instance);
if (fi != null) break;
typeControl = typeControl.BaseType;
}

if (fi == null) throw new ArgumentException("Impossible to find: " +
eventName);

// invokes each handler in the invocation list
foreach(KeyEventHandler keh in (fi.GetValue(c) as
MulticastDelegate).GetInvocationList())
{
keh.Invoke(c, new KeyEventArgs(k));
}
}


However, I'd recommend you to use OpenNETCF.Windows.Forms.SendKeys which
works like System.Windows.Forms.SendKeys:
http://msdn2.microsoft.com/library/k3w7761b.aspx
 
G

Guest

I would like to force the button to stay in "button down" state until the
next click.
Do you have any idea how can I reach it?

My very first idea was: to send KeyDown "message" without KeyUp. That's why
I asked the question in the subject.

Thank you for your answer

Frankie

Sergey Bogdanov said:
I don't know why do you want to call it but take a look at this sample:

InvokeKeyEventHandler(button1, "KeyDown", Keys.A);

....

private void InvokeKeyEventHandler(Control c, string eventName, Keys k)
{
FieldInfo fi = null;
Type typeControl = c.GetType();
// find correct FieldInfo for passed eventName
while (typeControl != typeof(System.Object))
{
fi = typeControl.GetField(eventName, BindingFlags.NonPublic |
BindingFlags.Instance);
if (fi != null) break;
typeControl = typeControl.BaseType;
}

if (fi == null) throw new ArgumentException("Impossible to find: " +
eventName);

// invokes each handler in the invocation list
foreach(KeyEventHandler keh in (fi.GetValue(c) as
MulticastDelegate).GetInvocationList())
{
keh.Invoke(c, new KeyEventArgs(k));
}
}


However, I'd recommend you to use OpenNETCF.Windows.Forms.SendKeys which
works like System.Windows.Forms.SendKeys:
http://msdn2.microsoft.com/library/k3w7761b.aspx

--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com

I ttry this:

Button MyButton...

MyButton.KeyDown();

but I get this

"The event 'System.Windows.Forms.Control.KeyDown' can only appear on the
left hand side of += or -="

Thanks

Frankie
 

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