Solved

When does TComboBox.Focused occur?

Posted on 1998-10-08
8
203 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now