Solved

When does TComboBox.Focused occur?

Posted on 1998-10-08
8
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 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