Dont show drop down on ComboBox

branston

Registered User.
Local time
Today, 03:34
Joined
Apr 29, 2009
Messages
372
Hi,

I have an if statement on the MouseDown event on a combo box.

I was wondering if anyone knew how to do the following:

if (a certain criteria) then
Do not show drop down box, and act as if nothing had been pressed
else
Show the combo box as usual
end if

basically, I have a combo box which automatically updates a date. if the 1st citeria is met I want a message box to pop up saying "You must do something before you can change this" and when that is closed for the combo box to return to its 'unopened' state without updating the date. (The date update is attached to the OnUpdate event of the combo)

Any ideas?

Thank you
 
You could do the following the first .visible will hide the combobox and the second .visible will unhide the combobox


if (a certain criteria) then
Do not show drop down box, and act as if nothing had been pressed
me!combobox.visible=false
else
Show the combo box as usual
me!combobox.visible=true
end if
 
Hi,

In case you do not want to actually hide the combobox when the user clicks on it, one unorthodox but still working way is to set the focus to another control (maybe an invisible "dummy" one). In this way, the combobox will lose focus and the list will not be displayed. There must also be a more "straight" way, but I cannot figure it out.

Anyway, you could just include this "if" block in another part of your code, maybe in an "exit" or "lostfocus" event of another control (which affects the condition in the "if" block), and enable or disable the combobox depending on the condition.

If you explained a little about what the (certain criteria) include and related controls I could be more specific.
 
Initailise the combobox RowSource as null with the OnCurrent event of the record source
Me.Combobox.Rowsource = ""

Set the RowSource to the valid fields of the combobox using the OnChange events of the mandatory fields.
Me.ComboBox.RowSource = "SELECT etc etc" if it is a query.
Me.Combobox.RowSource = "1;2;3" if it is a List.

You could even consider initialising the combobox with your message rather than the null or use your msgbox as you planned.

However I think there is a much better way to manage the whole process.

Simply disable the combobox. It will display its current value but won't dropdown.
Me.ComboboxName.Enabled = False
This will also grey it out suggesting it is not ready to perform.

Set the ToolTip Text to your "must enter something else" message. It will still popup even on a disabled control.
You can change it dynamically.
Me.ComboboxName.ControlTipText = "blah blah"

This message can be reinforced in several other ways.

Cover the dropdown arrow with a shutter when it is disabled. This makes it look like a text box.
Place a small rectangle over the dropdown arrow, set it the same colour as the form background, and set its border to transparent.
Change the background from transparent to opaque as required using the OnUpdate event of the mandatory fields.
Me.ComboShutter.BackStyle = 0 ' makes it transparent
Me.ComboShutter.Backstyle = 1 ' makes it opaque.

I always set the borderstyle to transparent when disabling text or comboboxes (with shuttter). This make them look like they are printed directly on the form and clearly uneditable.

Me.ComboboxName.BorderStyle = 0 ' makes it transparent
Me.ComboboxName.BorderStyle = 1 ' makes it opaque

Incidentally:

If you want to use the shutter trick to simply hide the dropdown arrow without deactivating the combobox you don't need to make the shutter transparent. The combobox will automatically come to the front when you click on it. This gives it the look of a textbox with the function of a combobox.

You can make a combobox drop down automatically when you click anywhere on it by using an OnGotFocus event.
Me.ComboboxName.Dropdown

If for some reason you want to stop a disabled combobox (text or listbox too) from greying out, lock it as well.
Me.ComboboxName.Locked = True
 
Last edited:
Here is another idea that will do exactly as you require and is very simple.

Disable the combobox.
Place an empty text box on top of it with transparent background and border. (Not hidden). Run the message from an event on the transparent box.

Use the OnChange event of the mandatory fields to enable the combobox and disable the transparent box (shift the focus elsewhere first).

Reset them for the next record with the OnCurrent event of the Form displaying the Record Source or whatever.

Haven't tried it but it should work. If it gives you any trouble use the mandatory field OnChange to move the transparent box to the side as well.
Me.ComboboxName.Left = SomeOtherInteger.

(Note that positions are measured in Twips in vb. One centimeter is 567 twips.)
 
Thank you everyone for all your suggestions. I will have a go at all of them, and see which one works best for what I want.

Thank you!
 
Right, I had a go at some of these.
I liked the idea of just disabling the combo box, but it came up with an error saying "Can not disable object that has focus" or something to that end. So I set the focus to something else and that worked. However I didnt actually want my combo box to be greyed out, so outside the if statement i made it enabled again. This works great, and was nice and easy - always a bonus.
The code I used was:

Dim strnow As String
strnow = Now()
If Me.RevDate <= DateAdd("n", -30, strnow) Then
MsgBox "You must create a new revision before you change the name", vbCritical
Me.RandomBoxName.SetFocus
Me.ComboName.Enabled = False
Else

End If
Me.ComboName.Enabled = True

so this basically only shows the combo box is a new revision has been made in the last half hour.

Thanks for all teh help everyone!
 
..... However I didnt actually want my combo box to be greyed out


As I mentioned in the incidental part at the end of of my first post. Disabled controls don't grey out if they are also also locked.
 
Ah, of course! Sorry about that Glaxiom. That is much neater! Thank you!
 

Users who are viewing this thread

Back
Top Bottom