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 7458467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:27:04+00:00 2026-05-29T13:27:04+00:00

I may confusing with the terminology, but here is what I’m trying to do:

  • 0

I may confusing with the terminology, but here is what I’m trying to do:

I have a move function which will end up moving selected items from one listBox to another. There are three list boxes with two right and left arrow buttons in between each to move the first list box items to the middle, the middle back to the first, etc.

My function accepts the different button names via sender and in the switch statement, I want to choose which listBox are the selected items going to be send from and where they will be sent to. If that makes sense.

The while loop at the bottom is going to perform the actual move, depending on what is set for the “to” and “from” listBoxes.

My question is how can I in the scope of this function reference the names of the three existing listBoxes in each of the cases of the switch statement? I know initializing a new listBox like I’ve done is wrong and will just create some more listBoxes. Maybe for this case the easiest thing is to explicitly put the while loop in each of the case statements, but for the future in a more complex scenario, I still would like to know how this is done.

private void move(object sender, EventArgs e)
{
    Button thisButton = sender as Button;
    ListBox to = new ListBox();
    ListBox from = new ListBox();

    switch (thisButton.Name)
    {
        case "queueToProgressButton":
            to.Name = "queueListBox";
            from.Name = "progressListBox";
            break;
        case "progressToQueueButton":                    
            to.Name = "queueListBox";
            from.Name = "progressListBox";
            break;
        case "progressToCompletedButton":                    
            to.Name = "queueListBox";
            from.Name = "progressListBox";
            break;
        case "completedToProgressButton":                    
            to.Name = "queueListBox";
            from.Name = "progressListBox";
            break;

    }
    while (from.SelectedItems.Count > 0)
    {
        to.Items.Add(from.SelectedItem);
        from.Items.Remove(from.SelectedItem);
    }
}
  • 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-05-29T13:27:05+00:00Added an answer on May 29, 2026 at 1:27 pm

    You should be using references to the existing list boxes, not allocating new ones. Also, your four branches of the switch are identical in the code you posted; I don’t think that’s what you intended. I made adjustments to the code based on what I think you meant to do in the switch.

    Try something like this instead:

    private void move(object sender, EventArgs e)
    {
        Button thisButton = sender as Button;
        ListBox toListBox, fromListBox;
    
        switch (thisButton.Name)
        {
            case "queueToProgressButton":
                toListBox = progressListBox; // notice no "", but actual references
                fromListBox = queueListBox;
                break;
            case "progressToQueueButton":                    
                toListBox = queueListBox;
                fromListBox = progressListBox;
                break;
            case "progressToCompletedButton":                    
                toListBox = completedListBox;
                fromListBox = progressListBox;
                break;
            case "completedToProgressButton":                    
                toListBox = completedListBox;
                fromListBox = progressListBox;
                break;
            // Should add a default just in case neither 
            // toListBox or fromListBox is assigned here.
        }
    
        while (fromListBox.SelectedItems.Count > 0)
        {
            toListBox.Items.Add(fromListBox.SelectedItem);
            fromListBox.Items.Remove(fromListBox.SelectedItem);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SQL question which may be basic to some but is confusing
Title may be confusing, but here's explanation with code. Based on some conditions, I
The title may have been confusing, but please let me explain: Currently when I
The title may sound confusing. But here's my issue. I want to show something
It may be a bit confusing, but... Let's say I have a vector type
That may sound a little confusing. Basically, I have a function CCard newCard() {
I know the question title may be confusing, but here is what I am
Ok so my question title may have been a little confusing. Here's my example:
The title may be bit confusing but here is what I am facing I
The title may sound confusing but it actually isn't, I just didn't know how

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.