473,396 Members | 1,836 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Deleting last Listbox item

Hi,

I have a windows form with a listbox control. My code all works correctly
when deleting an item from the listbox except the last item. I get the
following message when trying to delete the last item:-

Specified argument was out of the range of valid values. Parameter name:
'64' is not a valid value for 'Value'.

The 64 reduces depending on how many listbox items are displayed.

Also (related) when deleting the last and only item from the listbox, the
error message changes to:

Specified argument was out of the range of valid values. Parameter name: '0'
is not a valid value for 'Value'.

My code is

private void _DoDelUser()
{

// Prompt the user first.
if (MessageBox.Show(this, "This will delete the selected user - continue?",
this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) !=
DialogResult.OK)
return;

// Get the current user index.
int userIndex = m_listBoxUsers.SelectedIndex;

// Should we ignore the the event?
if (userIndex == -1)
return;

// Get the identifier.
int userID = (Int32)m_userTable.Rows[userIndex]["user_id"];

// Delete the user.
UserManager.Delete(userID);

// Get the GUI state.
int index = m_listBoxUsers.SelectedIndex;

// Update the tables
m_userTable = UserManager.FindAll().Tables[0];

// Bind the GUI to the tables.
m_listBoxUsers.DataSource = m_userTable;
m_listBoxUsers.DisplayMember = "user_name";
m_listBoxUsers.SelectedIndex = index;

} // End _DoDelUser()

Thanks in advance and King Regards

Nov 17 '05 #1
1 7846
Hi,

It may give you error, especially if you delete the last one ( as you are
experimenting )
The thing is that you are saving the selectedIndex BEFORE delete it, then
later you are reassign it to the listbox , BUT the listbox's item count will
be less that before delete the element

Beside, what is the point in keep the selected index if that is the one you
are going to delete after all?

If what you want is to select the next one, well then you have to decide
what to do when the selected is the last one, either select none:
listBoxUsers.SelectedIndex = -1;

or just select the current last one
listBoxUsers.SelectedIndex = listBoxUsers.Items.Count-1;
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"jez123456" <je*******@discussions.microsoft.com> wrote in message
news:08**********************************@microsof t.com...
Hi,

I have a windows form with a listbox control. My code all works correctly
when deleting an item from the listbox except the last item. I get the
following message when trying to delete the last item:-

Specified argument was out of the range of valid values. Parameter name:
'64' is not a valid value for 'Value'.

The 64 reduces depending on how many listbox items are displayed.

Also (related) when deleting the last and only item from the listbox, the
error message changes to:

Specified argument was out of the range of valid values. Parameter name:
'0'
is not a valid value for 'Value'.

My code is

private void _DoDelUser()
{

// Prompt the user first.
if (MessageBox.Show(this, "This will delete the selected user -
continue?",
this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) !=
DialogResult.OK)
return;

// Get the current user index.
int userIndex = m_listBoxUsers.SelectedIndex;

// Should we ignore the the event?
if (userIndex == -1)
return;

// Get the identifier.
int userID = (Int32)m_userTable.Rows[userIndex]["user_id"];

// Delete the user.
UserManager.Delete(userID);

// Get the GUI state.
int index = m_listBoxUsers.SelectedIndex;

// Update the tables
m_userTable = UserManager.FindAll().Tables[0];

// Bind the GUI to the tables.
m_listBoxUsers.DataSource = m_userTable;
m_listBoxUsers.DisplayMember = "user_name";
m_listBoxUsers.SelectedIndex = index;

} // End _DoDelUser()

Thanks in advance and King Regards

Nov 17 '05 #2

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

Similar topics

2
by: Peder Y | last post by:
Anyone knows if there is some kind of property or function that will return the last selected/deselected item/index in a multiselect ListBox? SelectedIndex will point to first index in the...
5
by: GG | last post by:
I know I can scroll to the last item in the listbox by calling SetSelected all the time What I need is when I add items through the Items.Add or Items.AddRange to be called automatically and go...
1
by: Tim | last post by:
Hi, I'm very new to .NET and am programming in C#. I have a web application where i have two list boxes. Its kind of like a shopping card where you can add items from one 'locations' list box to...
6
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset...
10
by: A_PK | last post by:
I am writing mobile application using Vb.net when i click the last row of list view, and try to delete it.... will promtpy the following message "Additional information:...
5
by: John Veldthuis | last post by:
My code works perfectly 100% when adding items to my ArrayList and updating the listbox. Works perfectly when deleting an item in the ArrayList when it is not the last entry but if it is the last...
1
by: tsuki | last post by:
Hello, I have a problem concerning DOM and XML. The structure of my xml is the following: <?xml version="1.0" encoding="utf-8"?> <website> <language1> <seite> <bereich1>
0
by: inandout | last post by:
Hi, I was wondering if anyone can help me with this. I have a listview that is specified to a size of: 348W x 308H When items are added to it, no problems. However when items continue to be...
5
by: Academia | last post by:
(If you've seen this in the drawing NG, sorry. I inadvertently sent it there.) I have a listbox populated with Objects. The Class has a String field that ToString returns. I assume that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.