Custom parameters to dynamic event handler

  • Thread starter Thread starter Marco [Stinger]
  • Start date Start date
M

Marco [Stinger]

Hi

I'm developping a Compact Framework, VS 2003 C# project for WinCE 5.0

One functionality requires that I create dynamically some controls out of a
XML project file, so I have some controls arrays (Buttons, PictureBoxes,
ListBoxe
and so on..) and I create the controls dynamically reading the XML.

Some of this controls require to respon to the click event, witch is fairly
simple : Object.Click += new EventHandler(NomeHandler);

My problem is if I connect some (let's say) PictureBox[n].Click to the
same EventHandler I don't know witch PictureBox called the Event !!!!!

Being fairly simple ( ;) ) I though I was going to put the array ID in the
tag propiety
of the control, and read it in the Event Handles....but Compact Framework
DOEAS NOT
have the tag property ( sig sig ).

So I came up with 2 possible solutions :

1) BRAND NEW CONTROLS
I could create my controls (MarcoButton, MarcoList, MarcoPictureBox etc
etc ) adding the int ArID property, but I'd like to think there's a simpler
and faster
solution.

2) PASS CUSTOM PARAMETERS to the EVENT HANDLER
I should be able to pass (maybe using EventArgs) some custom parameters to
the EventHandles...I did some researches on the Internet but I haven't found
any
solution yet....


Con you help me on Solution 2 ??.

Thanks in Advance and have a nice WeekEnd

Marco
 
The sender parameter of EventHandler will tell you which control/object is
responsible for the event (the object identity is the reference to the
object itself). In most scenarios you want to do something with the object
or its properties so accessing the sender is enough.

If you really want to offer a Tag property (which is supported in netcf v2.0
btw), you can derive from the existing controls adding your own strongly
typed Tag property; first though I would look at existing properties (e.g.
Text) and see if they are not good enough.

Cheers
Daniel
 
Hi Daniel and thanks for the reply..

I know that the Sender parameter will tell me witch control fired the
event, but how can I know witch PictureBox fired it out of a
PictureBox array ???

Ciao
Marco



Daniel Moth said:
The sender parameter of EventHandler will tell you which control/object is
responsible for the event (the object identity is the reference to the
object itself). In most scenarios you want to do something with the object
or its properties so accessing the sender is enough.

If you really want to offer a Tag property (which is supported in netcf
v2.0 btw), you can derive from the existing controls adding your own
strongly typed Tag property; first though I would look at existing
properties (e.g. Text) and see if they are not good enough.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Marco said:
Hi

I'm developping a Compact Framework, VS 2003 C# project for WinCE 5.0

One functionality requires that I create dynamically some controls out of
a
XML project file, so I have some controls arrays (Buttons, PictureBoxes,
ListBoxe
and so on..) and I create the controls dynamically reading the XML.

Some of this controls require to respon to the click event, witch is
fairly
simple : Object.Click += new EventHandler(NomeHandler);

My problem is if I connect some (let's say) PictureBox[n].Click to the
same EventHandler I don't know witch PictureBox called the Event !!!!!

Being fairly simple ( ;) ) I though I was going to put the array ID in
the tag propiety
of the control, and read it in the Event Handles....but Compact Framework
DOEAS NOT
have the tag property ( sig sig ).

So I came up with 2 possible solutions :

1) BRAND NEW CONTROLS
I could create my controls (MarcoButton, MarcoList, MarcoPictureBox etc
etc ) adding the int ArID property, but I'd like to think there's a
simpler and faster
solution.

2) PASS CUSTOM PARAMETERS to the EVENT HANDLER
I should be able to pass (maybe using EventArgs) some custom parameters
to
the EventHandles...I did some researches on the Internet but I haven't
found any
solution yet....


Con you help me on Solution 2 ??.

Thanks in Advance and have a nice WeekEnd

Marco
 
I thought I already made my suggestions...

In my scenarios, accessing the sender is enough. You imply that is not
enough in your scenario but you don't say why you need to know which one it
is and how having the sender is not enough. If you expand on your *actual*
end user goal we may be able to help.

My other suggestion was to use another property. So if we take the Text
property as an example (you could have used the Location, Image etc), assign
the Text of each picturebox to some value (e.g. the index of the control in
the array) and then access that from the sender object to see which one it
is.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Marco said:
Hi Daniel and thanks for the reply..

I know that the Sender parameter will tell me witch control fired the
event, but how can I know witch PictureBox fired it out of a
PictureBox array ???

Ciao
Marco



Daniel Moth said:
The sender parameter of EventHandler will tell you which control/object
is responsible for the event (the object identity is the reference to the
object itself). In most scenarios you want to do something with the
object or its properties so accessing the sender is enough.

If you really want to offer a Tag property (which is supported in netcf
v2.0 btw), you can derive from the existing controls adding your own
strongly typed Tag property; first though I would look at existing
properties (e.g. Text) and see if they are not good enough.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Marco said:
Hi

I'm developping a Compact Framework, VS 2003 C# project for WinCE 5.0

One functionality requires that I create dynamically some controls out
of a
XML project file, so I have some controls arrays (Buttons, PictureBoxes,
ListBoxe
and so on..) and I create the controls dynamically reading the XML.

Some of this controls require to respon to the click event, witch is
fairly
simple : Object.Click += new EventHandler(NomeHandler);

My problem is if I connect some (let's say) PictureBox[n].Click to the
same EventHandler I don't know witch PictureBox called the Event !!!!!

Being fairly simple ( ;) ) I though I was going to put the array ID in
the tag propiety
of the control, and read it in the Event Handles....but Compact
Framework DOEAS NOT
have the tag property ( sig sig ).

So I came up with 2 possible solutions :

1) BRAND NEW CONTROLS
I could create my controls (MarcoButton, MarcoList, MarcoPictureBox etc
etc ) adding the int ArID property, but I'd like to think there's a
simpler and faster
solution.

2) PASS CUSTOM PARAMETERS to the EVENT HANDLER
I should be able to pass (maybe using EventArgs) some custom parameters
to
the EventHandles...I did some researches on the Internet but I haven't
found any
solution yet....


Con you help me on Solution 2 ??.

Thanks in Advance and have a nice WeekEnd

Marco
 
I need to know witch PictureBox fired the click event 'cause
the XML project files tells me that PB 1 most respond to
Click with procedure A, PB2 with procedure B and so on....

Unfortunatelly PictureBox in CF doesn't have any original
proprerty I can use to store the information I need.

But I followed you suggestion and tried to create MY PictureBox
this way (I hope it Helps):

public class MarcoPictureBox : PictureBox
{
private int prArrID;
public MarcoPictureBox ()
{
prArrID = -1;
}
public int ArID
{
get
{
return prArrID;
}
set
{
prArrID = value;
}
}
}

This way I have a full functioning PictureBox PLUS a ArID int
property I can use.

Again thanks fot the support and have a nice WE

Ciao
Marco


Daniel Moth said:
I thought I already made my suggestions...

In my scenarios, accessing the sender is enough. You imply that is not
enough in your scenario but you don't say why you need to know which one
it is and how having the sender is not enough. If you expand on your
*actual* end user goal we may be able to help.

My other suggestion was to use another property. So if we take the Text
property as an example (you could have used the Location, Image etc),
assign the Text of each picturebox to some value (e.g. the index of the
control in the array) and then access that from the sender object to see
which one it is.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Marco said:
Hi Daniel and thanks for the reply..

I know that the Sender parameter will tell me witch control fired the
event, but how can I know witch PictureBox fired it out of a
PictureBox array ???

Ciao
Marco



Daniel Moth said:
The sender parameter of EventHandler will tell you which control/object
is responsible for the event (the object identity is the reference to
the object itself). In most scenarios you want to do something with the
object or its properties so accessing the sender is enough.

If you really want to offer a Tag property (which is supported in netcf
v2.0 btw), you can derive from the existing controls adding your own
strongly typed Tag property; first though I would look at existing
properties (e.g. Text) and see if they are not good enough.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


message Hi

I'm developping a Compact Framework, VS 2003 C# project for WinCE 5.0

One functionality requires that I create dynamically some controls out
of a
XML project file, so I have some controls arrays (Buttons,
PictureBoxes, ListBoxe
and so on..) and I create the controls dynamically reading the XML.

Some of this controls require to respon to the click event, witch is
fairly
simple : Object.Click += new EventHandler(NomeHandler);

My problem is if I connect some (let's say) PictureBox[n].Click to the
same EventHandler I don't know witch PictureBox called the Event !!!!!

Being fairly simple ( ;) ) I though I was going to put the array ID in
the tag propiety
of the control, and read it in the Event Handles....but Compact
Framework DOEAS NOT
have the tag property ( sig sig ).

So I came up with 2 possible solutions :

1) BRAND NEW CONTROLS
I could create my controls (MarcoButton, MarcoList, MarcoPictureBox etc
etc ) adding the int ArID property, but I'd like to think there's a
simpler and faster
solution.

2) PASS CUSTOM PARAMETERS to the EVENT HANDLER
I should be able to pass (maybe using EventArgs) some custom parameters
to
the EventHandles...I did some researches on the Internet but I haven't
found any
solution yet....


Con you help me on Solution 2 ??.

Thanks in Advance and have a nice WeekEnd

Marco
 
1. If what you really wanted was to respond to different picturebox clicks
with different procedures why not add different event handlers for each?
2. Why not use the (admittedly hack) of using the Text property (and convert
the string to int and vice versa)? Then you don't have to derive your own
control...

Anyway, if you have a solution that works then no need to change it I
guess...

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Marco said:
I need to know witch PictureBox fired the click event 'cause
the XML project files tells me that PB 1 most respond to
Click with procedure A, PB2 with procedure B and so on....

Unfortunatelly PictureBox in CF doesn't have any original
proprerty I can use to store the information I need.

But I followed you suggestion and tried to create MY PictureBox
this way (I hope it Helps):

public class MarcoPictureBox : PictureBox
{
private int prArrID;
public MarcoPictureBox ()
{
prArrID = -1;
}
public int ArID
{
get
{
return prArrID;
}
set
{
prArrID = value;
}
}
}

This way I have a full functioning PictureBox PLUS a ArID int
property I can use.

Again thanks fot the support and have a nice WE

Ciao
Marco


Daniel Moth said:
I thought I already made my suggestions...

In my scenarios, accessing the sender is enough. You imply that is not
enough in your scenario but you don't say why you need to know which one
it is and how having the sender is not enough. If you expand on your
*actual* end user goal we may be able to help.

My other suggestion was to use another property. So if we take the Text
property as an example (you could have used the Location, Image etc),
assign the Text of each picturebox to some value (e.g. the index of the
control in the array) and then access that from the sender object to see
which one it is.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Marco said:
Hi Daniel and thanks for the reply..

I know that the Sender parameter will tell me witch control fired the
event, but how can I know witch PictureBox fired it out of a
PictureBox array ???

Ciao
Marco



"Daniel Moth" <[email protected]> ha scritto nel messaggio
The sender parameter of EventHandler will tell you which control/object
is responsible for the event (the object identity is the reference to
the object itself). In most scenarios you want to do something with the
object or its properties so accessing the sender is enough.

If you really want to offer a Tag property (which is supported in netcf
v2.0 btw), you can derive from the existing controls adding your own
strongly typed Tag property; first though I would look at existing
properties (e.g. Text) and see if they are not good enough.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


message Hi

I'm developping a Compact Framework, VS 2003 C# project for WinCE 5.0

One functionality requires that I create dynamically some controls out
of a
XML project file, so I have some controls arrays (Buttons,
PictureBoxes, ListBoxe
and so on..) and I create the controls dynamically reading the XML.

Some of this controls require to respon to the click event, witch is
fairly
simple : Object.Click += new EventHandler(NomeHandler);

My problem is if I connect some (let's say) PictureBox[n].Click to the
same EventHandler I don't know witch PictureBox called the Event !!!!!

Being fairly simple ( ;) ) I though I was going to put the array ID in
the tag propiety
of the control, and read it in the Event Handles....but Compact
Framework DOEAS NOT
have the tag property ( sig sig ).

So I came up with 2 possible solutions :

1) BRAND NEW CONTROLS
I could create my controls (MarcoButton, MarcoList, MarcoPictureBox
etc
etc ) adding the int ArID property, but I'd like to think there's a
simpler and faster
solution.

2) PASS CUSTOM PARAMETERS to the EVENT HANDLER
I should be able to pass (maybe using EventArgs) some custom
parameters to
the EventHandles...I did some researches on the Internet but I haven't
found any
solution yet....


Con you help me on Solution 2 ??.

Thanks in Advance and have a nice WeekEnd

Marco
 
Hi Daniel

Quick answers:

1. Because I don't know how many PictureBox I'm going to have on each page
(it's dynamically generated out of a XML configuration file....and I have
many pages
with differenct controls)

2. PictureBox has no Text property.

Anyway thanks for the effort

Ciao
Marco


Daniel Moth said:
1. If what you really wanted was to respond to different picturebox clicks
with different procedures why not add different event handlers for each?
2. Why not use the (admittedly hack) of using the Text property (and
convert the string to int and vice versa)? Then you don't have to derive
your own control...

Anyway, if you have a solution that works then no need to change it I
guess...

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Marco said:
I need to know witch PictureBox fired the click event 'cause
the XML project files tells me that PB 1 most respond to
Click with procedure A, PB2 with procedure B and so on....

Unfortunatelly PictureBox in CF doesn't have any original
proprerty I can use to store the information I need.

But I followed you suggestion and tried to create MY PictureBox
this way (I hope it Helps):

public class MarcoPictureBox : PictureBox
{
private int prArrID;
public MarcoPictureBox ()
{
prArrID = -1;
}
public int ArID
{
get
{
return prArrID;
}
set
{
prArrID = value;
}
}
}

This way I have a full functioning PictureBox PLUS a ArID int
property I can use.

Again thanks fot the support and have a nice WE

Ciao
Marco


Daniel Moth said:
I thought I already made my suggestions...

In my scenarios, accessing the sender is enough. You imply that is not
enough in your scenario but you don't say why you need to know which one
it is and how having the sender is not enough. If you expand on your
*actual* end user goal we may be able to help.

My other suggestion was to use another property. So if we take the Text
property as an example (you could have used the Location, Image etc),
assign the Text of each picturebox to some value (e.g. the index of the
control in the array) and then access that from the sender object to see
which one it is.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


message Hi Daniel and thanks for the reply..

I know that the Sender parameter will tell me witch control fired the
event, but how can I know witch PictureBox fired it out of a
PictureBox array ???

Ciao
Marco



"Daniel Moth" <[email protected]> ha scritto nel messaggio
The sender parameter of EventHandler will tell you which
control/object is responsible for the event (the object identity is
the reference to the object itself). In most scenarios you want to do
something with the object or its properties so accessing the sender is
enough.

If you really want to offer a Tag property (which is supported in
netcf v2.0 btw), you can derive from the existing controls adding your
own strongly typed Tag property; first though I would look at existing
properties (e.g. Text) and see if they are not good enough.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


message Hi

I'm developping a Compact Framework, VS 2003 C# project for WinCE
5.0

One functionality requires that I create dynamically some controls
out of a
XML project file, so I have some controls arrays (Buttons,
PictureBoxes, ListBoxe
and so on..) and I create the controls dynamically reading the XML.

Some of this controls require to respon to the click event, witch is
fairly
simple : Object.Click += new EventHandler(NomeHandler);

My problem is if I connect some (let's say) PictureBox[n].Click to
the
same EventHandler I don't know witch PictureBox called the Event
!!!!!

Being fairly simple ( ;) ) I though I was going to put the array ID
in the tag propiety
of the control, and read it in the Event Handles....but Compact
Framework DOEAS NOT
have the tag property ( sig sig ).

So I came up with 2 possible solutions :

1) BRAND NEW CONTROLS
I could create my controls (MarcoButton, MarcoList, MarcoPictureBox
etc
etc ) adding the int ArID property, but I'd like to think there's a
simpler and faster
solution.

2) PASS CUSTOM PARAMETERS to the EVENT HANDLER
I should be able to pass (maybe using EventArgs) some custom
parameters to
the EventHandles...I did some researches on the Internet but I
haven't found any
solution yet....


Con you help me on Solution 2 ??.

Thanks in Advance and have a nice WeekEnd

Marco
 
1. Because I don't know how many PictureBox I'm going to have on each page
I'd profile that at a worst case scenario as I suspect horrible
performance...
2. PictureBox has no Text property.
What makes you say that?

// Add two pictureboxes to your form
private void Form1_Load(object sender, System.EventArgs e) {
pictureBox1.Text = "hi";
pictureBox2.Text = "bye";
pictureBox1.Click+=new EventHandler(pictureBox1_Click);
pictureBox2.Click+=new EventHandler(pictureBox1_Click);
}

private void pictureBox1_Click(object sender, EventArgs e) {
MessageBox.Show(((PictureBox)sender).Text);
}

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Marco said:
Hi Daniel

Quick answers:

1. Because I don't know how many PictureBox I'm going to have on each page
(it's dynamically generated out of a XML configuration file....and I have
many pages
with differenct controls)

2. PictureBox has no Text property.

Anyway thanks for the effort

Ciao
Marco


Daniel Moth said:
1. If what you really wanted was to respond to different picturebox
clicks with different procedures why not add different event handlers for
each?
2. Why not use the (admittedly hack) of using the Text property (and
convert the string to int and vice versa)? Then you don't have to derive
your own control...

Anyway, if you have a solution that works then no need to change it I
guess...

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Marco said:
I need to know witch PictureBox fired the click event 'cause
the XML project files tells me that PB 1 most respond to
Click with procedure A, PB2 with procedure B and so on....

Unfortunatelly PictureBox in CF doesn't have any original
proprerty I can use to store the information I need.

But I followed you suggestion and tried to create MY PictureBox
this way (I hope it Helps):

public class MarcoPictureBox : PictureBox
{
private int prArrID;
public MarcoPictureBox ()
{
prArrID = -1;
}
public int ArID
{
get
{
return prArrID;
}
set
{
prArrID = value;
}
}
}

This way I have a full functioning PictureBox PLUS a ArID int
property I can use.

Again thanks fot the support and have a nice WE

Ciao
Marco


"Daniel Moth" <[email protected]> ha scritto nel messaggio
I thought I already made my suggestions...

In my scenarios, accessing the sender is enough. You imply that is not
enough in your scenario but you don't say why you need to know which
one it is and how having the sender is not enough. If you expand on
your *actual* end user goal we may be able to help.

My other suggestion was to use another property. So if we take the Text
property as an example (you could have used the Location, Image etc),
assign the Text of each picturebox to some value (e.g. the index of the
control in the array) and then access that from the sender object to
see which one it is.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


message Hi Daniel and thanks for the reply..

I know that the Sender parameter will tell me witch control fired the
event, but how can I know witch PictureBox fired it out of a
PictureBox array ???

Ciao
Marco



"Daniel Moth" <[email protected]> ha scritto nel messaggio
The sender parameter of EventHandler will tell you which
control/object is responsible for the event (the object identity is
the reference to the object itself). In most scenarios you want to do
something with the object or its properties so accessing the sender
is enough.

If you really want to offer a Tag property (which is supported in
netcf v2.0 btw), you can derive from the existing controls adding
your own strongly typed Tag property; first though I would look at
existing properties (e.g. Text) and see if they are not good enough.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


message Hi

I'm developping a Compact Framework, VS 2003 C# project for WinCE
5.0

One functionality requires that I create dynamically some controls
out of a
XML project file, so I have some controls arrays (Buttons,
PictureBoxes, ListBoxe
and so on..) and I create the controls dynamically reading the XML.

Some of this controls require to respon to the click event, witch is
fairly
simple : Object.Click += new EventHandler(NomeHandler);

My problem is if I connect some (let's say) PictureBox[n].Click to
the
same EventHandler I don't know witch PictureBox called the Event
!!!!!

Being fairly simple ( ;) ) I though I was going to put the array ID
in the tag propiety
of the control, and read it in the Event Handles....but Compact
Framework DOEAS NOT
have the tag property ( sig sig ).

So I came up with 2 possible solutions :

1) BRAND NEW CONTROLS
I could create my controls (MarcoButton, MarcoList, MarcoPictureBox
etc
etc ) adding the int ArID property, but I'd like to think there's a
simpler and faster
solution.

2) PASS CUSTOM PARAMETERS to the EVENT HANDLER
I should be able to pass (maybe using EventArgs) some custom
parameters to
the EventHandles...I did some researches on the Internet but I
haven't found any
solution yet....


Con you help me on Solution 2 ??.

Thanks in Advance and have a nice WeekEnd

Marco
 
I know that the Sender parameter will tell me witch control fired the
event, but how can I know witch PictureBox fired it out of a
PictureBox array ???

Doesn't the Equals() method work with controls? Try the following:

Control GetSenderFromArray(Control[] controls, Control sender)
{
for (int index = 0; (index < controls.Length); index++)
if (controls[index].Equals(sender))
return sender;
return null;
}

Christian
 
Control GetSenderFromArray(Control[] controls, Control sender)
{
for (int index = 0; (index < controls.Length); index++)
if (controls[index].Equals(sender))
return sender;
return null;
}

Oops ... Try this:

int GetControlIndex(Control[] controls, Control sender)
{
for (int index = 0; (index < controls.Length); index++)
if (controls[index].Equals(sender))
return index;
return -1;
}

Christian
 
Back
Top