【问题标题】:changing TabStop in SelectedIndexChanged runs too late在 SelectedIndexChanged 中更改 TabStop 为时已晚
【发布时间】:2014-04-02 02:54:59
【问题描述】:

我有一个包含多个 ComboBox 控件的通话记录表单。

为了加快数据输入速度,某些字段会根据之前的字段选择默认值。其中一些默认值很可能为真,因此我希望在 Tab 键顺序中完全跳过默认字段。

例如,用户名字段设置成员类型字段的值。如果用户是成员,那么成员资格组合框将始终是成员,因此它不应该是 TabStop,但如果用户名是“非成员”,则有几个选择,所以它应该是 TabStop。

在用户名的 SelectedIndexChanged 事件中,我有设置成员资格类型的 TabStop 的逻辑,但因为成员资格类型是用户名设置后的下一个字段,所以为时已晚。选项卡处理已经开始,无论该字段上的 TabStop 是什么,焦点字段都将设置为成员类型。

我不能使用焦点事件,因为它们也会在用户单击该字段时触发,并且它仍然应该激活。应该只使用标签。

【问题讨论】:

    标签: c# asp.net .net winforms


    【解决方案1】:

    经过一番摆弄后,我想出了以下解决方案,但我之前找不到任何发布在此的内容,所以我发布它以防它对其他人有所帮助。我正在表单级别覆盖 ProccessTabKey 以检查 WinForms 是否搞砸了。

    protected override bool ProcessTabKey(bool forward)
    {
        //find out where we are
        Control startingFocus = this.ActiveControl;
    
        //go to the next control
        SelectNextControl(startingFocus, forward, true, true, true);
    
        //find out if we still wanted to go there, (tabstop might have been changed in a SelectedIndexChanged)
        Control newNext = GetNextControl(startingFocus, forward);
        while (!newNext.TabStop)
        {
            newNext = GetNextControl(newNext, forward);
        }
    
        //if we are in the wrong place, move to the right place
        if (this.ActiveControl != newNext)
        {
            newNext.Focus();
        }
    
        return true;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 1970-01-01
      相关资源
      最近更新 更多