Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PLEASE HELP(URGENT): Remove Item from Lisbox

Status
Not open for further replies.

SmileyFace

Programmer
Sep 10, 2002
99
0
0
US
Hi everyone! I have spent hours trying to figure out whats wrong here. My patience is running out, it seems so simple but there is still a problem somewhere! I am basically using an add button to transfer dates selected in a calendar control to a listbox using.net. I also have a 'remove' button which is supposed to remove a selected item. Here is the problem...it removes an item alright...but it is ALWAYS the first listbox item, not the selected one!!

Here is the code..

For the button:

<asp:button id="btnRemove" runat="server" Font-Bold="True" Text="Remove"></asp:button>

The 'code behind' for this button is as follows:

Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
Dim counter As Integer
For counter = lstVacation.Items.Count - 1 To 0 Step -1

If lstVacation.Items(counter).Selected = True Then
lstVacation.Items.RemoveAt(counter)
End If
Next
End Sub


I think it has something to do with the value, but cannot figure that out. ANY HELP WILL BE GREATLY APPRECIATED...THANKS! Here is the code
 
2 suggestions. Look at the Selected Index member, and look at this Signature:

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Sorry RickGuy. I'm kinda new here. Will post there. But please be more specific when you say look at the selected index.
 
More specific:
Code:
Dim lstItems As ListBox
dim iSelectedItemIndex = lstItems.SelectedIndex()

"Gets of sets the zero-based index of the currently selected item in a System.Windows.Forms.ListBox"

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
My goal here is not to write code for every single problem. Some of these I can look at and see that a nudge in the right direction will help you out. In this case, the SelectedIndex member of the ListBox class. If you type the name of your list box and hit '.' (In VS.Net) the autolist will show the available members, if you select SelectedIndex, the tool tip that pops up gives you a description. If you are developing outside of VS.Net you should have a reference book/website handy and look through the ListBox members.

That being said, if I have code on hand, or see something that looks like it could be fun, educational, or of use in a project, I'm all for writing the code. There are many chunks of code on the site here that have either come from or gone into an app I was working on.

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
I am still not following. How do I use it with the code above? My listboxe lstVacation is defined like this..


<asp:listbox id="lstVacation" runat="server" Width="254px" Height="218px" SelectionMode="Multiple"></asp:listbox>
 
The simple answer is, You Don't. You go to the ASP.Net forum and ask someone there to write your code.

The other option is to look at what the SelectedIndex memeber returns and compare it to what your code is doing.

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top