Board index » delphi » Using the Sender:TObject parameter

Using the Sender:TObject parameter

Hi, I have a form with 4 comboBoxes. I want to assign the same OnChange for
all. The problem is that I must use the name of the comboBox on that event,
and of course they are differents. It is possible to do that, instead of
writing an event for each one?

With the sender parameter I can know the className and the classType.

procedure TfrmFiltros.cbFields1Change(Sender: TObject);
var
  c:integer;
begin
  ds.FieldDefs.Clear;
  if sender.ClassName = 'TComboBox' then
  begin
    if ds.FieldDefList.FieldDefs[cbFields1.ItemIndex + 1].DataType = ftMemo
then
    begin
      showMessage('Tipo de campo invlido para pesquisas.');
      exit;
    end;
  end;
end;

I have cbFieds1, cbFieds2, cbFieds3 and cbFieds4. How can I use that event
for every comboBox?

Thanks.

 

Re:Using the Sender:TObject parameter


Quote
Isildo Mendes wrote in message <8a5910$6...@bornews.borland.com>...
>Hi, I have a form with 4 comboBoxes. I want to assign the same OnChange for
>all. The problem is that I must use the name of the comboBox on that event,
>and of course they are differents. It is possible to do that, instead of
>writing an event for each one?

>With the sender parameter I can know the className and the classType.

>procedure TfrmFiltros.cbFields1Change(Sender: TObject);
>var
>  c:integer;
>begin
>  ds.FieldDefs.Clear;
>  if sender.ClassName = 'TComboBox' then
>  begin
>    if ds.FieldDefList.FieldDefs[cbFields1.ItemIndex + 1].DataType = ftMemo
>then
>    begin
>      showMessage('Tipo de campo invlido para pesquisas.');
>      exit;
>    end;
>  end;
>end;

>I have cbFieds1, cbFieds2, cbFieds3 and cbFieds4. How can I use that event
>for every comboBox?

Isildo,

This kind of code should work:

  if TComboBox(Sender) is cbFieds1 then
    // Execute code for cbFieds1
  else if TComboBox(Sender) is cbFieds2 then
    // Execute code for cbFieds2
  else ...

HTH,

Chris.
---------

Re:Using the Sender:TObject parameter


Quote
Isildo Mendes <isildomen...@net.sapo.pt> wrote in message

news:8a5910$6go1@bornews.borland.com...
The problem is that I must use the name of the comboBox on that event,

Quote
> and of course they are differents.

This is untrue,  the name of the event handler is immaterial.

try this:

Create your 1st combobox.
Write your OnChange handler.
Use ctrl-c to copy the 1st combo, paste it 3 times et voila !

Alternatively if you already have 4 combos on your form you can use the
object inspector to set the onchange event (either by typing in the
handler's name, or selecting it from the drop down list), to the code you
have already written.

Hope this helps
Regards Hugh

Re:Using the Sender:TObject parameter


Make one OnChange for one of the ComboBoxes
Assign the same OnChange in the properties of the other ComboBoxes.
In the OnChange typecast the Sender TComboBox(Sender). The name is then
TComboBox(Sender).Name.
Alternatively assign Sender to a variable bla: TCombobox or use as
operator.
Better read a book about Object Pascal. You seem to have problems to
grasp the concept.

Isildo Mendes schrieb:

Quote

> Hi, I have a form with 4 comboBoxes. I want to assign the same OnChange for
> all. The problem is that I must use the name of the comboBox on that event,
> and of course they are differents. It is possible to do that, instead of
> writing an event for each one?

> With the sender parameter I can know the className and the classType.

> procedure TfrmFiltros.cbFields1Change(Sender: TObject);
> var
>   c:integer;
> begin
>   ds.FieldDefs.Clear;
>   if sender.ClassName = 'TComboBox' then
>   begin
>     if ds.FieldDefList.FieldDefs[cbFields1.ItemIndex + 1].DataType = ftMemo
> then
>     begin
>       showMessage('Tipo de campo invlido para pesquisas.');
>       exit;
>     end;
>   end;
> end;

> I have cbFieds1, cbFieds2, cbFieds3 and cbFieds4. How can I use that event
> for every comboBox?

> Thanks.

Re:Using the Sender:TObject parameter


You could use then "TAG" to seperate the combos and make a typecast on then
Sender:.
..
 case tComboBox(Sender).Tag of
  1: ect.
..
Give each combo its own TAG

Isildo Mendes <isildomen...@net.sapo.pt> skrev i en
nyhedsmeddelelse:8a5910$6...@bornews.borland.com...

Quote
> Hi, I have a form with 4 comboBoxes. I want to assign the same OnChange
for
> all. The problem is that I must use the name of the comboBox on that
event,
> and of course they are differents. It is possible to do that, instead of
> writing an event for each one?

> With the sender parameter I can know the className and the classType.

> procedure TfrmFiltros.cbFields1Change(Sender: TObject);
> var
>   c:integer;
> begin
>   ds.FieldDefs.Clear;
>   if sender.ClassName = 'TComboBox' then
>   begin
>     if ds.FieldDefList.FieldDefs[cbFields1.ItemIndex + 1].DataType =
ftMemo
> then
>     begin
>       showMessage('Tipo de campo invlido para pesquisas.');
>       exit;
>     end;
>   end;
> end;

> I have cbFieds1, cbFieds2, cbFieds3 and cbFieds4. How can I use that event
> for every comboBox?

> Thanks.

Re:Using the Sender:TObject parameter


I would suggest using the Tag value of each combo box in the test

Of course with the sender parameter you can test for the className and the
classType. If it is a TComboBox then you can refine your "filter" and check
for the tag property. Of course, you should assign a different value for
each combo box let's say 1 to 4 and then use in synchronization with
cbFields1 through cbFields4.

Hope it helps!

Lina Scarpellini
===============
Genial Software Inc.    www.genial.qc.ca

Other Threads