Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> C# >> Move Items from one ListBox to another in CSharp .NET
Move Items from one ListBox to another in CSharp .NET
Move Items from one ListBox to another in CSharp .NET


by Haider M Rizvi on 7/16/2010 9:20:01 PM

Views: 6159       Comments: 0

Move Items from one ListBox to another in C#

To move items from ListBox1 to ListBox2 and vice-versa, we would have to write code for four buttons.

·         Forward: to move selected item in listbox1 to listbox2.

·         Forward All: to move all items from listbox1 to listbox2

·         Remove: to remove item from listbox2 and add in listbox1

·         Remove All: to remove all items in listbox2 and add in listbox1

Forward

private void btnforward_Click(object sender, EventArgs e)

        {

            try

            {

                if (listBox1.Items.Count > 0)

                {

// checking whether listbix1 has items or not if yes then moving items

                    listBox2.Items.Add(listBox1.SelectedItem.ToString());

                    listBox1.Items.Remove(listBox1.SelectedItem);

                }               

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

//selecting first item of listbox.

            if(listBox1.Items.Count>0)

                listBox1.SelectedIndex = 0;

            listBox2.SelectedIndex = listBox2.Items.Count - 1;

        }

 

Forward All

 

        private void btnForwardAll_Click(object sender, EventArgs e)

        {

            try

            {

   //moving all items from listbox1 to listbox2

                listBox2.Items.AddRange(listBox1.Items);

                listBox1.Items.Clear();

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

            listBox2.SelectedIndex = 0;

        }

Remove

        private void btnRemove_Click(object sender, EventArgs e)

        {

            try

            {

                if (listBox2.Items.Count > 0)

{

//checking whether listbox2 has items or not. If yes then moving

//selected item from listbox2 to listbox1

                    listBox1.Items.Add(listBox2.SelectedItem.ToString());

                    listBox2.Items.Remove(listBox2.SelectedItem);

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

            if (listBox2.Items.Count > 0)

                listBox2.SelectedIndex = 0;

            listBox1.SelectedIndex = listBox1.Items.Count - 1;

 

        }

 

Remove All

        private void btnRemoveAll_Click(object sender, EventArgs e)

        {

            try

            {

//moving all items from listbox2 to listbox1

                listBox1.Items.AddRange(listBox2.Items);

                listBox2.Items.Clear();

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

 

            listBox1.SelectedIndex = 0;

 

        }

 

       

 

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Haider M RizviRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 2649
Advertisement
MindStick DataConver
Advertise with Us
  
Copyright © 2013MindStick. All Rights Reserved.