Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Another comboBox problem

Status
Not open for further replies.

Katy44

Technical User
Dec 12, 2003
723
GB
(Windows application)
I have created a KeyDown event for my comboboxes, which looks like this:

Code:
protected void comboBox_KeyDown(object sender, KeyEventArgs e)
{

ComboBox cbSender = (ComboBox)sender;
	if (e.KeyValue == 13)
	{				
								findRecords();				
	}
}
findRecords simply redraws part of the screen.
It is intended so the user can use the arrow keys to navigate up and down the combobox and then use the neter key to make their selection.
When the selection is made and the enter key pressed, the screen redraws itself (i.e. calls the findRecords() method) fine on the first time I try it, but after that it draws itself more than once. I think that on the second time it is 2, then 4 then 8 etc.
I wondered if that was because of the way I had appended the event handler:

Code:
if (this.Controls[i] is ComboBox && isPageLoad)
	{
this.Controls[i].KeyDown += new KeyEventHandler(this.comboBox_KeyDown);
	}
but the comboBoxes aren't redrawn every time the screen is redrawn anyway.
I'm sorry if I haven't explained this very well, but any help would be appreciated.
 
I'm not sure what's going on. It could be something as simple as using this:

Code:
e.Handled = true;

where [tt]e[/tt] is of type [tt]System.Windows.Forms.KeyEventArgs[/tt].

Also you might want to check out the [tt]typeof[/tt] operator to verify that the [tt]KeyDown[/tt] is there before assigning stuff. Here's an example:

Code:
private void Form_CurrentChanged(object sender, EventArgs e)
{
    if(typeof(CurrencyManager).Equals(sender.GetType()))
    {
        //do stuff.
    }
}

Bryan Wilhite
Songhay System
 
Thanks for your help.
I have figured out that every time the page was re-drawn, the same event handler was appended to the comboBox, so I've stopped that happening.
However, the e.Handled method may come in useful in another way.
My comboboxes have an onFocus event which drops them down as soon as they come into focus (as most navigation in the system will be via the keyboard).
That meant that clicking on the arrow button dropped the combobox down (as it came into focus) and then it immediately shot back up (as the arrow button had been clicked when it was dropped down).
I stopped this by adding an onclick event, which says if the combobox is not dropped down drop it down.
This now means that if the arrow button is clicked the comboBox drops down, shoots back up again and then drops down again. That works fine but looks a bit clumsy. Would e.Handled work with this?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top