Search code examples
c#winforms.net-coreeventsraiseevent

Raising form event


I have been finding that “Windows Forms App (.NET Core)” projects lack the functionality of normal “.NET framework” apps. I'm specifically using .NET Core 3.1.

I want to be able to raise a form event on an object, but cannot find a way to do this. In my example, I want to call click on a System.Windows.Forms.TextBox. I know calling TextBox.Focus() will essentially emulate the behavior, but that's not the point.

Calling an event handler for the forms apps object does not do the job either. It only calls my custom code, not the actual base event handlers. And Control.RaiseEvent does not exist in .NET core. I don't think Control.Invoke can do the job either, but I haven't tested.


Solution

  • The answer is that control events are callable through subclassed controls. For example

    public class TestPictureBox : PictureBox
    {
        public void CallClick(EventArgs e = null) => base.OnClick(e ?? EventArgs.Empty);
        public void CallResize(EventArgs e = null) => base.OnResize(e ?? EventArgs.Empty);
    }
    

    With these example functions you can raise the control’s events.