PlanetSquires Forums

Support Forums => Other Software and Code => Topic started by: Gary Stout on January 02, 2015, 05:51:38 AM

Title: Has Focus/ Lost focus
Post by: Gary Stout on January 02, 2015, 05:51:38 AM
I am trying to determine through a message when a control has focus and when it loses focus. For example, when a textbox has focus, I change the background color to highlight the active textbox and when it loses focus, I change the color back to its normal color.

Thanks,
Gary
Title: Re: Has Focus/ Lost focus
Post by: David Kenny on January 02, 2015, 02:53:39 PM
You are looking for the EN_SETFOCUS and EN_KILLFOCUS messages.  Both supported by FF message handling routines.
Title: Re: Has Focus/ Lost focus
Post by: Gary Stout on January 02, 2015, 06:05:23 PM
Thanks David,

I will check those.
Your textbox/listbox example has been very helpful. I have made a few modifications to your example so that it acts pretty much like a combobox. Once you type a letter into the textbox and the listbox opens, I now have the selectionbar appear and the textbox will trap the up/down arror keys so that one can move the selectionbar in the listbox like a normal combobox selection would act. Pressing <ENTER> will select the highlighted item or pressing <ESC> will close the listbox and clear the search text in the textbox. I am also trapping the HOME and End keys to either go to the top of the list of the bottom. I am just about to the point of moving the test code over to the actual program. Your code was very helpful to get the results I was trying to duplicate from the old program.

Thanks again,
Gary
Title: Re: Has Focus/ Lost focus
Post by: David Kenny on January 03, 2015, 03:16:14 AM
Thanks for the kind words Gary.

I'm glad you liked it and can make use of it.  I had always preferred the filtering method instead of the more common method of simply scrolling to the first match.  I couldn't find a PB version of it anywhere (I am sure I just missed it, but I looked for some time), so I resorted to rolling my own.

It's good you have been able to modify it to suit your needs also.  As I said, you could do it with the combobox, but there was a reason I did it that way.  It might have been because I didn't like the default roll-up and drop-down behavior of the combobox.  It's been too long.

David
Title: Re: Has Focus/ Lost focus
Post by: Gary Stout on January 03, 2015, 01:58:28 PM
Quote from: David Kenny on January 02, 2015, 02:53:39 PM
You are looking for the EN_SETFOCUS and EN_KILLFOCUS messages.  Both supported by FF message handling routines.

When I try either of these messages for a particular textbox, I seem to create an endless loop. Basically I put a messagebox in each of the two events and all I get is the Focus msgbox and the NoFocus msgbox alternating. What am I missing? Basically, my goal is to set the background color of the textbox while it has the focus and then revert back to the default color once the textbox loses focus.

Thanka again,
Gary
Title: Re: Has Focus/ Lost focus
Post by: Gary Stout on January 03, 2015, 03:33:20 PM
I think I may have figured it out. I am still trying to learn where to put stuff. I did some searching on the PB Forums and found some DDT code that I was able to adapt to FF code. This is what I done....if someone can confirm I did it/put it in the right place.

David, this is the Textbox from the sample code you posted in another post.


Function FORM1_WM_COMMAND ( _
                          hWndForm     As Dword, _  ' handle of Form
                          hWndControl  As Dword, _  ' handle of Control
                          wNotifyCode  As Long,  _  ' notification code
                          wID          As Long   _  ' item, control, or accelerator identifer
                          ) As Long

   Select Case wID
          Case IDC_FORM1_FINDTB
               If wNotifyCode = %EN_SetFocus Then
                  FF_Control_SetColor(HWND_FORM1_FINDTB, -1, &H00FFFF )
               ElseIf wNotifyCode = %EN_KillFocus Then
                  FF_Control_SetColor(HWND_FORM1_FINDTB, -1, &HFFFFFF )
               End If 
   End Select
End Function


Thanks again,
Gary

ps... I am gonna master this thing one way or the other! LOL
Title: Re: Has Focus/ Lost focus
Post by: David Kenny on January 03, 2015, 03:44:32 PM
The msgbox is causing that.  The control loses focus to the msgbox and regains it when you close the msgbox.  I am betting that a msgbox added to new code you posted will have the same results.

You can use a label and change it's text instead of the msgbox.  The label won't be getting the focus so it won't cause the same problem. You can also use ZTrace, but that will take the focus away one time if it's not already open.  To stop that you can fire ztrace once in the Form Create routine to make sure it's already open.

You did it in the right place the first time.  Just don't use msgbox in there. :)

David
Title: Re: Has Focus/ Lost focus
Post by: Gary Stout on January 03, 2015, 03:55:39 PM
Thanks David!

I tried putting just the FF_Control_SetColor code in the SetFocus and the KillFocus events for the textbox and it works perfect! Thanks again for helping me along. Once I learn where to put things, I don't think I will have as many problems. It is all starting to make more sense...a little at a time.

Thanks again,
Gary
Title: Re: Has Focus/ Lost focus
Post by: David Kenny on January 03, 2015, 04:57:19 PM
I wish I could say I skipped the frustration and learning curve, but I can't.  I have no doubt you will get up to speed quickly.
Title: Re: Has Focus/ Lost focus
Post by: Wilko Verweij on January 04, 2015, 08:32:55 AM
You can use the Set Selection snippet from FireFly, like

Function YourControl_EN_SETFOCUS ( _
                                      ControlIndex   As Long,  _  ' index in Control Array
                                      hWndForm       As Dword, _  ' handle of Form
                                      hWndControl    As Dword, _  ' handle of Control
                                      idTextControl  As Long   _  ' identifier of text control
                                      ) As Long

  FF_TextBox_SetSel(hWndControl, 0, -1)

End Function

That makes the entire text selected. Hope this helps.
Wilko
Title: Re: Has Focus/ Lost focus
Post by: Gary Stout on January 04, 2015, 07:33:52 PM
Thanks for the reply, Wilko!
Title: Re: Has Focus/ Lost focus
Post by: Petrus Vorster on January 06, 2015, 04:40:46 PM
You can always do it the quick and dirty way....but its time consuming if you have loads of textboxes.
In the Textbox Setfocus and killfocus events just place the color changes.
So not WHILE it has the focus, but when it receives the focus, and change again on KILLFOCUS.

But its not via the message you were looking for!  ???
Title: Re: Has Focus/ Lost focus
Post by: Gary Stout on January 06, 2015, 05:52:49 PM
Quote from: Petrus Vorster on January 06, 2015, 04:40:46 PM
You can always do it the quick and dirty way....but its time consuming if you have loads of textboxes.

Thanks Petrus,

Is there a better way or location is one is using quite a few textboxes? The particular form I am working on has maybe 20 textboxes....not a bunch, but enough. If there is an easier way to handle many textboxes, I would be greatful to know how!

Thanks again,
Gary
Title: Re: Has Focus/ Lost focus
Post by: Paul Squires on January 06, 2015, 07:20:31 PM
Easiest method would be to create a Control Array of TextBoxes. In the message handler you identify the TextBox by using the ControlIndex parameter.
Title: Re: Has Focus/ Lost focus
Post by: Gary Stout on January 06, 2015, 07:34:19 PM
Thanks Paul,

I will explore that method. Do you have any FF updates in the works?
Good to hear from you! Hope you and your family have a great New Year!

Gary
Title: Re: Has Focus/ Lost focus
Post by: Paul Squires on January 07, 2015, 10:24:12 AM
Hi Gary, no big FF updates in the works. I will address any major bugs but I have put plans for new features/functionality on hold. With the demise of PowerBASIC it does not make much sense to devote too much development time on new features.

Hope you're doing well these days!