Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raising an event on user control from parent

I have a user control that has the following overridden event:

protected override void OnKeyDown(KeyEventArgs e)
{
    if (e.KeyCode == Keys.Left )
        DoSomething();
}

When I place the user control on my Main Form, this event is not fired.
How can I access this event from Parent form?

protected override void OnKeyDown(KeyEventArgs e)
{
    e.Handled = true;
    if (e.KeyCode == Keys.Left)
        Move(pt.X,pt.Y);//Move is a function within the usercontrol
    else if (e.KeyCode == Keys.Right)
        Move(pt.X,pt.Y);
   //other conditions
   e.Handled = false;
}

I need the parent to be notified on this event

like image 296
Avrum Avatar asked Jan 24 '26 12:01

Avrum


2 Answers

If I understand correctly, you are trying to invoke the OnKeyDown method of the user control from within the parent form.
This is the main form class:

public class Form1 : Form
{
    private UserControl1 myControl;

    public Form1()
    {
        myControl = new UserControl1();
        Controls.Add(myControl);
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        base.OnKeyDown(e);
        myControl.InvokeOnKeyDown(e);
    }
}

And this is the User Control:

public class UserControl1 : UserControl
{
    protected override void OnKeyDown(KeyEventArgs e)
    {
        base.OnKeyDown(e);
        MessageBox.Show("Key Down Fired!");
    }

    public void InvokeOnKeyDown(KeyEventArgs e)
    {
        OnKeyDown(e);
    }
}

Edit regarding arrow keys: the arrow keys are not normally considered to be input keys and therefore are not passed on to the key methods. To change this you must override the IsInputKey method as such:

 protected override bool IsInputKey(Keys e)
 {
     if (e == Keys.Up || e == Keys.Down ||
         e == Keys.Left || e == Keys.Right) return true;
     return base.IsInputKey(e);
 }
like image 93
Rotem Avatar answered Jan 26 '26 01:01

Rotem


Following your updated question and code example posted, if you want the parent Form to be notified that a move operation took place, you need to create an event in the UserControl and subscribe to it in the parent form. This could be achieved in the following example. Please let me know if this solves your problem, else please post further detail.

Best regards

// Define custom EventArgs to pass into the Move event
public class MoveEventArgs : EventArgs
{
    private Point _movePoint;
    public MoveEventArgs(Point movePoint)
    {
        _movePoint = _movePoint;
    }

    public Point MovePoint { get { return _movePoint; } } 
}

// Define a custom user control that raises an event to subscribers on move
public class MyUserControl : UserControl
{
    public event EventHandler<MoveEventArgs> Moved;

    protected override void OnKeyDown(KeyEventArgs e)
    {
        e.Handled = true;
        if (e.KeyCode == Keys.Left)
        {
            Move(pt.X,pt.Y);//Move is a function within the usercontrol
            OnMoved(pt);
        }   
        else if (e.KeyCode == Keys.Right)
        {
            Move(pt.X,pt.Y);
            OnMoved(pt);
        }
       //other conditions
       e.Handled = false;
    }

    // Raises a custom event, Moved 
    protected void OnMoved(Point movePoint)
    {
        var handler = Moved;
        if (handler != null)
        {
            handler(this, new MoveEventArgs(movePoint);
        }
    }
}

// How to subscribe to the event (and be notified of move)
public class MyParentForm : Form
{
    public MyParentForm()
    {
        InitializeComponent();
        _myUserControl.Moved += new EventHandler<MoveEventArgs>(MyUserControl_Moved);
    }

    private void MyUserControl_Moved(object sender, MoveEventArgs e)
    {
        // e.MovePoint now contains the point that the usercontrol was moved to
        // this event will fire whenever the user presses Left or Right arrow
    }
}
like image 23
Dr. Andrew Burnett-Thompson Avatar answered Jan 26 '26 01:01

Dr. Andrew Burnett-Thompson