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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:42:41+00:00 2026-05-22T01:42:41+00:00

I have two listBoxes. The first listbox contains the list of traffic violations. When

  • 0

I have two listBoxes. The first listbox contains the list of traffic violations. When you click the add button and execute the code, the listbox2 got this item “ListBoxTest.Violation”, not the item being displayed from the listBox1…

What is wrong with my code?

namespace ListBoxTest
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        private List<Violation> violationList = new List<Violation>();
        public MainForm()
        {
            InitializeComponent();
        }

        void MainFormLoad(object sender, EventArgs e)
        {
            LoadViolations(); // Initialize and add violations to violationList.
            listBox1.DataSource = violationList; // Set the DataSource property.
            listBox1.ValueMember = "Code";
            listBox1.DisplayMember = "Description";

        }

        void LoadViolations()
        {
            Violation violation;

            violation = new Violation("001", "Beating the red light");
            violationList.Add(violation);

            violation = new Violation("002", "Exceeding posted speed limit on the road");
            violationList.Add(violation);

            violation = new Violation("003", "Driving a vehicle without license to drive");
            violationList.Add(violation);

            violation = new Violation("004", "Driving a non registered vehicle");       
            violationList.Add(violation);

            violation = new Violation("005", "Vehicle has no plate number");
            violationList.Add(violation);
        }

        void BtnAddClick(object sender, EventArgs e)
        {
            listBox2.Items.Add(listBox1.SelectedItem); // Add item from listBox1 to listBox2;
        }
    }

    /// <summary>
    /// Violation Class
    /// Properties: Code, Description
    /// </summary>
    public class Violation
    {
        private string _code;
        private string _description;

        public Violation(string code, string description)
        {
            _code = code;
            _description = description;
        }

        public String Code
        {
            get { return _code; }
            set { _code = value; }
        }

        public String Description
        {
            get { return _description; }
            set { _description = value; }
        }
    }
}
  • 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-22T01:42:42+00:00Added an answer on May 22, 2026 at 1:42 am

    Type cast the selected item to Violation. This should fix the problem.
    Edit: I have modified the code to fix the issue.

    private void AddClick(object sender, EventArgs e)
    {
        // Set the DataSource property.          
        listBox2.ValueMember = "Code";
        listBox2.DisplayMember = "Description";    
        listBox2.Items.Add((Violation)listBox1.SelectedItem); 
    }    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two ListBoxes. First ListBox items are list of Products. and second ListBox
I have a two stacked listboxes with data(first ListBox contains <10 elements, second may
I have two ListBoxes, lstAvailableColors and lstSelectedColors. Between each listbox are two buttons, Add
I have two list boxes, a datagridview and an Add and Remove button. Listbox1
I have a vertical StackPanel with two elements: a Button and a ListBox. How
I have two panorama pages. The first one contains letters, the second one contains
If I have two listboxes, with a button between them, how do I update
I have two databound listboxes. The first only shows items that have been assigned
I have a dialog popup with a textearea, two listboxes and one button. By
i have two list boxes in my form and a button in between these

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.