Solved

C #.Net Windows application Transferring ListBox items to another ListBox

Posted on 2006-10-21
5
198 Views
Last Modified: 2010-04-16
Hello,
I have a listbox that stores items and I have another listbox which is empty. In the middle of these listboxes I have a button names ADD. I have created an array for the items stored in the first listbox. I would like to transfer the items stored in the 1st listbox to the 2nd listbox when the item is seleceted and the ADD button is pressed.
string[] strSubs;
strSubs = new string[]{"English", "Maths", "Soscial Sciences", "Physics",};

Thanks in advance,
Erdem
0
Comment
Question by:operatortobe
  • 4
5 Comments
 
LVL 29

Expert Comment

by:Nightman
ID: 17780118
you don't need the array. Simply place this in the onclick event of the button

   
      foreach (object o in comboBox1.Items)
      {
        comboBox2.Items.Add(o.ToString());
      }
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17780125
Sorry, I misread your question

add this in the onclick event handler
      comboBox2.Items.Add(comboBox1.SelectedItem.ToString());
0
 
LVL 29

Accepted Solution

by:
Nightman earned 250 total points
ID: 17780127
My first post will add EVERTYTHING
My second will add only the selected item
0
 

Author Comment

by:operatortobe
ID: 17780572
Hi there,
I just had a chance to check the adding items from listbox1 to listbox2. It works fine, however if the user doesn't choose ant item from the listbox1 and just hits the ADD button I get this following message;

"NUll Reference exception was not handled"
lstChosenSubs.Items.Remove(lstChosenSubs.SelectedItem.ToString());

I guess I need to use the try and catch to handle this exception,right? Any idea how should I use it?
Regards,
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17780833
Yes - check the value of the SelectedIndex - if -1 then there is nothing selected - simply do nothing.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Data Saving(2) 2 41
MVC5 Dataformat String for Phone not working 3 26
C# application error "Parameter Is Not Valid". 3 80
Reference issues in Visual Studio 3 0
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now