Delphi - ComboBox - Click event handler running when key is pressed - delphi

I'm using Delphi where I have a ComboBox on a form - in the Click event handler I'm calling ShowMessage('click') while in the KeyPress event handler ShowMessage('KeyPress'). When the ComboBox has the focus, and I press a 'normal' key like 'd', first the KeyPress event handler is running then follows the Click event handler.
Interesting: when the combobox's style property is set to csDropDownList or csDropDown, the Click event handler runs even twice, when it's set to csSimple, it runs 'only' once.
So I can't easily have a combobox where i (in the click event handler) set the focus to the next component after the user selected an item by clicking, since when the user is pressing keys the click event handler will run and set the focus to the next component. so what i planned for usability purposes would drive him crazy.
I wished the Delphi developers would be forced to use such a program for a whole week, 8 hours a day - I'd set the forms caption to sth like 'Never ever trigger the click event handler when the mouse wasn't really clicked!' - Though the problem is well known FOR MANY YEARS now, and Delphi costs REALLY much, the problems still exists.
Is there anyone with a good solution?

Related

Textbox lostfocus event doesn't fire when using a default button

Today I stumbled upon a problem with a LostFocus event from a TextBox that didn't fire. Most clients didn't have any problems but a small portion of them reported unexpected behavior. After some research I found that the clients who didn't had the problem clicked on the "Ok" button with the mouse while the other clients pressed Enter on their keyboard. The "Ok" button was the default button on the Form so pressing Enter should work just fine. The problem is that pressing Enter doesn't fire a LostFocus event on the TextBox with focus.
After some Googling it was pretty clear that this is the expected behavior of a default button. The focus never loses the TextBox and the code behind the CommandButton Click event is being run without it being clicked.
How to get the LostFocus event to fire when using a default button?
A simple hack that worked for me is to set the focus to the "Ok" button whenever the Click event is being fired. That way the current control automatically runs its LostFocus event. Don't forget to put an extra DoEvents after setting the focus. Otherwise the LostFocus event fire after your other code has been executed.
Private Sub cmdOk_Click()
cmdOK.SetFocus
DoEvents
'Run your other code
End Sub

How to get the handle of a Vista style Open/SaveDialog?

I'd like to get the window handle of a new vista-style Open/SaveDialog opened by my Delphi application.
It was possible with the old style dialog by parsing OnShow, but with the new style dialog there is no such event.
Is there a possibility maybe to iterate through all window handles in Windows and get it that way?
Thanks!
Edit: I know that OpenDialog.Handle will return the handle, but only when the dialog is visible (otherwise it's 0). I'd need an event to catch the Handle straight after showing the dialog (without any user action, ie.: select an item in the dialog, changing the file type, etc.).
I'd like to get the window handle of a new vista-style Open/SaveDialog opened by my Delphi application.
This is available through the dialog's Handle property.
Probably the easiest way to catch the event of the dialog showing is to use a CBT hook that you set immediately before showing the dialog, and remove as soon as it closes.
TOpenDialog has an OnShow event which fires just after dialog is shown so you can use to get the OpenDialog.Handle since the handle is already set at that time.
EDIT: After some pepole pointed out that using of OnShow event changes the dialogs apperance I tested this out and can confirm that using of OnShow event realy does change the dialogs aperance.

Select text in HTML with GWT mouse events sunk

I have a <div> with some text and I would like the user to be able to natively select the text. The issue is that when I attach any mouse event handler to the element or it's parent (besides document element) the selection won't work any longer.
This could be observe on the site linked below. If you sink mouse events the selection won't work for the first line.
http://rafalrybacki.com/lab/selection_ios/
What happens after clicking "sink mouse events" is:
main.sinkEvents(Event.MOUSEEVENTS);
Checked with iOS5.
I would like make the text selecting work (keeping the mouse events). How to do this?

Problems with leaving ComboBox on Delphi 7

I have just noticed a strange behavior of ComboBox component. I am using it in DropDownList style. If I click on it, it drops down the list of items, as usual. But then if I click on some other component on the form, the combobox will not let me leave it, unless I select one of the items. I thought of adding onmouseleave event, but in that case it would close even if I move mouse out of combobox area a little, which is undesirable. What I would like to see is combobox losing focus whenever i click somewhere outside of its area. Is that possible?
Actually, I just did what you did: started a new application and threw ComboBox and TEdit. Ran the program. It drops down the list just as you said when you clicked on it. Also, when I clicked on the TEdit, the combobox closed automatically as expected. So, I don't know what is the problem with your program.

Will a Delphi form always fire OnResize when it's shown?

If I create a new Delphi form, hook its OnResize event, and run the app, OnResize is fired before the window is shown. What I don't know is whether this will always happen, for any window.
(For anyone familiar with the Windows API, I've traced it to the ShowWindow call in TCustomForm.ShowingChanged (Forms.pas line 5503 in Delphi 2007), which apparently triggers a WM_SIZE... at least, for a new window with no other properties set. I haven't seen it documented that ShowWindow always fires WM_SIZE, so I don't know whether I can count on this or not.)
So: Can I rely on a TForm always firing OnResize when it's first shown? Or are there circumstances (maybe if the window is non-resizable, maybe if the Position property has certain values, etc.) where OnResize might not fire before the window is first shown?
No, this event doesn't always fire when the form shows, depending on things like BorderStyle. For example, it fires on startup for bsSingle, but not for bsDialog.
It's easy to test. Just add some logging code to the main form's OnResize event, change the BorderStyle and run your app.

Resources