Solved

When does TComboBox.Focused occur?

Posted on 1998-10-08
8
206 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create Database on Android via Delphi dbExpress 3 108
Making delphi communicate with a c# service 16 111
Find and Replace Stream with 0s 8 69
Multi-layered image in FireMonkey 9 48
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
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…

791 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