如何相对于所选按钮在上,下,左和右按钮上更改焦点? [英] How to change focus on buttons up, down, left and right relative to the button that is selected?

查看:77
本文介绍了如何相对于所选按钮在上,下,左和右按钮上更改焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手。



我不知道怎么解释!!



如何使我的WinForms应用程序了解按钮的位置,例如
想象定位的许多按钮,并且其中一个是select



button1- button2 -button3



button4-(button5)-button6



button7 -button8- button9



我如何按下键盘并跳至(button8)而不是(button6),或者按上键并从(button2)转到。
我如何使Visual Studio理解哪个按钮相对

解决方案

您可以将按钮添加到




注意:

buttons_PreviewKeyDown 事件处理程序对于所有Button / Controls都是相同的。

tlpButtons 是用作按钮/控件容器的TableLayoutControl的名称


已更新为也可与数字键盘一起使用



 私有无效buttons_PreviewKeyDown(对象发送者,PreviewKeyDownEventArgs e)
{
var btn =发送者作为控件;
var pos = tlpButtons.GetPositionFromControl(btn);
bool moveFocus = false;

开关(e.KeyCode){
case Keys.NumPad8:
case Keys.Up:
pos.Row =(pos.Row> 0)? pos.Row-1:tlpButtons.RowCount-1;
moveFocus = true;
休息时间;
case Keys.NumPad2:
case Keys.Down:
pos.Row =(pos.Row<(tlpButtons.RowCount-1))?位置行+ 1:0;
moveFocus = true;
休息时间;
case Keys.NumPad4:
if(pos.Column> 0){
pos.Column-= 1;
}
else {
pos.Column = tlpButtons.ColumnCount-1;
pos.Row = pos.Row> 0? pos.Row-1:tlpButtons.RowCount-1;
}
moveFocus = true;
休息时间;
case Keys.NumPad6:
if(pos.Column<(tlpButtons.ColumnCount-1)){
pos.Column + = 1;
}
else {
pos.Column = 0;
pos.Row =(pos.Row< tlpButtons.RowCount-1)?位置行+ 1:0;
}
moveFocus = true;
休息时间;
}
if(moveFocus){
e.IsInputKey = true;
var ctrl = tlpButtons.GetControlFromPosition(pos.Column,pos.Row);
if(ctrl!= null)this.ActiveControl = ctrl;
}
}


I'm new in programming.

I don't know how to explain well!!.

How can i make my WinForms application understand the button location, for example imagine a number of buttons positioned and that one of them is select

button1- button2 -button3

button4-(button5)-button6

button7 -button8- button9

how can i press down on the keyboard and jump to (button8) and not to (button6), or press upkey and go from (button2). how can i make visual studio understand which button is up or down or left and right relative to the button that is selected?

解决方案

You can add your Buttons to a TableLayoutPanel, so each Button's position is determined by the TableLayoutPanel's (Column:Row) coordinates.

  • Create a TableLayoutPanel (in the below, named tlpButtons) with enough Rows and Columns to contain your Buttons. You can add/remove Rows and Column at run-time, if needed.
  • Select all your Buttons and subscribe to the PreviewKeyDown, using the PropertyGrid in the Form Designer, so that you have just one event handler for all your Buttons (in this code, the event handler is named buttons_PreviewKeyDown).
  • When the Key.Up or Keys.Down are pressed, the PreviewKeyDown handler of the Buttons is invoked. The sender argument references the control that triggered the event, so we cast sender to Control (since only a Control type reference is needed, we don't use any property specific to a derived type, like Button)
  • If we handle the Key pressed, we have to set IsInputKey, otherwise the event is passed on to the control and processed (causing the normal selection to trigger)
  • The TableLayoutPanel's GetPositionFromControl() returns the Row/Column position of this Control.
  • We just set the Row value ± 1, depending on what cursor Key has been pressed (while checking whether the new value is in the [0 : RowsCount] range).
    The GetControlFromPosition() method returns the reference of the control in the new position: we use this reference to set the current ActiveControl.

This is the result:


Note:
the buttons_PreviewKeyDown event handler is the same for all Buttons/Controls.
tlpButtons is the name of the TableLayoutControl used as container for the Buttons/Controls

Updated to also work with a Numeric Pad when either active or inactive.

private void buttons_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    var btn = sender as Control;
    var pos = tlpButtons.GetPositionFromControl(btn);
    bool moveFocus = false;

    switch (e.KeyCode) {
        case Keys.NumPad8:
        case Keys.Up:
            pos.Row = (pos.Row > 0) ? pos.Row - 1 : tlpButtons.RowCount - 1;
            moveFocus = true;
            break;
        case Keys.NumPad2:
        case Keys.Down:
            pos.Row = (pos.Row < (tlpButtons.RowCount - 1)) ? pos.Row + 1 : 0;
            moveFocus = true;
            break;
        case Keys.NumPad4:
            if (pos.Column > 0) {
                pos.Column -= 1;
            }
            else {
                pos.Column = tlpButtons.ColumnCount - 1;
                pos.Row = pos.Row > 0 ? pos.Row - 1 : tlpButtons.RowCount - 1;
            }
            moveFocus = true;
            break;
        case Keys.NumPad6:
            if (pos.Column < (tlpButtons.ColumnCount - 1)) {
                pos.Column += 1;
            }
            else {
                pos.Column = 0;
                pos.Row = (pos.Row < tlpButtons.RowCount - 1) ? pos.Row + 1 : 0;
            }
            moveFocus = true;
            break;
    }
    if (moveFocus) {
        e.IsInputKey = true;
        var ctrl = tlpButtons.GetControlFromPosition(pos.Column, pos.Row);
        if (ctrl != null) this.ActiveControl = ctrl;
    }
}

这篇关于如何相对于所选按钮在上,下,左和右按钮上更改焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆