Answered by:
How can I override an event method for a combobox control used in my Windows App?

Question
-
Hi,
I'm using a combo box control in my windows Application. I want to override the keydown event of the combo box control so that the behaviour of changing the selected items in a circular way while pressing the up or down arrow keys.
Any help will be appriciated.
With Regards,
SreeRam.
a dedicated learner- Moved by Peter Ritchie Friday, June 20, 2008 1:53 PM off-topic
Thursday, June 19, 2008 10:27 AM
Answers
-
Hi again,
Well by combining what I have in my post with what you have in yours, here's a version of the modified combo box:
public class ModComboBox : System.Windows.Forms.ComboBox { public ModComboBox() {} protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e) { if (this.Items.Count == 0) return; if (e.KeyCode == Keys.Down) if (this.SelectedIndex == this.Items.Count - 1) { this.SelectedIndex = 0; this.SelectAll(); e.SuppressKeyPress = true; } if (e.KeyCode == Keys.Up) if (this.SelectedIndex == 0) { this.SelectedIndex = this.Items.Count - 1; this.SelectAll(); e.SuppressKeyPress = true; } } }
Hope this helps,
-Tudor
19 years old student http://www.tudoralex.com- Proposed as answer by Tudor Alexandru Thursday, June 19, 2008 1:52 PM
- Marked as answer by SreeRamaSaran Mullapudi Friday, June 20, 2008 7:22 AM
Thursday, June 19, 2008 1:51 PM
All replies
-
Hi,
Here's an example of how to achieve what you want for the down key. For the up key things are similar.1 public partial class Form1 : Form 2 { 3 public Form1() 4 { 5 InitializeComponent(); 6 7 for (int i = 0; i < 10; i++) 8 this.comboBox1.Items.Add(i); 9 10 this.comboBox1.KeyDown += new KeyEventHandler(comboBox1_KeyDown); 11 } 12 13 void comboBox1_KeyDown(object sender, KeyEventArgs e) 14 { 15 ComboBox c = sender as ComboBox; 16 if (e.KeyCode == Keys.Down && c.SelectedIndex == c.Items.Count - 1) 17 { 18 c.SelectedIndex = 0; 19 c.SelectAll(); 20 e.SuppressKeyPress = true; 21 } 22 } 23 }
Hope this helps,
-Tudor
19 years old student http://www.tudoralex.com- Proposed as answer by Tudor Alexandru Thursday, June 19, 2008 10:39 AM
- Edited by Tudor Alexandru Thursday, June 19, 2008 10:40 AM ----
Thursday, June 19, 2008 10:38 AM -
Hi Tudor,
Thanks for your reply. I already build the logic to perform the required task in KeyDown event. Below is the code I've wrote for on combo box.
private void cboFactorLevel_KeyDown(object sender, KeyEventArgs e)
{
if (cboFactorLevel.Items.Count == 0) return;
switch (e.KeyValue)
{
case 38:
if (cboFactorLevel.SelectedItem == null || cboFactorLevel.SelectedItem.Index == 0)
{
cboFactorLevel.SelectedItem = cboFactorLevel.Items[cboFactorLevel.Items.Count - 1];
}
else if (cboFactorLevel.SelectedItem.Index > 0)
{
cboFactorLevel.SelectedItem = cboFactorLevel.Items[cboFactorLevel.SelectedItem.Index - 1];
}
break;
case 40:
if (cboFactorLevel.SelectedItem == null || cboFactorLevel.SelectedItem.Index == cboFactorLevel.Items.Count - 1)
{
cboFactorLevel.SelectedItem = cboFactorLevel.Items[0];
}
else if (cboFactorLevel.SelectedItem.Index < cboFactorLevel.Items.Count - 1)
{
cboFactorLevel.SelectedItem = cboFactorLevel.Items[cboFactorLevel.SelectedItem.Index + 1];
}
break;
default:
break;
}
}
Now I have plenty of combo boxes in my windows application which forces me to write the same above code for all the combos I have. I tried to override the combobox control by using the below code. Unfortunately it is not working. I created a new class by inheriting the combobox control. Just look the code below. I hope you'll understand it better.
public class ComboBox : System.Windows.Forms.ComboBox
{
public ComboBox()
{
}
protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
{
if (this.Items.Count == 0) return;
switch (e.KeyValue)
{
case 38:
if (this.SelectedItem == null || this.SelectedItem.Index == 0)
{
this.SelectedItem = this.Items[this.Items.Count - 1];
}
else if (this.SelectedItem.Index > 0)
{
this.SelectedItem = this.Items[this.SelectedItem.Index - 1];
}
break;
case 40:
if (this.SelectedItem == null || this.SelectedItem.Index == this.Items.Count - 1)
{
this.SelectedItem = this.Items[0];
}
else if (this.SelectedItem.Index < this.Items.Count - 1)
{
this.SelectedItem = this.Items[this.SelectedItem.Index + 1];
}
break;
default:
break;
}
base.OnKeyDown(e);
}
}
I think you can guide me better.
Once again thanks for your reply.
With regards,
SreeRam.
a dedicated learnerThursday, June 19, 2008 10:49 AM -
Hi again,
Well by combining what I have in my post with what you have in yours, here's a version of the modified combo box:
public class ModComboBox : System.Windows.Forms.ComboBox { public ModComboBox() {} protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e) { if (this.Items.Count == 0) return; if (e.KeyCode == Keys.Down) if (this.SelectedIndex == this.Items.Count - 1) { this.SelectedIndex = 0; this.SelectAll(); e.SuppressKeyPress = true; } if (e.KeyCode == Keys.Up) if (this.SelectedIndex == 0) { this.SelectedIndex = this.Items.Count - 1; this.SelectAll(); e.SuppressKeyPress = true; } } }
Hope this helps,
-Tudor
19 years old student http://www.tudoralex.com- Proposed as answer by Tudor Alexandru Thursday, June 19, 2008 1:52 PM
- Marked as answer by SreeRamaSaran Mullapudi Friday, June 20, 2008 7:22 AM
Thursday, June 19, 2008 1:51 PM -
Thanks Mr. Tudor
Could I use a partial class instead of using a brand new class for the above purpose to avoid initialization of this new class? If yes plz let me know how could i achieve it? I tried the previous code you suggested me and it's working fine. But It doesn't support the designer. To use this we have to add the controls programatically. I got another thought that if I use the same control without inheriting the combobox control it looks fine and it saves the time. For that purpose I want to use the partial class and just override the onkeydown event method. I tried it but it doesn't work. Could plz tell me how it will be achieved? If you suggest me any article to accomplish this I am ready to learn anything to get the job done.
awaiting for your reply.
Thanks a lot.
With regards,
SreeRam.
a dedicated learnerFriday, June 20, 2008 7:31 AM -
For questions and discussions regarding client application development using Windows Forms controls and features, please see http://forums.microsoft.com/msdn/ShowForum.aspx?ForumID=8&SiteID=1
http://www.peterRitchie.com/blogFriday, June 20, 2008 1:52 PM