Board index » delphi » TComboBox - simple how to?

TComboBox - simple how to?

Hello All,

I have a simple (I hope) TComboBox question that escapes me. I am
using Delphi 3.0.

I have a TComboBox with names in it. I would like to be notified if
someone selects a name from the drop list. I would also like to
generate the same "selected" notification if someone types an ENTER
into the text portion or if someone clicks an external OK button.

The OnChange event seems to handle the drop list select, but will it
generate an OnChange with each char that someone types in the Edit
portion?

Is there a better way?

Thanks,
Barry.

 

Re:TComboBox - simple how to?


For key-monitoring you should use the onkeydown - event. if you are looking
for enter - try "key = vk_return"

Re:TComboBox - simple how to?


I think I would do something like this:

First, create a variable to hold the ItemIndex property of the
ComboBox. This variable needs to be accessible by your whole form, so
I would put it in the private declaration clause:

type
  TForm1 = class(TForm)
  .
  .
  private
    ComboBoxIndex: integer;
  end;

I would also declare a new procedure to handle the changes which could
come from one of three sources. This isn;t really necessary, but makes
for cleaner and easier to interpret code.

type
  TForm1 = class(TForm)
  .
  .
  private
    ComboBoxIndex: integer;
    procedure ComboBoxSelectionChange;
  end;

Make sure to initialize your new variable to something - this can be
done in Form's OnCreate event handler:

procedure TForm1.FormCreate(Sender: TObject);
begin
  ComboBoxIndex:=0; //the first item in the list, BUT
       // if the ComboBox begins empty use -1;
end;

Then, write the following code in the ComboBox's OnKeyPress handler:

procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: char...);
begin
  if Key=#13 then
     ComboBoxSelectionChange;
end;

And, you will need code to handle a selection from the drop down list:

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  if ComboBox1.ItemIndex<>ComboBoxIndex then
    begin
        ComboBoxIndex:=ComboBox1.ItemIndex;
        ComboBoxSelectionChange;
    end;
end;

Finally, for the external button:

procedure TForm1.Button1Click(Sender: TObject);
begin
  ComboBoxSelectionChange;
end;

And in the procedure itself you can do the actually work:

procedure TForm1.ComboBoxSelectionChange;
begin
  solve_world_hunger;
  do_something_else;
  .
  .
end;

There are various other approaches, some of them perhaps better ot
more efficient, and it depends on exactly the purpose of using this
event and in what ways it will typically be triggered. But I hope this
steers you in the right direction.

Take care,
Tom

Quote
On Wed, 17 Nov 1999 15:50:16 GMT, bschl...@twcny.rr.com (Barry) wrote:
>Hello All,

>I have a simple (I hope) TComboBox question that escapes me. I am
>using Delphi 3.0.

>I have a TComboBox with names in it. I would like to be notified if
>someone selects a name from the drop list. I would also like to
>generate the same "selected" notification if someone types an ENTER
>into the text portion or if someone clicks an external OK button.

>The OnChange event seems to handle the drop list select, but will it
>generate an OnChange with each char that someone types in the Edit
>portion?

>Is there a better way?

>Thanks,
>Barry.

Re:TComboBox - simple how to?


Thanks,
Using a temp var. This is a good approach.

I wish Delphi had passed on the CN_SELENDOK or something message from
Windows which helps alot. Maybe I'll see if I can use this in a
customized ComboBox to make things easier - then post it to the DSP.

Thanks,
Barry.

Quote
tomval...@volny.cz (Tom) wrote:
>I think I would do something like this:

>First, create a variable to hold the ItemIndex property of the
>ComboBox. This variable needs to be accessible by your whole form, so
>I would put it in the private declaration clause:

>type
>  TForm1 = class(TForm)
>  .
>  .
>  private
>    ComboBoxIndex: integer;
>  end;

>I would also declare a new procedure to handle the changes which could
>come from one of three sources. This isn;t really necessary, but makes
>for cleaner and easier to interpret code.

>type
>  TForm1 = class(TForm)
>  .
>  .
>  private
>    ComboBoxIndex: integer;
>    procedure ComboBoxSelectionChange;
>  end;

>Make sure to initialize your new variable to something - this can be
>done in Form's OnCreate event handler:

>procedure TForm1.FormCreate(Sender: TObject);
>begin
>  ComboBoxIndex:=0; //the first item in the list, BUT
>       // if the ComboBox begins empty use -1;
>end;

>Then, write the following code in the ComboBox's OnKeyPress handler:

>procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: char...);
>begin
>  if Key=#13 then
>     ComboBoxSelectionChange;
>end;

>And, you will need code to handle a selection from the drop down list:

>procedure TForm1.ComboBox1Change(Sender: TObject);
>begin
>  if ComboBox1.ItemIndex<>ComboBoxIndex then
>    begin
>        ComboBoxIndex:=ComboBox1.ItemIndex;
>        ComboBoxSelectionChange;
>    end;
>end;

>Finally, for the external button:

>procedure TForm1.Button1Click(Sender: TObject);
>begin
>  ComboBoxSelectionChange;
>end;

>And in the procedure itself you can do the actually work:

>procedure TForm1.ComboBoxSelectionChange;
>begin
>  solve_world_hunger;
>  do_something_else;
>  .
>  .
>end;

>There are various other approaches, some of them perhaps better ot
>more efficient, and it depends on exactly the purpose of using this
>event and in what ways it will typically be triggered. But I hope this
>steers you in the right direction.

>Take care,
>Tom

>On Wed, 17 Nov 1999 15:50:16 GMT, bschl...@twcny.rr.com (Barry) wrote:

>>Hello All,

>>I have a simple (I hope) TComboBox question that escapes me. I am
>>using Delphi 3.0.

>>I have a TComboBox with names in it. I would like to be notified if
>>someone selects a name from the drop list. I would also like to
>>generate the same "selected" notification if someone types an ENTER
>>into the text portion or if someone clicks an external OK button.

>>The OnChange event seems to handle the drop list select, but will it
>>generate an OnChange with each char that someone types in the Edit
>>portion?

>>Is there a better way?

>>Thanks,
>>Barry.

Other Threads