使用箭头键控制导航时出现问题 [英] Problem while navigating with in control by arrow keys

查看:74
本文介绍了使用箭头键控制导航时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在使用一个功能,该功能将用于通过向上/向左/向右/向下箭头键和回车键在控件之间进行导航.我的代码如下所示.

Hello,

I am woking on a funtion which will be used for navigating between controls by Up/Left/Right/Down arrow keys and on enter key. my code is like below.

public void NavigateControl(Form form, KeyEventArgs e)
{           
  Control actCntrl = form.ActiveControl;
  if (actCntrl.GetType() == typeof(TextBox))
  {
    TextBox tb = actCntrl as TextBox;
    Control c = tb.Parent;
    if (e.KeyCode == Keys.Right || e.KeyCode == Keys.Down)
    {
      Control next = c.GetNextControl(tb, true);
      if (next != null)
        next.Focus();
    }
    else if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Up)
    {
      Control next = c.GetNextControl(tb, false);
      if (next != null)
        next.Focus();
    }
  }
}


这是工作.但是我对此有一点问题.假设在一个文本框中我有文本(例如``cod project'').现在,我发现文本中缺少一个字符(缺少"e"),因此我试图通过右键选择该位置(在d旁边),但是按照我右边的代码向下键光标移至下一个控件.所以我不能在任何箭头键上的文本框文本内移动....我不想使用鼠标. Combobox,datetimepicker等也发生了同样的事情.
如何解决此问题?
请帮助我.


It is working. But I have a little problem with this. Suppose in a textbox I have text(e.g ''cod project''). Now I have found that there is a character misssing in the text(''e'' is missing) so I''m trying to move to that position(next to the d) by right key, but as per my code on right key down cursor moves to next control. So I can''t move within text of textbox on any arrow key.... I don''t want to use the mouse. The same thing is happening with Combobox, datetimepicker etc..
What to do to remove this problem?
Please help me.

推荐答案

这很容易解决.我真的非常喜欢您不想使用鼠标.这是认真的谈话.真实的UI应该能够完全导航而无需鼠标.不幸的是,许多应用程序都无法遵循这个简单的规则-我谨此致敬,Yatin.

首先,请注意您对不可能的事物的要求. (我知道这不是您的真实意图,而是正式地...).让我们来看看.考虑您专注于TextBox.从文本编辑器的角度来看,键Left应该从您想要的表单导航的角度移到左边的字符,它应该转移焦点.不能这样工作,同意.

首先,在任何情况下都必须放弃NavigateControl.见下文.可能有不同的分辨率.

#1.放弃此导航-这是非标准的,会使用户感到困惑.认真.

#2.将所有控件放入数据网格视图"中-它将提供类似的导航;相反:可能不适合您的设计,某些用户可能会觉得有些异样.

#3.同意使用Ctrl + Right进行导航,同意使用Right-进行常规控件处理; Ctrl + Up和Up等.请参见下面的技术说明.

最后解决方案的实现:

This is easy to fix. What I really, really like is that you don''t want to use mouse. This is serious talking. Real UI should be able to navigate fully without a mouse. Too bad many applications fail to follow this simple rule -- my respect, Yatin.

First of all, pay attention you kind of asking for the impossible. (I know this is not your real intention, but formally...). Let''s see. Consider you focus on TextBox. The key Left, from the stand point of text editor should shift to the left character, from the standpoint of your intended form navigation, it should shift focus. It cannot work this way, agree.

First, give up NavigateControl at all, in all cases. See below. There can be different resolutions.

#1. Give up this navigation -- it is non-standard, will confuse the user. Seriously.

#2. Put all your controls in Data Grid View -- it will provide similar navigation; contra: may not fit your design, will look to exotic to some users.

#3. Agree to use Ctrl+Right for navigation, Right -- for normal control handling; Ctrl+Up and Up, etc. See explanation of the technique below.

Implementation for last solution:

//make universal navigation handler:
enum Direction
   { Left, Top, Right, Left, Down, Next, Prev, } //whatever

//...

void Navigate(Control current, Direction direction) {/* ... */}
Direction Translate(KeyboardEventArgs event)  {/* ... */}
void ProcessEvent(KeyboardEventArgs event) {
    Direction direction = Translate(event);
    switch (direction) {
    //...
    newFocusControl.Focus(); //something like this after calculations
}



ProcessEvent的实现应考虑带有前缀的键(我建议使用Ctrl +),以将导航与常规控制键盘的处理分开.

现在,对于所有控件(分别以递归和编程方式),设置键盘事件处理程序:



You implementation of ProcessEvent should consider keys with prefix (I suggested Ctrl+) to isolate navigation from normal control keyboard processing.

Now, for all controls, individually recursively and programmatically, setup keyboard event handlers:

void SetupKeyboardHandler(control target) {
    target.KeyDown += delegate(
        object sender, KeyboardEventArgs eventArgs) {
            ProcessEvent(eventArgs);
    } //target.KeyDown
    target.KeyUp += delegate(
        object sender, KeyboardEventArgs eventArgs) {
            ProcessEvent(eventArgs);
    } //target.KeyDown
} //SetupKeyboardHandler

static void SetupKeyboardHandlers(Form form) {
    //...
    foreach(Control control in ControlSet)
        SetupKeyboardHandlder(control);
} //SetupKeyboardHandlers



在这里,我简化了SetupKeyboardHandlers.在现实生活中,您应该递归收集表单上的所有控件(使用Control.Controls属性).为了简化起见,我假设您将所有它们收集在某个集合ControlSet中,这并不是最佳选择(尽管谁在乎?).更好的解决方案应具有递归功能,并直接在内部递归中进行每个SetupKeyboardHandlder调用.

这不太难.但是,我本人会选择解决方案1.



Here, I simplified SetupKeyboardHandlers. In real life you should collect all controls on the form recursively (using Control.Controls property). For simplification, I assume you collected all of them in some collection ControlSet, which is not optimal (who cares though?). Better solution should make recursive function and do every SetupKeyboardHandlder call directly insider recursion.

This is not too hard. However, I would personally have chosen solution #1.


我可以看到您正在尝试做的事情,不幸的是,这是您尝试扭曲法线时遇到的问题UI体验将成为用户无法期望的事情:如您所见,他们希望左右键在文本框中向左和向右移动.

您可以执行以下操作:
1)坚持仅在按下CTRL键的情况下,导航控件才处于活动状态.对于用户而言,这不那么令人困惑,因为光标键现在在所有应用程序中都相同.
2)使用光标键使CTRL键具有较旧的常规功能.对用户来说太可怕了,对您来说并没有很多乐趣.

您确实意识到可以使用TAB和CTRL + TAB在输入字段之间按定义的顺序移动,不是吗?并且您可以为输入控件分配快捷键(因此,用户在名称"字段中键入ALT + N,在位置"中键入ALT + P,依此类推)?
这些至少是整个UI体验中常见的,因此用户不会感到困惑.
I can see what you are trying to do, and unfortunately, this is the kind of problem you get when you try to twist the normal UI experience into something the user is not going to expect: they expect the left and right keys to move left and right within a text box, as you have seen.

There are a few things you can do:
1) Insist that the navigation controls are only active when the CTRL key is pressed. This is less confusing for the user because the cursor keys now do the same in all applications.
2) Make the CTRL key have the old, normal functionality with teh cursor keys. Horrible for the user, and not a lot of fun for you.

You do realise that you can use TAB and CTRL+TAB to move between input fields in a defined order, don''t you? And that you can assign short-cut keys to input controls (so the user types ALT+N for the "Name" field, ALT+P for the "Position" and so on)?
These are at least common to the entire UI experience, so users wouldn''t get confused.


这篇关于使用箭头键控制导航时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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