Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

When does TComboBox.Focused occur?

Posted on 1998-10-08
8
Medium Priority
?
214 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

650 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