473,545 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Anyone: ListView Selected Index change seems to fire twice?


Hi All,
Is it normal that the ListView fires the selectedinexcha nge event
twice?

I have a LvLoaners list view item that updates 3 text boxes (code
below), when I click on an item. I have set MultiSelect to False.

The first click goes as planned, but all subsqequent clicks seem to
fire the event twice. The first firing triggers an ArgumentExcepti on,
resulting in nItem not being initialised. That's why I have the first
Catch Block.

Is this normal, or is something a little goofy?

-nick

Private Sub lvLoaners_Selec tedIndexChanged (ByVal sender As
System.Object, ByVal e As System.EventArg s) Handles
lvLoaners.Selec tedIndexChanged
Try

Dim nItem As Integer = lvLoaners.Selec tedIndices(0)

Dim lvItem As ListViewItem = lvLoaners.Items (nItem)

Me.txtEditLoane rName.Text = lvItem.SubItems (0).Text
Me.txtEditLoane rPhone.Text = lvItem.SubItems (1).Text
Me.txtEditLoane rEmail.Text = lvItem.SubItems (2).Text

Catch a As ArgumentExcepti on
Return

Catch ex As Exception
MessageBox.Show ("Error: " & ex.Message & " in
lvLoaners_Selec tedIndexChanged ()", "Error")

End Try
Nov 21 '05 #1
3 10314
Have you read the manual?

for the ListView.Select edIndexChanged event it says:

<quote>
In a multiple selection ListView control, this event occurs whenever an item
is removed or added to the list of selected items.
</quote>
"n. Smith" <ni***@yahoo.dk > wrote in message
news:g9******** *************** *********@4ax.c om...

Hi All,
Is it normal that the ListView fires the selectedinexcha nge event
twice?

I have a LvLoaners list view item that updates 3 text boxes (code
below), when I click on an item. I have set MultiSelect to False.

The first click goes as planned, but all subsqequent clicks seem to
fire the event twice. The first firing triggers an ArgumentExcepti on,
resulting in nItem not being initialised. That's why I have the first
Catch Block.

Is this normal, or is something a little goofy?

-nick

Private Sub lvLoaners_Selec tedIndexChanged (ByVal sender As
System.Object, ByVal e As System.EventArg s) Handles
lvLoaners.Selec tedIndexChanged
Try

Dim nItem As Integer = lvLoaners.Selec tedIndices(0)

Dim lvItem As ListViewItem = lvLoaners.Items (nItem)

Me.txtEditLoane rName.Text = lvItem.SubItems (0).Text
Me.txtEditLoane rPhone.Text = lvItem.SubItems (1).Text
Me.txtEditLoane rEmail.Text = lvItem.SubItems (2).Text

Catch a As ArgumentExcepti on
Return

Catch ex As Exception
MessageBox.Show ("Error: " & ex.Message & " in
lvLoaners_Selec tedIndexChanged ()", "Error")

End Try

Nov 21 '05 #2
"n. Smith" <ni***@yahoo.dk > schrieb:
Is it normal that the ListView fires the selectedinexcha nge event
twice?

I have a LvLoaners list view item that updates 3 text boxes (code
below), when I click on an item. I have set MultiSelect to False.

The first click goes as planned, but all subsqequent clicks seem to
fire the event twice. The first firing triggers an ArgumentExcepti on,
resulting in nItem not being initialised. That's why I have the first
Catch Block.


That's not a bug. It's a feature.

When the user initiates a selection change that effectively results in all
selected items being deselected and then one or more items being
(re)selected, the control will raise the event twice, first for deselecting
the items and then for selecting them again (although the user will only see
that as a single operation). You can avoid the exception by extending your
code like this:

\\\
Dim SourceControl As ListView = DirectCast(send er, ListView)
If SourceControl.S electedItems.Le ngth > 0 Then
...
End If
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

Mr Wagner,

Thanks a lot. An excellent solution.

Cheers,
Nick
On Tue, 3 May 2005 11:06:58 +0200, "Herfried K. Wagner [MVP]"
<hi************ ***@gmx.at> wrote:
"n. Smith" <ni***@yahoo.dk > schrieb:
Is it normal that the ListView fires the selectedinexcha nge event
twice?

I have a LvLoaners list view item that updates 3 text boxes (code
below), when I click on an item. I have set MultiSelect to False.

The first click goes as planned, but all subsqequent clicks seem to
fire the event twice. The first firing triggers an ArgumentExcepti on,
resulting in nItem not being initialised. That's why I have the first
Catch Block.


That's not a bug. It's a feature.

When the user initiates a selection change that effectively results in all
selected items being deselected and then one or more items being
(re)selected , the control will raise the event twice, first for deselecting
the items and then for selecting them again (although the user will only see
that as a single operation). You can avoid the exception by extending your
code like this:

\\\
Dim SourceControl As ListView = DirectCast(send er, ListView)
If SourceControl.S electedItems.Le ngth > 0 Then
...
End If
///


Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
50862
by: Steve Murphy | last post by:
Is there a way to set a selected index for a Listview in a WinForms application? Thanks in advance, Steve Murphy
2
5033
by: william_dudek | last post by:
I am becoming extremely frustrated with the event handlingin C#. I need to handle the OnSelectedIndex changed event and am getting the following error message when I build the application. C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\RecipieGuide\FormRecipe.cs(599):...
12
3968
by: moondaddy | last post by:
I have a dropdown combo box on a form and I have its AutoPostBack property set to true. When I select something from the list it does a postback and then the Page_Load event fires followed by it's SelectedIndexChanged event. However, when I go back to this control again and select the first item in the list, it does a postback and the...
0
1544
by: jw56578 | last post by:
I have a dropdownlist in a panel on the page. autopostback is set to true. when i choose the second option in the the list, the selected index event is triggered fine. when i go back and choose the first default value the page posts, but the selected index change event is not triggered. why might this be. thanks
5
13839
by: Steve Jones | last post by:
Hi there, I am working with web forms, and am finding that the standard list of combo box events is dramatically reduced when using the dropdownlist. For example, the only event that I can see that will trigger when a user selects a different menu item is 'selected index changed'. I can confirm with a separate button that the index has...
11
5789
by: Santosh | last post by:
Dear all , i am writting following code. if(Page.IsPostBack==false) { try { BindSectionDropDownlist();
2
2125
by: =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post by:
What can i use to fire the selected index. Can i use a button or link? i have the event wired but don't know how to fire it? -- (i''ll be asking a lot of these, but I find C# totally way cooler than vb and there''s no go''n back!!!) thanks (as always) kes
1
4326
by: Sobin Thomas | last post by:
Hi All....... I am a beginner in asp.net. How can I call a javascript function on the selected index change of Gridview ,in asp.net? Can you help me with sample code? Thanks in advance....
0
1997
by: khushi patel | last post by:
I am working on Lightbox that has dropdown on it. depending on ddl value div should be shown up pleae help <%@ Page Title="" Language="VB" MasterPageFile="~/MasterSite.master" AutoEventWireup="false" CodeFile="UsrLn.aspx.vb" Inherits="UsrLn" %> <%@ Register TagPrefix="custom" Namespace="CustomControls" %> <asp:Content ID="Content1"...
0
7478
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7410
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7668
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7923
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4960
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.