Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9137555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:07:04+00:00 2026-06-17T09:07:04+00:00

I have 2 listboxes on a windows form and 2 button at the middle

  • 0

I have 2 listboxes on a windows form and 2 button at the middle of those boxes. one shows right other shows left. what I want to do is when I select some items lets say on listbox1 and click on the right showing button, I want to copy the items I selected on listbox1 to listbox 2 at the same indexes where I selected them on 1. I can do this, however I can’t copy the empty rows. This is how my listboxes looks like(i added the numbers just to track the lines easily here).

    ListBox1                Listbox2

1.  u                       a
2.
3.  l                       c
4.                          b
5.  m                       e
6.                          f
7.  n      >>(right button)          
8.                          c
9.  z      <<(left button)  t
10.                         q
11. s                         
12.                        
13. g                       b

now in these boxes, if I select any item with a text on it, and click on the button, it will copy the items to the other listbox. but if I select any line w/o text, any empty row, and click on the button, it won’t change anything. it won’t make the other box item empty. here’s the code, how I copy the rows.

private void button4_Click(object sender, EventArgs e)
{

    List<int> selectedItemIndexes = new List<int>();
    foreach (string o in listBox1.SelectedItems)
        selectedItemIndexes.Add(listBox1.Items.IndexOf(o));

    for (int i = 0; i < selectedItemIndexes.Count; i++)
    {
        listBox2.Items[selectedItemIndexes[i]] =  listBox1.Items[selectedItemIndexes[i]];
    }

    selectedItemIndexes.Clear();


}

private void button3_Click(object sender, EventArgs e)
{
    List<int> selectedItemIndexes = new List<int>();
    foreach (object o in listBox2.SelectedItems)
        selectedItemIndexes.Add(listBox2.Items.IndexOf(o));

    for (int i = 0; i < selectedItemIndexes.Count; i++)
    {
        listBox1.Items[selectedItemIndexes[i]] = listBox2.Items[selectedItemIndexes[i]];
    }

    selectedItemIndexes.Clear();
}

how can I also copy the empty rows?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:07:04+00:00Added an answer on June 17, 2026 at 9:07 am

    That is because in following line

    foreach (string o in listBox1.SelectedItems)
    selectedItemIndexes.Add(listBox1.Items.IndexOf(o));

    You will get empty string, and it will always return the first index of the empty string.

    The solution is to use the selectedIndices as following:

         foreach (int selectedItemInd in listBox1.SelectedIndices)
            {
                selectedItemIndexes.Add(selectedItemInd);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# Windows Forms project with a Form containing 2 ListBoxes and
I have a button on my windows form that calls the RunWorkerAsync() method, this
I have a Windows Form with two list boxes on it. Also there is
I have two listbox in windows Form c#, the first one has all valid
I have a form with three panels. The top panel contains listboxes, the middle
I have ListBox called lstProductGroups. On a simple Windows Form, a method called GetGroups
I have 2 listboxes in my XAML and both of them are multi select.
I have a form with a dropdownbox two listboxes and two buttons on it.
I have a VERY simple windows form that the user uses to manage Stores.
I have a Windows Form and a class with two simple methods that run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.