Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > ListBox Problem

Reply
Thread Tools

ListBox Problem

 
 
Jack Lewis
Guest
Posts: n/a
 
      09-01-2005
Dear All.

I have a Web Form which has 2 ListBoxes and an Add Button. I have 4 items in
my First List Box. What I want to do is shift the selected Items from first
List Box to 2nd List Box.The selection mode of both of my ListBoxes is
"Multiple". I am able to add the selected Items from the 1st List Box to 2nd
List Box. But The problem is that when I have added the selected items from
1st List Box to 2nd List Box. the added Items still seem to appear in the 1st
List Box. This doesn't happen when the Selection Mode of my ListBox is
"Single". I want the selected Items to be deleted once the they have been
copied into 2nd List Box.
Below is my Code:


private void Page_Load(object sender, System.EventArgs e)
{ // Put user code to initialize the page here

if(!IsPostBack)

{

ListBox1.Items.Add("UK");
ListBox1.Items.Add("USA"); ListBox1.Items.Add("Ireland");
ListBox1.Items.Add("Wales");
}

}

private void btnAdd_Click(object sender, System.EventArgs e)
{
if (ListBox1.SelectedItem != null)
{
foreach(ListItem i in ListBox1.Items)
{
if(i.Selected==true)
{
strSelection = i.Text;

if (ListBox2.Items.FindByText(strSelection)== null )
{
ListBox2.Items.Add(strSelection);
ListBox2.SelectedIndex =0;

}

//*** Here is the Code where I am removing the Items from ListBox1**//


for(int counter = 0; counter<=ListBox1.Items.Count-1; counter++)
{
if (ListBox1.Items[counter].Selected == true)
{
ListBox1.Items.RemoveAt(counter);
}
}

I don't know whats wrong with the above code.But it doesn't seem to remove
the selected items from the 1st List Box properly. I mean it does remove
Items frm the 1st list Box. But not the all which I have selected.

Any help would be greatly appreciated.

cheers,
Jack

 
Reply With Quote
 
 
 
 
Microsoft
Guest
Posts: n/a
 
      09-02-2005
Change your for loop to count down instead of up:
for (int counter = ListBox1.Items.Count-1; counter >= 0; counter--)




"Jack Lewis" <Jack http://www.velocityreviews.com/forums/(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Dear All.
>
> I have a Web Form which has 2 ListBoxes and an Add Button. I have 4 items
> in
> my First List Box. What I want to do is shift the selected Items from
> first
> List Box to 2nd List Box.The selection mode of both of my ListBoxes is
> "Multiple". I am able to add the selected Items from the 1st List Box to
> 2nd
> List Box. But The problem is that when I have added the selected items
> from
> 1st List Box to 2nd List Box. the added Items still seem to appear in the
> 1st
> List Box. This doesn't happen when the Selection Mode of my ListBox is
> "Single". I want the selected Items to be deleted once the they have been
> copied into 2nd List Box.
> Below is my Code:
>
>
> private void Page_Load(object sender, System.EventArgs e)
> { // Put user code to initialize the page here
>
> if(!IsPostBack)
>
> {
>
> ListBox1.Items.Add("UK");
> ListBox1.Items.Add("USA"); ListBox1.Items.Add("Ireland");
> ListBox1.Items.Add("Wales");
> }
>
> }
>
> private void btnAdd_Click(object sender, System.EventArgs e)
> {
> if (ListBox1.SelectedItem != null)
> {
> foreach(ListItem i in ListBox1.Items)
> {
> if(i.Selected==true)
> {
> strSelection = i.Text;
>
> if (ListBox2.Items.FindByText(strSelection)== null )
> {
> ListBox2.Items.Add(strSelection);
> ListBox2.SelectedIndex =0;
>
> }
>
> //*** Here is the Code where I am removing the Items from ListBox1**//
>
>
> for(int counter = 0; counter<=ListBox1.Items.Count-1; counter++)
> {
> if (ListBox1.Items[counter].Selected == true)
> {
> ListBox1.Items.RemoveAt(counter);
> }
> }
>
> I don't know whats wrong with the above code.But it doesn't seem to remove
> the selected items from the 1st List Box properly. I mean it does remove
> Items frm the 1st list Box. But not the all which I have selected.
>
> Any help would be greatly appreciated.
>
> cheers,
> Jack
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Listbox selection to populate another listbox? Chris Kettenbach ASP .Net 3 06-16-2005 09:19 PM
Listbox item added with client scripts not submitting with ASP:Listbox Simon Prince ASP .Net 2 10-19-2004 04:11 PM
How do I move all items in a listbox to another listbox kent ASP .Net 1 05-03-2004 12:17 AM
click listbox and refresh another listbox DC Gringo ASP .Net 0 04-06-2004 02:13 AM
Re: now desparate! - 1st listbox contents disappears when 2nd listbox appears? blenderdude ASP .Net 0 08-03-2003 10:18 PM



Advertisments