【问题标题】:Preventing ListView from Scrolling/Selecting Items when Pressing Keys防止 ListView 在按键时滚动/选择项目
【发布时间】:2017-12-15 14:26:48
【问题描述】:

我试图阻止我的ListView 在我按下的第一个字母时自动向下滚动/选择项目。

所以我试图覆盖它,但这对我不起作用。

public ref class ExtendedListView : public System::Windows::Forms::ListView
{
public:
    ExtendedListView();

    virtual void KeyPress(KeyEventArgs e) override
    {
        if (e.KeyCode == Keys::W || e.KeyCode == Keys::A || e.KeyCode == Keys::S || e.KeyCode == Keys::D)
        {
            MessageBox::Show("Test");
            return;
        }           
    }

};

(我添加了MessageBox 来测试它是否有效)

【问题讨论】:

    标签: windows winforms listview c++-cli overriding


    【解决方案1】:

    'KeyPress' 事件在列表视图更改之前触发(我厌倦了列表框) 诀窍是定义 2 个变量:

    int selectedindex=0;
    bool goBack=false;
    
    private void listBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode==Keys.B)
        {
            selectedindex = listBox1.SelectedIndex;
            goBack = true;
        }
    }
    
    private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
    {
        if (goBack)
        {
            listBox1.SelectedIndex = selectedindex;
            goBack = false;
        }
    }
    

    例如,此示例阻止了“B”键。 我希望这就是你的意思。

    【讨论】:

    • 这不是个好主意。它纠正了已经完成的事情。更好的是防止它。所以只需使用 SelectedIndexChanging 事件。通过此事件,您可以防止选择更改。
    • 我的朋友列表视图或列表框没有这样的事件
    • 查看包含这个的命名空间!它适用于 System.Web.UI.WebControls
    • 你是对的。奇怪的是,列表视图的 .NET 实现不支持 Windows API 提供的功能:LVN_ITEMCHAMGING。 msdn.microsoft.com/de-de/library/windows/desktop/… 我错了,我确定这个通知也可以在 :NET 表单中使用。
    【解决方案2】:

    只需使用KeyEventArgs 中的SuppressKeyPress

    这应该会导致控件忽略这个键。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-08
      • 1970-01-01
      • 2014-07-14
      • 2018-09-19
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      相关资源
      最近更新 更多