Undo ComboBox Dropdown?

J

joecool1969

I have an app that has several controls. One is a TextBox where the
user must enter the name of a View in a database, and the next control
is a ComboBox. I want to verify that the name of the view the user
entered is a valid view name and if not, set the focus back to that
textbox. All well and good if the user tabs out of the textbox and
focus shifts to the combobox, I perform the check in the textbox's
Leave event. Now if the user clicks on the combobox's dropdown, I can
perform the check in the combobox's DropDown event. But here's the
rub. If the value for the view is invalid and I set focus back to the
textbox, the combobox's listbox is still visible. How do I force the
dropdown to re-hide the combobox's listbox?
 
A

AliR \(VC++ MVP\)

I think you are doing things the hard way. Have you looked at the
Validating event for the edit control. I'm pretty sure that would be a much
better place to validate the data of the edit control. And if things are
not correct you can simply keep the focus on the edit control by setting the
cancel property of the CancelEventArgs to true.

AliR.
 
A

AliR \(VC++ MVP\)

And if you still want to do things the way you are right now, you can close
the combobox by sending it the CB_SHOWDROPDOWN message with false in the
wParam.

AliR.
 
J

joecool1969

I think you are doing things the hard way.  Have you looked at the
Validating event for the edit control. I'm pretty sure that would be a much
better place to validate the data of the edit control.  And if things are
not correct you can simply keep the focus on the edit control by setting the
cancel property of the CancelEventArgs to true.

AliR.

You know, I have used the Validating event to make sure all data on a
tabpage was valid before letting the user select a different tabpage.
I never thought of using it on other types of controls. Using it to
validate the textbox text works perfectly since it fires before the
next control's dropdown event fires.

Thanks.
 
A

AliR \(VC++ MVP\)

Glad I could help.

Keep in mind that if you have the system menu turned on (where the user can
press the X to close the form) you might have to check and see if the cursor
is in your client area when the event is fired, otherwise you might no be
able to close the form.

private void textBox_Validating(object sender, CancelEventArgs e)
{
if (ClientRectangle.Contains(PointToClient(Cursor.Position)))
{
if (thing are wrong)
{
e.Cancel = true;
}
}
}


AliR.


I think you are doing things the hard way. Have you looked at the
Validating event for the edit control. I'm pretty sure that would be a
much
better place to validate the data of the edit control. And if things are
not correct you can simply keep the focus on the edit control by setting
the
cancel property of the CancelEventArgs to true.

AliR.

You know, I have used the Validating event to make sure all data on a
tabpage was valid before letting the user select a different tabpage.
I never thought of using it on other types of controls. Using it to
validate the textbox text works perfectly since it fires before the
next control's dropdown event fires.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top