Click to See Complete Forum and Search --> : Add text to a cboBOX
Peter Droegenueller
March 24th, 1999, 04:21 AM
I have a question concerning the combo box control. I created a form with a combo box. If the form are loaded some items are added to the combo box.
In my original programme I implemented to the click event an If clause (See below). If the if clause is wrong then the a MsgBox with warning should appear and the combo box shall be set to a predefined value. In my example "today". If I performe this action the combo Box stays blank after the click event. Do you have any Idea how to performe this command
Private Sub Form_Load()
Combo1.AddItem "monday"
Combo1.AddItem "tuesday"
Combo1.AddItem "wendsday"
End Sub
Private Sub Combo1_Click()
IF....then
MsgBox "WARNING", vbCritical + vbOKOnly, "Warning"
Combo1.Text = "today"
endif
End Sub
M.Anand
March 24th, 1999, 07:20 AM
What is the Style of the Combo-Box ? May be there is some catch there
Peter D.
March 24th, 1999, 08:03 AM
The Style is 0 - Dropdown Combo.
Ravi Kiran
March 25th, 1999, 09:29 PM
Hi,
This is an interesting problem!.
First things first :
1. Do you want your user to type anything in the combo box?
Only in situations where you would allow your user to type anything in the combobox and you validate it later, should you have a combo box type 0.
If i want my user to just select from existing options *only* , i prefer style set to 2.
2. Is it ok, if your default option also appears in the list: I provide here a solution with this assumption!. i.e the default option also appears in the list, BUT when the user selects any other option, it would be cancelled.
CbBox style is still '0'.
(Even SetWindowText wouldn't work!.)
My suggestion:Take a test project, put a form in it, place a combobox (Combo1) and a check box (Check1). This check1's value =1 is your 'If' statement's simulation. When ever check1 is checked, you can choose only the default.
copy & patse the following code and run. See if does what you want to do:
I have provided debug prints. Keep the debug window open, ( Cntrl-G) before running.
Note that when the message box appears the text in the combo box shows user's selection. Also note that two click events are fired on wrong selection.
So if you are doing long processing on click, put those statements after the line : If dontprocessclick Then Exit Sub
Notice that we are removing the default option from the list, when check1 is unchecked.
Tell me if it helped :-)
Ravi
--------
Option Explicit
Private Sub Combo1_Change()
Debug.Print "combo change event : Text:-" & Combo1.Text
End Sub
Private Sub Combo1_Click()
Debug.Print "Combo click event : Text :-" & Combo1.Text
Static dontprocessclick As Boolean
If dontprocessclick Then Exit Sub
With Combo1
If Check1.Value = 1 Then ' replace this with your if statement
If .ListIndex .ListCount - 1 Then
dontprocessclick = True
' We need the above line because,
' the line below will fire another click event, if the user's
' selection is not the Default option ie ListCount.
MsgBox ("This selection Not allowed under this condition")
.ListIndex = .ListCount - 1
dontprocessclick = False
' i prefer using something like this because if i have a big
' processing loop on click event, i need to take care of multiple
' (recursive) calls in each of them. simpler would be to
' use a variable like the one above and block it
End If
End If
End With
End Sub
Private Sub Combo1_DropDown()
With Combo1
If Check1.Value = 1 Then
If .List(.ListCount - 1) "*Today*" Then
.AddItem "*Today*", .ListCount
End If
Else
If .List(.ListCount - 1) = "*Today*" Then
.RemoveItem .ListCount - 1
End If
End If
End With
Debug.Print "Combo Drop down event : Text:-" & Combo1.Text
End Sub
Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)
' Debug.Print "Combo keydown event : Keycode=" & Chr$(KeyCode) & ", text:-" & Combo1
End Sub
Private Sub Combo1_KeyPress(KeyAscii As Integer)
' Debug.Print "Combo KeyPress event : Keycode=" & Chr$(KeyAscii) & ", text:-" & Combo1
End Sub
Private Sub Combo1_KeyUp(KeyCode As Integer, Shift As Integer)
' Debug.Print "Combo keyup event : Keycode=" & Chr$(KeyCode) & ", text:-" & Combo1
End Sub
Private Sub Form_Load()
With Combo1
.AddItem "Sunday"
.AddItem "Monday"
.AddItem "Tuesday"
.AddItem "Wednesday"
.AddItem "Thursday"
.AddItem "Friday"
.AddItem "Saturday"
End With
Check1.Caption = "Allow only default selection"
End Sub
-------
Peter D.
March 26th, 1999, 06:00 AM
Thanks a lot Ravi,
you solved my problem.
Your explanation was excelent. I hav'nt had any problem to implement your subroutine in my programme. And it works immidiately.
Thanks a lot. I can recommend your answer.
Peter
Ravi Kiran
March 29th, 1999, 12:48 AM
Hi,
Here is a small change you need to make in order to *totally* remove the
popping up of default option in the list. But if i were the user i would rather
have the default option also listed! :-)
If you have already figured this out, then please excuse me.
Change these two routines a little : The technique remains same : i.e add your default option to the list and select that list index. Preferable to add it in the end. Catch the DropDown event and see if the value is default value is present in the list, and remove it. Automatically the textbox area also gets cleared. You user will never see the default option. May be ideal if you are using it as a hint, like "Please select again---", than as option...
Private Sub Combo1_Click()
Static dontprocessclick As Boolean
If dontprocessclick Then Exit Sub
With Combo1
If Check1.Value = 1 Then
' add these 3 new lines:
If .List(.ListCount - 1) "*Today*" Then
.AddItem "*Today*", .ListCount
End If
If .ListIndex .ListCount - 1 Then
dontprocessclick = True
MsgBox ("This selection Not allowed under this condition")
.ListIndex = .ListCount - 1
dontprocessclick = False
End If
End If
End With
End Sub
Private Sub Combo1_DropDown()
With Combo1
If .List(.ListCount - 1) = "*Today*" Then
.RemoveItem .ListCount - 1
End If
End With
End Sub
Regards,
Ravi
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.