Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic How to receive keyboard event in myUserControl? (Read 483 times)
GrandSon
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 8
Joined: Nov 26th, 2023
How to receive keyboard event in myUserControl?
Nov 26th, 2023 at 3:50am
Print Post  
I implemented user-control(named MyControl) that have Label and RichTextBox.
And I set it ControlNode.Control.

MyControl want keyboard event (e.g. F2 key down).
I can handled KeyDown event where MyControl on the Form directly, but set MyControl to ControlNode.Control MyControl_KeyDown method wasn't called.

Below methods could't my problem.
Code
Select All
DiagramView.Focus();
Diagram.Selection.AddItem(ControlNode);
DiagramView.Select();
MyControl.Focus();
 



Can MyControl on the ControlNode.Control receive Keyboard event directly?If can, how to reveive?
Now, I have passed event to MyControl when ControlNode received KeyDown event. Is there alternative (better) way?

I use MindFusion.Diagramming 7.0.0.
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3173
Joined: Oct 19th, 2005
Re: How to receive keyboard event in myUserControl?
Reply #1 - Nov 26th, 2023 at 5:28pm
Print Post  
Hi,

I've played a bit with focus:

1. The Focus() method doesn't seem to work from form's constructor nor Loaded event; e.g. you could print CanFocus value to verify. The earliest focus worked for me is form's OnShown event:

Code
Select All
protected override void OnShown(EventArgs e)
{
	var control = new MyControl();
	var node = new ControlNode(diagramView, control);
	diagramView.Diagram.Nodes.Add(node);

	Debug.WriteLine(control.CanFocus);
	control.Focus();
} 



2. Then actually the rich-text child control shows it has focus and keyboard events are received by it. So instead of control.KeyDown, handle richtext.KeyDown. If you prefer handling in user control, I've found it's possible to detect F2 in ProcessKeyPreview override:

Code
Select All
public partial class MyControl : UserControl
{
	public MyControl()
	{
		InitializeComponent();
	}

	void richTextBox1_KeyDown(object sender, KeyEventArgs e)
	{
		Debug.WriteLine("richTextBox1_KeyDown");
	}

	const int WM_KEYDOWN = 0x100;
	const int WM_KEYUP = 0x101;

	protected override bool ProcessKeyPreview(ref Message m)
	{
		if (m.Msg == WM_KEYDOWN && (Keys)m.WParam == Keys.F2)
			Debug.WriteLine("F2");
		return base.ProcessKeyPreview(ref m);
	}
} 



Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
GrandSon
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 8
Joined: Nov 26th, 2023
Re: How to receive keyboard event in myUserControl?
Reply #2 - Nov 27th, 2023 at 12:44am
Print Post  
Thanks slavcho.
I focused MyControl in DiagramView_Click, and I key pressed then I could get KeyDown event directly.
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3173
Joined: Oct 19th, 2005
Re: How to receive keyboard event in myUserControl?
Reply #3 - Nov 27th, 2023 at 9:57am
Print Post  
Hi,

I'm receiving direct MyControl_KeyDown events too if the rich-text is disabled. Do you need detecting F2 to enable the textbox and let users type?

So if that's the only situation when you need to handle keyboard event on the user control, I guess UserControl.KeyDown should work fine. If you need to detect keys while rich-text is active, you might have to go through ProcessKeyPreview or handle textbox' own events.

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint