【问题标题】:inheritance and event handling for non-instantiated object C#非实例化对象的继承和事件处理 C#
【发布时间】:2012-12-17 11:26:26
【问题描述】:

我在弄清楚如何创建一个继承类来扩展 Windows 窗体控件以始终拥有一个事件处理程序来处理该对象的每个实例的按键事件时遇到了一点麻烦。

我可能解释得很糟糕。本质上,我想在 Windows 窗体中扩展 DatagridView 类,以便始终为我扩展的 DatagridView 类的任何实例化对象提供 keyPress 事件处理程序。

我想知道是否有可能有一个事件处理程序来监听按键并使用类似于我在下面编写的代码来处理它们:

    private void dgvObject_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (Char.IsLetterOrDigit(e.KeyChar))
        {
            //start the loop at the currently selected row in the datagridview
            for (int i = dgvObject.SelectedRows[0].Index; i < dgvObject.Rows.Count; i++)
            {
                //will only evaluate to true when the current index has iterated above above the 
                //selected rows index number AND the key press event argument matches the first character of the current row
                // character of the 
                if (i > dgvObject.SelectedRows[0].Index && dgvObject.Rows[i].Cells[1].FormattedValue
                    .ToString().StartsWith(e.KeyChar.ToString(), true, CultureInfo.InvariantCulture))
                {
                    //selects current iteration as the selected row
                    dgvObject.Rows[i].Selected = true;
                    //scrolls datagridview to selected row
                    dgvObject.FirstDisplayedScrollingRowIndex = dgvObject.SelectedRows[0].Index;
                    //break out of loop as I want to select the first result that matches
                    break;
                }
            }
        }
    }

上面的代码简单地选择下一行,该行以触发时按键事件在其事件参数中的任何字符开头。我想知道我是否可以将其作为始终存在的继承处理程序的原因。我认为这比在我的 Windows 窗体中为每个单独的 DatagridView 对象显式创建数百个处理程序要好。如果我的想法是错误的,请随时纠正我!无论如何,感谢您的任何意见。

我已经用 C# 编程大约 5 个月了,我还在学习中 =)

【问题讨论】:

  • 因为“我认为这比在我的 Windows 窗体中为每个单独的 DatagridView 对象显式创建数百个处理程序更好”对我来说似乎很奇怪,您也可以简单地使用与所有处理程序相同的方法DatagridView 事件。如果您的意思不止一种形式,请参阅 Jamiec 的回答。
  • 嗯,我真的应该想到这一点。你是对的,我可以简单地将我的所有事件订阅到相同的方法。我不太确定为什么那个人会溜走我。也就是说,Jamiec 的回答与我所问的最接近。感谢您的反馈!

标签: c# winforms inheritance datagridview event-handling


【解决方案1】:

是的,在你的继承类中只覆盖OnKeyPress,之后你应该记得调用base.OnKeyPress

protected override OnKeyPress(KeyPressEventArgs e)
{
   .. all your code

   base.OnKeyPress(e); // to ensure external event handlers are called
}

【讨论】:

  • 如果您使用了密钥,请不要调用 base.OnKeyPress()。请注意,现在输入数据不再实用。
  • @HansPassant - [需要引用]
  • 嗯,没有引用就很明显了。如果他想使用一个键来选择一行,那么该键不应该也改变行中的一个单元格。
  • 啊,谢谢,这正是我想要做的。这比我想象的要容易得多。如果我也想覆盖其他事件,这很有用。
  • @HansPassant - 事实上,我认为这完全是错误When overriding OnKeyPress in a derived class, be sure to call the base class's OnKeyPress method so that registered delegates receive the event. - 来源docs
【解决方案2】:

您可以通过覆盖 ProcessCmdKey 来捕获所有按键甚至组合:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
{
    if (keyData == (Keys.Control | Keys.F)) 
    {
        //your code here
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多