如何将Button_Click事件处理程序实现到自定义控件?

时间:2018-12-15 15:37:36

标签: c# winforms

我正在尝试创建按钮的行为。我检查了其他stackoverflow问题的解决方案。我不知道我在哪里弄错了。这是代码:

class StartButton : Button
{
    public StartButton() : base()
    {
        this.Location = new Point(100, 100);
        this.Visible = true;
        this.Size = new Size(200, 50);
        this.Click += StartButton_Click;
    }
    void StartButton_Click(object sender, RoutedEventArgs e)
    {

    }
}

Visual返回2个错误: 1)“ StartButton_click”没有重载匹配委托“ EventHandler” 2)找不到类型或名称空间名称“ RoutedEventArgs”(您是否缺少using指令或程序集引用?)

任何解决方案我都会感激

1 个答案:

答案 0 :(得分:1)

此:

internal class User
{
    private readonly string _value;

    internal User([NotNull] string name, [CanBeNull] string value)
    {
        _value = value;
    }

    public bool Exists => !(_value is null);

    public async Task CopyToAsync(Stream stream)
    {
        if (Exists)
        {
            // ReSharper disable once AssignNullToNotNullAttribute - '_value' won't be null here
            using (var valueStream = _value.ToStreamReader()) 
            {
                await valueStream.BaseStream.CopyToAsync(stream);
            }
        }
    }   
}

应为:

void StartButton_Click(object sender, RoutedEventArgs e)
{

}

只需将void StartButton_Click(object sender, EventArgs e) { } 更改为RoutedEventArgs