This is a migrated thread and some comments may be shown as answers.

Autoselect value with tab key

3 Answers 104 Views
MultiColumn ComboBox
This is a migrated thread and some comments may be shown as answers.
Francois
Top achievements
Rank 1
Francois asked on 13 Oct 2011, 02:03 PM
The MultiColumnComboBox currently selects the correct value in the popup when using the ENTER key. I want to achieve the same behavior with the TAB key but I haven't succeeded.

Also, if the user's text is not associated to any of the items, I want to empty the textbox so that he sees that's not a valid option.

Thanks!

Sample code :

using System.Collections.Generic;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.Data;
using Telerik.WinControls.UI;
 
namespace MultiColumnComboAutoSelectWithTab
{
    public class RandomObject
    {
        public int Id { get; set; }
        public string FirstStuff { get; set; }
        public string SecondStuff { get; set; }
 
        public RandomObject(int id, string first, string second)
        {
            this.Id = id;
            this.FirstStuff = first;
            this.SecondStuff = second;
        }
 
    }
    public class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 
            InitializeCustomComponent();
 
            var lst = new List<RandomObject>();
            lst.Add(new RandomObject(1, "Albert", "Stuff"));
            lst.Add(new RandomObject(2, "Alphonse", "Stuff"));
 
            this.radMultiColumnComboBox1.DataSource = lst;
        }
 
        private void InitializeCustomComponent()
        {
            this.radMultiColumnComboBox1.DisplayMember = "FirstStuff";
            this.radMultiColumnComboBox1.ValueMember = "Id";
 
            this.radMultiColumnComboBox1.EditorControl.Columns.Add(new GridViewTextBoxColumn("Id", "Id"));
            this.radMultiColumnComboBox1.EditorControl.Columns["Id"].IsVisible = false;
            this.radMultiColumnComboBox1.EditorControl.Columns.Add(new GridViewTextBoxColumn("FirstStuff", "FirstStuff"));
            this.radMultiColumnComboBox1.EditorControl.Columns["FirstStuff"].HeaderText = "First stuff";
            this.radMultiColumnComboBox1.EditorControl.Columns.Add(new GridViewTextBoxColumn("SecondStuff", "SecondStuff"));
            this.radMultiColumnComboBox1.EditorControl.Columns["SecondStuff"].HeaderText = "SecondStuff";
 
            this.radMultiColumnComboBox1.AutoFilter = true;
            this.radMultiColumnComboBox1.DropDownStyle = RadDropDownStyle.DropDown;
            this.radMultiColumnComboBox1.AutoSize = true;
            this.radMultiColumnComboBox1.AutoSizeDropDownToBestFit = true;
 
            var filter = new FilterDescriptor(this.radMultiColumnComboBox1.DisplayMember, FilterOperator.StartsWith, string.Empty);
            this.radMultiColumnComboBox1.EditorControl.MasterTemplate.FilterDescriptors.Add(filter);
        }
 
        /// <summary>
        /// Variable nécessaire au concepteur.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Nettoyage des ressources utilisées.
        /// </summary>
        /// <param name="disposing">true si les ressources managées doivent être supprimées ; sinon, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region AutoGeneratedStuff
 
        private void InitializeComponent()
        {
            this.radMultiColumnComboBox1 = new Telerik.WinControls.UI.RadMultiColumnComboBox();
            this.radTextBox1 = new Telerik.WinControls.UI.RadTextBox();
            ((System.ComponentModel.ISupportInitialize)(this.radMultiColumnComboBox1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.radTextBox1)).BeginInit();
            this.SuspendLayout();
            //
            // radMultiColumnComboBox1
            //
            //
            // radMultiColumnComboBox1.NestedRadGridView
            //
            this.radMultiColumnComboBox1.EditorControl.BackColor = System.Drawing.SystemColors.Window;
            this.radMultiColumnComboBox1.EditorControl.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.radMultiColumnComboBox1.EditorControl.ForeColor = System.Drawing.SystemColors.ControlText;
            this.radMultiColumnComboBox1.EditorControl.Location = new System.Drawing.Point(0, 0);
            //
            //
            //
            this.radMultiColumnComboBox1.EditorControl.MasterTemplate.AllowAddNewRow = false;
            this.radMultiColumnComboBox1.EditorControl.MasterTemplate.AllowCellContextMenu = false;
            this.radMultiColumnComboBox1.EditorControl.MasterTemplate.AllowColumnChooser = false;
            this.radMultiColumnComboBox1.EditorControl.MasterTemplate.EnableGrouping = false;
            this.radMultiColumnComboBox1.EditorControl.MasterTemplate.ShowFilteringRow = false;
            this.radMultiColumnComboBox1.EditorControl.Name = "NestedRadGridView";
            this.radMultiColumnComboBox1.EditorControl.ReadOnly = true;
            this.radMultiColumnComboBox1.EditorControl.ShowGroupPanel = false;
            this.radMultiColumnComboBox1.EditorControl.Size = new System.Drawing.Size(240, 150);
            this.radMultiColumnComboBox1.EditorControl.TabIndex = 0;
            this.radMultiColumnComboBox1.Location = new System.Drawing.Point(81, 78);
            this.radMultiColumnComboBox1.Name = "radMultiColumnComboBox1";
            //
            //
            //
            this.radMultiColumnComboBox1.RootElement.AutoSizeMode = Telerik.WinControls.RadAutoSizeMode.WrapAroundChildren;
            this.radMultiColumnComboBox1.Size = new System.Drawing.Size(369, 20);
            this.radMultiColumnComboBox1.TabIndex = 0;
            this.radMultiColumnComboBox1.TabStop = false;
            this.radMultiColumnComboBox1.Text = "radMultiColumnComboBox1";
            //
            // radTextBox1
            //
            this.radTextBox1.Location = new System.Drawing.Point(81, 131);
            this.radTextBox1.Name = "radTextBox1";
            this.radTextBox1.Size = new System.Drawing.Size(330, 20);
            this.radTextBox1.TabIndex = 1;
            this.radTextBox1.TabStop = false;
            this.radTextBox1.Text = "radTextBox1";
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(498, 266);
            this.Controls.Add(this.radTextBox1);
            this.Controls.Add(this.radMultiColumnComboBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.radMultiColumnComboBox1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.radTextBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
 
        }
 
        #endregion
 
        private Telerik.WinControls.UI.RadMultiColumnComboBox radMultiColumnComboBox1;
        private Telerik.WinControls.UI.RadTextBox radTextBox1;
    }
}

3 Answers, 1 is accepted

Sort by
0
Francois
Top achievements
Rank 1
answered on 14 Oct 2011, 01:51 PM
I tried registering to the KeyUp/Down/Pressed events but they are not raised when I press TAB.

And the solution proposed in the thread http://www.telerik.com/community/forums/winforms/multicolumncombo/how-to-add-tab-key-as-select-value.aspx does not select a value; the text that the user typed remains and no value is selected.
0
Accepted
Martin Vasilev
Telerik team
answered on 18 Oct 2011, 12:25 PM
Hi Francois,

Thank you for writing.

You can achieve the described behaviour by overriding the ProcessCmdKey and call OnKeyDown event to execute the same functionality as when pressing the Enter key. Please consider the following code:
public class CustomMultiColumnComboBox : RadMultiColumnComboBox
{
    protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
    {
        switch (keyData)
        {
            case Keys.Tab:
                OnKeyDown(new KeyEventArgs(Keys.Enter));
                this.MultiColumnComboBoxElement.ClosePopup();
                break;
        }
 
        return base.ProcessCmdKey(ref msg, keyData);
    }
}

Hope this helps. Let me know if you have any other questions.

Regards,
Martin Vasilev
the Telerik team

Q2’11 SP1 of RadControls for WinForms is available for download (see what's new); also available is the Q3'11 Roadmap for Telerik Windows Forms controls.

0
Francois
Top achievements
Rank 1
answered on 18 Oct 2011, 01:09 PM
Thank you, works as expected.
Tags
MultiColumn ComboBox
Asked by
Francois
Top achievements
Rank 1
Answers by
Francois
Top achievements
Rank 1
Martin Vasilev
Telerik team
Share this question
or