Obtaining the index of an array item from inside an event

  • Thread starter Thread starter bouy78
  • Start date Start date
B

bouy78

Quick Question Seems to be an easy task, but apparently I'm not very
bright. I have an array of checkboxes, created at runtime and
inserted in a panel. Upon creation, they're each attached to the same
CheckChanged event. Within this event, how do I know the index of the
checkbox that triggered it? Thanks in advance.
 
Quick Question Seems to be an easy task, but apparently I'm not very
bright. I have an array of checkboxes, created at runtime and
inserted in a panel. Upon creation, they're each attached to the same
CheckChanged event. Within this event, how do I know the index of the
checkbox that triggered it? Thanks in advance.

The "sender" parameter of the delegate method subscribed to the event is
the reference to the instance of the checkbox that is generating the event.

In the simplest case, you could just use Array.IndexOf() on the array of
checkboxes, using that reference, to determine the index.

Of course, if the reason you want the index is so that you can get the
reference to the checkbox instance, well...obviously in that case all
you have to do is use the "sender" parameter itself. :)

Pete
 
"sender" (the first event-arg) should be your CheckBox. So cast (if
necessary) and locate; in the case of an array, you can use:

int index = Array.IndexOf<CheckBox>(myArray, (CheckBox)sender);

With a List<> you can just use IndexOf directly.

Marc
 
"sender" (the first event-arg) should be your CheckBox. So cast (if
necessary) and locate; in the case of an array, you can use:

int index = Array.IndexOf<CheckBox>(myArray, (CheckBox)sender);

With a List<> you can just use IndexOf directly.

Marc


Thanks..not quite sure what to do, but I'll figure it out

These CheckBoxes have indexes that correspond to another object.
(there not part of the class because this is just a test app and I
dont want to convolute the objects any more than they are) when a box
is checked it updates. Simple enough....
The 'sender' object has the checkbox text in it (my objects also have
this unique text). I can see this when I step through, but I cant cast
the sender to anything useful.

protected void checkboxarray_SelectedIndexChanged(object sender,
EventArgs e)//autopostback event of checkboxes
{

i = 0;
foreach (DataRow aRow in myDataSet.Tables[0].Rows)
{
if (this.myClass.myText == (checkboxarray)sender //
need to extract the checkbox text here
this.myClass.myText.MarkedWhen = DateTime.Now;
i++;
}
 
thanks...and sorry for such a dumb question.

"sender" (the first event-arg) should be your CheckBox. So cast (if
necessary) and locate; in the case of an array, you can use:
int index = Array.IndexOf<CheckBox>(myArray, (CheckBox)sender);
With a List<> you can just use IndexOf directly.

Thanks..not quite sure what to do, but I'll figure it out

These CheckBoxes have indexes that correspond to another object.
(there not part of the class because this is just a test app and I
dont want to convolute the objects any more than they are) when a box
is checked it updates. Simple enough....
The 'sender' object has the checkbox text in it (my objects also have
this unique text). I can see this when I step through, but I cant cast
the sender to anything useful.

protected void checkboxarray_SelectedIndexChanged(object sender,
EventArgs e)//autopostback event of checkboxes
{

i = 0;
foreach (DataRow aRow in myDataSet.Tables[0].Rows)
{
if (this.myClass.myText == (checkboxarray)sender //
need to extract the checkbox text here
this.myClass.myText.MarkedWhen = DateTime.Now;
i++;
}
 
[...]
The 'sender' object has the checkbox text in it (my objects also have
this unique text). I can see this when I step through, but I cant cast
the sender to anything useful.

Are you sure? In the debugger, it will show you the Name property of
the control, but it's still a CheckBox control instance, and you should
be able to cast it to a CheckBox (not that you really need to...the
generic version of IndexOf that Marc suggests is not strictly required,
though it is IMHO a little nicer).

Your code should be able to work like this:

CheckBox[] _checkBoxArray;

protected void checkboxarray_SelectedIndexChanged(object sender,
EventArgs e)//autopostback event of checkboxes
{
this.myClass.MyText[Array.IndexOf(_checkBoxArray,
sender)].MarkedWhen = DateTime.Now;
}

Note: I don't know what you really call your array of CheckBox
instances. The above code assumes it's "_checkBoxArray", so you'd just
replace that with whatever it actually is.

Note: the code you posted had a line that appeared to be trying to cast
the "sender" to the actual array. This is wrong...the sender is the
individual control, not the array.

Pete
 
Quick Question Seems to be an easy task, but apparently I'm not very
bright. I have an array of checkboxes, created at runtime and
inserted in a panel. Upon creation, they're each attached to the same
CheckChanged event. Within this event, how do I know the index of the
checkbox that triggered it? Thanks in advance.
Create a class that inherits from 'CheckBox' called 'IndexCheckBox'. In
'IndexCheckBox' have a property called 'index'. When you create the
CheckBox index it:

public class IndexCheckBox: CheckBox
{
private int index;

public int Index
{
get
{
return index;
}
set
{
index = value;
}
}
}

Cheers,

Cliff
 
Enkidu said:
Create a class that inherits from 'CheckBox' called 'IndexCheckBox'. In
'IndexCheckBox' have a property called 'index'. When you create the
CheckBox index it:

That seems like overkill to me. There's unlikely to be so many
instances that simply looking it up in the array is a problem. However,
even if that's a concern, one could just use the Tag property of the
control to store the index. I don't think there's a need to create a
whole subclass just for that purpose.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top