简体   繁体   English

你如何让箭头键在WinForms TableLayoutPanel中移动你的焦点?

[英]How do you let the arrow keys move your focus around in WinForms TableLayoutPanel?

It seems that the default is the up and left keys go to the previous tabstop, and the down/right keys go to the next tabstop. 似乎默认是向上和向左键转到上一个tabstop,向下/向右键转到下一个tabstop。 I've got a TableLayoutPanel each with one button in it. 我有一个TableLayoutPanel,每个都有一个按钮。 Is there an easy way to make the arrow keys focus the button in the cell to the corresponding direction in the TableLayoutPanel instead of just following the tabstops? 是否有一种简单的方法可以使箭头键将单元格中的按钮聚焦到TableLayoutPanel中的相应方向而不是仅仅跟随tabstops?

Yes, no problem. 是没有问题。 Just create your own TableLayoutPanel override and have it process the cursor keys to get what you want. 只需创建自己的TableLayoutPanel覆盖并让它处理光标键即可获得所需内容。 Add a new class to your project and paste the code shown below. 在项目中添加一个新类并粘贴下面显示的代码。 Compile. 编译。 Drop the new panel from the top of the toolbox onto your form, replacing the old one. 将新面板从工具箱顶部拖放到表单上,替换旧表单。

using System;
using System.Windows.Forms;

class MyLayoutPanel : TableLayoutPanel {
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
        var ctl = this.FindForm().ActiveControl;
        if (ctl.Parent == this) {
            int col = this.GetColumn(ctl);
            int row = this.GetRow(ctl);
            if (keyData == Keys.Left && col > 0) {
                var newctl = this.GetControlFromPosition(col - 1, row);
                if (newctl != null) newctl.Focus();
                return true;
            }
            // etc..
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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