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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coding ComboBox Upclick and DownClick

Status
Not open for further replies.

FoxEgg

Programmer
Mar 24, 2002
749
AU
May I ask..

I have a Combo box on a simple form with values Mr, Mrs Miss Ms etc

I can cycle thru clicking with the little arrow on the Combo with the mouse..

But I thought it would be nice to use the keyboard up down keys.. Hence my code.

I checked with messagebox that 24 was getting thru ...but the Combo didn't move with the keyboard strokes !

Is there a trick ?

Code:
LPARAMETERS nKeyCode, nShiftAltCtrl
IF nKeyCode == 5
*!*	MESSAGEBOX(nKeyCode)
This.UpClick
thisform.Refresh()

*NODEFAULT
ENDIF

IF nKeyCode == 24
MESSAGEBOX(nKeyCode)
this.DownClick
thisform.Refresh()

*NODEFAULT
ENDIF

Sydney, Australia
 
Events occur and Event methods are triggered by the events, but not vice versa. If you call an event method you only call the code in the event method as you'd call code in any other normal native or user defined method (we can't define events), not the event behaviour. The UpClick and DownClick event methods exist for you to specify what should happen on top of the normal event behaviour, but will not cause this event and its behaviour. Eg you can call _screen.KeyPress(65,0) but you won't cause an A typed and as you can't put code into _SCREEN as a running object, nothing will happen.

This is often thought, as calling a button.click does the click. Well, that's just because the main behavour you want executed in that case is your own click code, but if you call the click event the default behaviour of a button to look pressed will not occur.

Besides that, in Dropdown Comobo style those combobox Up and DownClick events only occur, once the dropdown list is dropped down, not before. And then it's default behaviour the up/down arrow keys work on their own. And if you change Style to Dropdown List the up/down arrows work rightaway, and without any code! You're writing code, which implements the standard behaviour of a dropdown list, so use the dropdown list mode.

The name Combobox comes from being a combo, a combination of textbox and dropdown list. You only want selection of given values, so don't want the textbox part, which causes up and down arrow to work the same as tab/backtab to change controls, which many users and developers also don't know. If you want that base behaviour to change you would need to call nodefault in keypress in case of these keys, but in your case that's would of course be counter productive.

So overall, simply use style=2 - dropdown list, and your combobox acts as you like without any code.

Bye, Olaf.
 
The best solution is to simply set the combo's style property to 2.
If you want to prevent the list to be expanded, then place a NoDefault in the combo's dropdown event.

But if you wish to allow user to type (not only to choose from the list), then instead of UpClick/DownClick, you can manipulate the ListIndex property.

Code:
PUBLIC ofrm
ofrm = CREATEOBJECT("MyForm")
ofrm.show()

DEFINE CLASS MyForm as Form
	ADD OBJECT txt as textbox
	ADD OBJECT cmb as combobox ;
		WITH RowSourceType = 1,;
			 RowSource = "Mr,Mrs,Miss,Ms",;
			 top = 50 &&,  style=2 && the simplest solution
	PROCEDURE cmb.KeyPress
		LPARAMETERS nKeyCode, nShiftAltCtrl
		IF nKeyCode == 5
			IF This.ListIndex >1
				This.ListIndex = This.ListIndex - 1 && previous item
			ENDIF
			NODEFAULT
		ENDIF

		IF nKeyCode == 24
			IF This.ListIndex < This.ListCount
				This.ListIndex = This.ListIndex + 1 && next item
			ENDIF
			NODEFAULT
		ENDIF 
	ENDPROC
*!*	*********************************************
*!*	* Only if you want to prevent epanding the list	
*!*		PROCEDURE cmb.dropdown
*!*			NODEFAULT
*!*		ENDPROC
*!*	*********************************************
ENDDEFINE

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
To clarify. If you want to enable typing any other addressing/salutation and want Style=1, Vilhelm-Ion's KeyPress method will help to give the up/down arrow keys the functionality to skip forth and back through the list, even though it's a combobox in it's combinatorial mode of textbox and dropdown list and otherwise up/down would move focus.

If you uncomment his dropdown event code to react with NODEFAULT, users can type, choose predefined items with up/down arrow, but won't ever expand the list to see it fully.

And now don't get the idea you need this as is, you'd rather add it to your visual form, it's just the easiest way to post code in code form here and in many other forums. I just say the obvious here, as you surely won't switch your whole visual forms to code, but there have been such misunderstandings from others in the past.

Bye, Olaf.
 
Olaf said:
Events occur and Event methods are triggered by the events, but not vice versa.

Yes, yes, and yes.

But with the added detail that you can call RaiseEvent() to fire an event (thereby triggering the event method).

I would not do this. There is almost always a better, more manageable, way to organize your code. But it's worth mentioning for those times when you've already painted yourself into a corner.
 
Thanks again guys...

I just stole Vilhem's bit of PROC code and stuck it in the KEYPRESS and it worked very well

Code:
		LPARAMETERS nKeyCode, nShiftAltCtrl
		IF nKeyCode == 5
			IF This.ListIndex >1
				This.ListIndex = This.ListIndex - 1 && previous item
			ENDIF
			NODEFAULT
		ENDIF

		IF nKeyCode == 24
			IF This.ListIndex < This.ListCount
				This.ListIndex = This.ListIndex + 1 && next item
			ENDIF
			NODEFAULT
		ENDIF

And for all the other information... WOW ! I am gonna enjoy playing with that.

AND thank you ALL

Combo working...
Fox working < (this.Fox.human.working = .T.)
VFP working

John Fox

PS Regarding the use of 'NODEFAULT' ... I am officially an expert on this topic now... if something doesn't work as I want.. I stick a 'NODEFAULT' in (lets see if that works)... It is a bit like my electronics design... either a bigger or smaller resistor should make it work. I am a 'parts jockey'.. an official VFP 'coding jockey'



Sydney, Australia
 
>if something doesn't work as I want.. I stick a 'NODEFAULT' in (lets see if that works)
That's nonsense, you should read up what it means and then you'd know it won't be a universal cure for problems, not even in regard of events.
Just a hint: The VFP help has over 4000 html files (you can decompile any chm file) and that makes it the most comprehensive reference book (or even multiple books) on VFP.

Bye, Olaf.
 
Hi Olaf...

And you are ABSOLUTELY correct... as and engineer I know that you look up what something does, you should fully understand it and use it appropriately
As a surgeon ... That is doubly true...
As someone who is both, and who is time poor ... who has a time-restricted urgent job to do... then I will try a solution that,
IF IT WORKS, it doesn't kill anyone and
IF IT WORKS, I don't bother anyone... and
IF IT DOESN'T.. then it doesn't kill anyone
AND there are the brilliant Olaf's and Dan's and Ramani's and Vilhelm's and Gagnon's and Mike's out there who volunteer their precious time... AND I haven't killed anyone.

I am not a religious person... but I do delight in simplicity.... In fact I did one (a NODEFAULT) this morning on a TEXT box.. and it worked....

And so ... to quote a mate of mine...

When the solution is simple, God is answering.
Albert Einstein

I only wish Albert's God was on Tek-Tips

Cheers

JF

((PS I know you are right... and I am wrong... but boy it saves me some time))

PS2.. 'the killing someone' is a little bit of hyperbole.... I haven't killed anyone with surgery or software... but it makes for a good laugh.

Was it Roberta Flack that wrote the original code ? "Killing me softly with his code"... Just checked it was Roberta ...Google "watch?v=O1eOsMc2Fgg"
"Killing his software with his code"



Sydney, Australia
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top