ListView / SelectedIndexChanged problems

G

Guest

Here's a strange one.
I have a screen with a listView on it, in Details view, which basically
shows a list of customers.
If I click on one of these customers, a second screen appears, showing
details about that customer. Click on OK, and it returns to the listview
screen.

I add a simple function to the ListView's SelectedIndexChanged
event, which checks to see which item has been clicked on, and
then displays the second (Customer Details) screen:


private void lvCustomers_SelectedIndexChanged(object sender,
System.EventArgs e)
{
int m_selectedRow = -1;
for (int n=0; n<lvServiceReqs.Items.Count; n++)
if (lvServiceReqs.Items[n].Selected)
m_selectedRow = n;

if (m_selectedRow == -1)
return;

// I have a DataSet, dsCustomers, which contains one record
// per row shown in the ListView
m_selectedCustomerId =
int.Parse(dsCustomers.Tables[0].Rows[m_selectedRow].ItemArray[1].ToString());

frmCustomerDetails dlg = new frmCustomerDetails(m_selectedCustomerId);
dlg.ShowDialog();
}


When I click on a customer, no problem, the second screen appears.
I can click on OK and return to the List screen.
But if I try to click on a second customer, it seems to ignore the
click. Clicking a *second* time works okay though - it'll then show
details of the second customer I clicked on.
Click on OK on the customer details screen, and once again, the
list view appears, clicking on a customer will be ignored, but click
again, and it'll work okay.

This works fine when using the DataGrid control... why does
this problem occur with ListViews ?


Michael
Bristol, UK
 
D

Daniel Moth

I suggest you look up: SelectedIndices andalso ItemActivate/Activation [all
listview properties/events]

Note that SelIndxChanged fires twice (once when no items are selected).

Finally, I vaguely recall a situation where exceptions in the listview
eventhandlers got swallowed, so explicitly inserting a try..catch might help
if that is the case.

Cheers
Daniel
 
G

Guest

Hi Daniel,

Thanks for the reply.

Yes, I noticed that the SelectedIndexChanged event fires twice, but I'm
finding that it fires twice on the *second* time I select one of the items
in the ListView. It doesn't fire at all when I tap on it the first time.

Your ItemActivate event works okay, but only when you double-tap
on a particular item. It doesn't get kicked off at all if you've only
tapped once.


I still reckon that my problem is due to the underlying
OnSelectedIndex function not kicking off.. so when you come back
into the ListView form and tap on an item, it, well, finishes off what
it'd started.
For example, at the moment, my SelectedIndexChanged code is
preventing the item you tap on from being highlighted. Is this because
I'm kicking off a second form ? I'm not sure.

(Sigh.)
I'm not having a good day ;-)


Mike


Daniel Moth said:
I suggest you look up: SelectedIndices andalso ItemActivate/Activation [all
listview properties/events]

Note that SelIndxChanged fires twice (once when no items are selected).

Finally, I vaguely recall a situation where exceptions in the listview
eventhandlers got swallowed, so explicitly inserting a try..catch might help
if that is the case.

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


Michael Gledhill said:
Here's a strange one.
I have a screen with a listView on it, in Details view, which basically
shows a list of customers.
If I click on one of these customers, a second screen appears, showing
details about that customer. Click on OK, and it returns to the listview
screen.

I add a simple function to the ListView's SelectedIndexChanged
event, which checks to see which item has been clicked on, and
then displays the second (Customer Details) screen:


private void lvCustomers_SelectedIndexChanged(object sender,
System.EventArgs e)
{
int m_selectedRow = -1;
for (int n=0; n<lvServiceReqs.Items.Count; n++)
if (lvServiceReqs.Items[n].Selected)
m_selectedRow = n;

if (m_selectedRow == -1)
return;

// I have a DataSet, dsCustomers, which contains one record
// per row shown in the ListView
m_selectedCustomerId =
int.Parse(dsCustomers.Tables[0].Rows[m_selectedRow].ItemArray[1].ToString());

frmCustomerDetails dlg = new frmCustomerDetails(m_selectedCustomerId);
dlg.ShowDialog();
}


When I click on a customer, no problem, the second screen appears.
I can click on OK and return to the List screen.
But if I try to click on a second customer, it seems to ignore the
click. Clicking a *second* time works okay though - it'll then show
details of the second customer I clicked on.
Click on OK on the customer details screen, and once again, the
list view appears, clicking on a customer will be ignored, but click
again, and it'll work okay.

This works fine when using the DataGrid control... why does
this problem occur with ListViews ?


Michael
Bristol, UK
 
D

Daniel Moth

I mentioned the Activation property and ItemActivate event because I think
it is a better route to follow. The Activation property has interesting
enumeration values: check them out.

I mentioned SelectedIndices because in your code you are trying to determine
which item is selected; rather than the loop do something like this:
m_selectedRow = lvServiceReqs.Items[lvServiceReqs.SelectedIndices[0]].Index;

I mentioned the event firing twice because usually we don't care about it
firing when there is no item selected so we do something like this at the
top of the event handler:
if (lvServiceReqs.SelectedIndices.Count==0){
return;
}

Found the reference to the exception being swallowed:
http://groups.google.com/[email protected]&rnum=1

When asking for a small repro I meant something that doesn't have datasets
and custom forms that you show as dialog, for example. In any case I think
the above gives you enough to go on.

Finally, and I am only being picky here, I suggest if your listview is named
lvServiceReqs that the event handler name does not start with lvCustomers_
....

If any of the suggestion above don't solve the problem feel free to post
back. Good luck :)

Cheers
Daniel

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


Michael Gledhill said:
Hi Daniel,

Thanks for the reply.

Yes, I noticed that the SelectedIndexChanged event fires twice, but I'm
finding that it fires twice on the *second* time I select one of the items
in the ListView. It doesn't fire at all when I tap on it the first time.

Your ItemActivate event works okay, but only when you double-tap
on a particular item. It doesn't get kicked off at all if you've only
tapped once.


I still reckon that my problem is due to the underlying
OnSelectedIndex function not kicking off.. so when you come back
into the ListView form and tap on an item, it, well, finishes off what
it'd started.
For example, at the moment, my SelectedIndexChanged code is
preventing the item you tap on from being highlighted. Is this because
I'm kicking off a second form ? I'm not sure.

(Sigh.)
I'm not having a good day ;-)


Mike


Daniel Moth said:
I suggest you look up: SelectedIndices andalso ItemActivate/Activation
[all
listview properties/events]

Note that SelIndxChanged fires twice (once when no items are selected).

Finally, I vaguely recall a situation where exceptions in the listview
eventhandlers got swallowed, so explicitly inserting a try..catch might
help
if that is the case.

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


Michael Gledhill said:
Here's a strange one.
I have a screen with a listView on it, in Details view, which basically
shows a list of customers.
If I click on one of these customers, a second screen appears, showing
details about that customer. Click on OK, and it returns to the
listview
screen.

I add a simple function to the ListView's SelectedIndexChanged
event, which checks to see which item has been clicked on, and
then displays the second (Customer Details) screen:


private void lvCustomers_SelectedIndexChanged(object sender,
System.EventArgs e)
{
int m_selectedRow = -1;
for (int n=0; n<lvServiceReqs.Items.Count; n++)
if (lvServiceReqs.Items[n].Selected)
m_selectedRow = n;

if (m_selectedRow == -1)
return;

// I have a DataSet, dsCustomers, which contains one record
// per row shown in the ListView
m_selectedCustomerId =
int.Parse(dsCustomers.Tables[0].Rows[m_selectedRow].ItemArray[1].ToString());

frmCustomerDetails dlg = new frmCustomerDetails(m_selectedCustomerId);
dlg.ShowDialog();
}


When I click on a customer, no problem, the second screen appears.
I can click on OK and return to the List screen.
But if I try to click on a second customer, it seems to ignore the
click. Clicking a *second* time works okay though - it'll then show
details of the second customer I clicked on.
Click on OK on the customer details screen, and once again, the
list view appears, clicking on a customer will be ignored, but click
again, and it'll work okay.

This works fine when using the DataGrid control... why does
this problem occur with ListViews ?


Michael
Bristol, UK
 
G

Guest

Hi Daniel,

Thanks for the tips.
I've added some breakpoints, and a Try..Catch, but no, no exceptions
seemed to have been triggered.

I have managed to get around my problem though.

Ahhh, this is such a tacky solution, it deserves an award.

Rather than kicking off the new dialog in the SelectedIndexChanged
function, I have removed these lines, and replaced them with a few
lines to set a timer going for about half a second.
When the timer expires, it kicks off a function to show the Customer
Details screen, for the customerID that I'd clicked on.

As far as programming examples go, this one's a terrible one to
learn from, but it works a treat. Even the row that you click on
gets selected now.


Timer tim;

private void lvServiceReqs_SelectedIndexChanged(object sender,
System.EventArgs e)
{
//
// [ Code to work out the ID of the customer we've clicked on..]
//

tim = new Timer();
tim.Interval = 200;
tim.Tick += new EventHandler(tim_Tick);
tim.Enabled = true;
}

private void tim_Tick(object sender, EventArgs e)
{
tim.Enabled = false;
frmCustomerDetails dlg = new frmCustomerDetails(m_selectedCustomerId);
dlg.ShowDialog();
}



P.S. The lvServiceReqs/lvCustomers thing... actually, my listView has
nothing to do with customers, I was trying to simplify it as an example,
but left the ServiceReqs thing in by accident..!
 
D

Daniel Moth

Glad you got it working. FYI and anybody else that comes across this thread
in the future:

Placing the following code in either a SelectedIndexChanged event or an
ItemActivate (where Activation=ItemActivation.OneClick) works as expected in
both PPC and WinCE emulators:

{
if (lvServiceReqs.SelectedIndices.Count==0){
return;
}

int m_selectedRow = -1;
m_selectedRow =
lvServiceReqs.Items[lvServiceReqs.SelectedIndices[0]].Index;

Form2 dlg = new Form2();
dlg.ShowDialog();
}

Cheers
Daniel
 

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

Top