【问题标题】:How to navigate through rows/columns of buttons如何浏览按钮的行/列
【发布时间】:2013-09-29 12:55:02
【问题描述】:

这可能是一个微不足道的问题,但是,我还没有找到答案。我正在尝试通过添加键导航(箭头键)的可能性来改进我的 winforms 应用程序。问题是我有许多按行和列排列的按钮,而且导航起来很麻烦。 UP/RIGHT 和 DOWN/LEFT 箭头只增加和减少索引,而不是在指定的方向上移动。到目前为止,我唯一的想法是将按钮索引映射到二维数组并将其用作按钮的引用,但是,我没有成功。有人对我如何实现这一点有一些想法或建议吗? 我在 Visual Studio 2008 和 .NET3.5 上使用 C#

Control[,] MyButtons = new Control[4,3] { {b1,b2,b3},{b4,b5,b6},{b7,b8,b9},{btn_Clear,b0,btn_Cancel}};
   for(int i=0;i<4;i++)
      for (int j = 0; j < 3; j++)
      {
        switch (e.KeyValue)
        {
          case 13: //return
            { e.Handled = false; break; }

          case 39:  //Right
            {
               j++;
               break;
            }
          case 38:  //Up
            {
               i++;
               break;
            }
          case 37: //Left
            {
               j--;
               break;
            }

          case 40: //Down
            {
               i--;
               break;
            }
        }
        if (e.KeyValue != 13)
        {
           e.Handled = true;                         //Don't pass on the keystroke
           MyButtons[i,j].Focus(); // Set the focus to the selected control
          Application.DoEvents();
        }
}

使用此代码,我不断为数组的每个元素收到此错误 无效的排名说明符:预期为 ',' 或 ']'

【问题讨论】:

  • 描述事物的布局。直接上表格?在诸如TableLayoutPanel 之类的容器内?导航...您的意思是当按下箭头时将“更改焦点”到不同的按钮?你有什么尝试...显示一些代码。
  • 按钮直接在表单上,​​以 3x3 格式排列。基本上我要的是一种将焦点更改为箭头键指向的按钮的方法,而不是索引中的下一个按钮

标签: c# visual-studio-2008 .net-3.5


【解决方案1】:

我会给你一个完整的例子,你可以推断你的项目/代码。

表单本身:

这是该表单的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class frmButtons : Form
    {
        private Int32 _selectedButton = 1;
        private Dictionary<Int32, Button> _buttonDict = new Dictionary<Int32, Button>();

        public frmButtons()
        {
            InitializeComponent();

            foreach (Button b in this.Controls.OfType<Button>().Where(x => x.Tag != null))
            {
                Int32 i;
                if (Int32.TryParse(b.Tag.ToString(), out i))
                    _buttonDict.Add(i, b);
            }
            if (_buttonDict.Count > 0)
                _buttonDict[_selectedButton].Focus();
        }

        private void frmButtons_KeyUp(Object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    _selectedButton -= 3;
                    e.Handled = true;
                    break;
                case Keys.Down:
                    _selectedButton += 3;
                    e.Handled = true;
                    break;
                case Keys.Left:
                    _selectedButton -= 1;
                    e.Handled = true;
                    break;
                case Keys.Right:
                    _selectedButton += 1;
                    e.Handled = true;
                    break;
                case Keys.Enter:
                    break;
            }

            if (_selectedButton < 1)
                _selectedButton += 12;
            else if (_selectedButton > 12)
                _selectedButton -= 12;
            _buttonDict[_selectedButton].Focus();
        }
    }
}

我注意到当您按下某个键时出现故障...表单中有一个默认行为,它会尝试处理箭头键以移动到 Tab 键顺序中的下一个键。解决这个问题,你就明白了。抱歉,我没时间处理这个了。

【讨论】:

  • 非常感谢您的回答。我还没有设法让它工作,但我对代码有一些疑问,以供我个人理解。为什么在填充字典时使用按钮标签?列表不是更好吗?谢谢
  • 我假设您的意思是代码中的列表。但这与物理按钮有什么关系?使用按钮上的Tag 属性可为您提供该物理链接。在这个代码示例中,它比实际更具前瞻性。假设在某个时候您需要按钮来告诉您它是哪个数字?使用Tag,就可以了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多