简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM