Solved

When does TComboBox.Focused occur?

Posted on 1998-10-08
8
204 Views
Last Modified: 2010-04-06
I am trying to figure out WHEN a TComboBox has the focus.  I have created a OnChange event for a TComboBox, and yet, while I am physically typing data INTO the TComboBox, the event constantly reports that the TComboBox does NOT have the focus.  How can that be?!

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
      if ComboBox1.Focused then
            Caption := 'ComboBox is focused'
      else
            Caption := 'ComboBox not focused';
end;
0
Comment
Question by:Softtech
8 Comments
 
LVL 5

Expert Comment

by:Christian_Wenz
ID: 1342293
just a wild guess, but doesn't the "change" event for a ComboBox occur when you enter text _and_ the combox box gets blurred?
0
 

Author Comment

by:Softtech
ID: 1342294
A "Change" event occurs whenever you type a character into the TComboBox's edit field.  I am sure a Change event occurs at other times, but in my case, I am dealing with type-in's.
0
 
LVL 10

Accepted Solution

by:
viktornet earned 50 total points
ID: 1342295
Use the OnEnter() and OnExit() events of the combobox

procedure TForm1.ComboBox1Enter(Sender: TObject);
begin
  Caption := 'The Combo Box has got the focus!';
end;

procedure TForm1.ComboBox1Exit(Sender: TObject);
begin
  Caption := 'The Combo Box ain''t got the focus!';
end;

Regards,
Viktor Ivanov
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 2

Expert Comment

by:kjteng
ID: 1342296
You can use Form1.ActiveControl.Name to check if the combobox is focused.
eg.
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  if ActiveControl.name = 'ComboBox1' then
    Caption := 'ComboBox is focused'
  else
    Caption := 'ComboBox not focused';
end;

Your code works if you make it to be onClick event. It will not work in onChange event because (I guess) the combobox is in editing mode (ie the edit window is focused).

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1342297
Well, if an OnChange() occur in the combobox, of course the focus on the combobox... And if you enter the combobox, no matter if you change or edit or whatever you do, the combobox will still be in focus.... Summary : OnChange() occurs only if the component has gotten the focus... OnChange() cannot occur if the control doesn't have the focus.. so if yuour code is in OnChange() then the control is in focus .... Don't know if this makes any sense...Anyway, hope it does :-)

Regards,
Viktor Ivanov
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1342298
Sorry Viktor, but you're wrong on that last comment.
The combobox hasn't got the focus when in editing mode since the editbox you're editing in is a child to the combobox, and it is the editbox that actually has the focus.
Of course the OnChange event gets fired even if the combobox hasn't got the focus. The text can still be changed. Ex: SelText := 'anytext'; fires the OnChange event any time.

Try this:

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  if ( GetParent( GetFocus ) = ComboBox1.Handle ) then // combobox's editbox has the focus
  begin
    Windows.Beep( 1000, 20 ); // just some feedback...
  end;
end;

/// John

0
 

Author Comment

by:Softtech
ID: 1342299
Although Viktornet's answer doesn't explain the phenomenon at hand, Expert erajoj's comment seems to be correct.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1342300
Anyway, no one is perfect ;->
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to convert wav to mp3 in delphi 9 200
Convert Jpg /PNG To GIF 5 135
Comparing dates in Delphi. Greater than/ Less than 3 152
Breakpoint doesn't stop in my variable 3 27
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question