How to enable the WinForm button while receiving focus using tabs

Visual Studio 2010, C #

I have ComboBoxwith DropDown, AutoCompleteinstalled in SuggestAppend, and AutoCompleteSource- with ListItems. The user enters data into it until it is correctly entered. If the data matches one of the list items, the button next to the list is disabled.

If the user presses the tab key, the autocomplete function accepts the current sentence. It also moves on to the next control in the tab sequence that is included. Of course, since I want it to go to the forbidden button, I need to turn it on as soon as I confirm this entry.

The problem is that none of the events that I have tried, PreviewKeyDown, LostFocus, SelectedIndexChangedallows me to incorporate the button to complete it and get the focus. It always moves to the next button in tab order, which is always on.

I am ready to leave the button on and give it an error if it is pressed too soon, but I do not want to do this. I also don't want to have special mode flags to keep track of when these controls get focus. The check is more correct, but I'm stuck.

If it SelectedIndexChangedworked when the user made a match, that would be easy. It does not work when the field is cleared or when a match is found.

+5
source share
4 answers

ComboBox, . - :

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.myComboBox1.TheButton = this.button1;

            this.myComboBox1.Items.AddRange( new string[] {
                "Monday",
                "Tuesday",
                "Wednesday",
                "Thursday",
                "Friday",
                "Saturday",
                "Sunday"
            } );

            button1.Enabled = false;
        }
    }

    public class MyComboBox : ComboBox
    {
        public Control TheButton { get; set; }

        public MyComboBox()
        {
        }

        bool IsValidItemSelected
        {
            get { return null != this.SelectedItem; }
        }

        protected override void OnValidated( EventArgs e )
        {
            if ( null != TheButton )
            {
                TheButton.Enabled = this.IsValidItemSelected;
                TheButton.Focus();
            }

            base.OnValidated( e );
        }

        protected override void OnTextChanged( EventArgs e )
        {
            if ( null != TheButton )
            {
                TheButton.Enabled = this.IsValidItemSelected;
            }

            base.OnTextChanged( e );
        }
    }
}
+1
try this :

key_press:

if (e.KeyData == Keys.Enter)
        {
            button2.Enabled = true;
            button2.Focus();
        }
0

(LostFocus, SelectedIndexChanged PreviewKeyDown) "" , .

, , .

.

    private void comboBox1_Validated(object sender, EventArgs e)
    {
        button1.Enabled = true;
        button1.Focus();
    }
0
source

With some thoughts on the other answers here, I came up with a partial senario that works without using autocomplete. A side effect is that the PreviewKeyDown event is fired a second time, and therefore the validation is fired twice. I wonder why ... maybe I should ask another question.

    private void comboBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
      if (e.KeyData == Keys.Tab) {
        if (ValidationRoutine()) {
          e.IsInputKey = true;  //If Validated, signals KeyDown to examine this key
        } //Side effect - This event is called twice when IsInputKey is set to true
      }          
    }

    private void comboBox1_KeyDown(object sender, KeyEventArgs e) {
      if (e.KeyData == Keys.Tab) {
          e.SuppressKeyPress = true; //Stops further processing of the TAB key
          btnEdit.Enabled = true;
          btnEdit.Focus();
      }
    }

As soon as you turn AutoCompleteModeon with any setting other than None, the event KeyDownno longer fires for Tab, this key will be silently used.

0
source

All Articles