To start I enter some information into 4 textboxes which then the data is stored into a class.
I just add the class object into the list box to keep things simple and when i click on the object in the listbox I want it to show the original text that was entered into the text boxes. The objects all the have same name but they store different information based on what i enter and i want the original information to display into the textboxes when i click on each different object.

I'm thinking i need to use SelectedIndexChanged to do what i want but am not really sure how.


This is the class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace hw
{
    class Car
    {
         string make;
         string model;
         string year;
         string price;

         public void setMake(string _Make)
         {
             make = _Make;
             
         }
         public string getMake()
         {
             return make;
         }



         public void setModel(string _Model)
         {
             model = _Model;

         }
         public string getModel()
         {
             return model;
         }


         public void setYear(string _Year)
         {
             year = _Year;

         }
         public string getYear()
         {
             return year;
         }


         public void setPrice(string _Price)
         {
             price = _Price;

         }
         public string getPrice()
         {
             return price;
         }

    }
}

This is the form which contains the textboxes and listbox

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace hw
{

    public partial class frmAdd : Form
    {
        public frmAdd()
        {
            
            InitializeComponent();

             
        }


        Car car = new Car();
        
       

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            car.setMake(txtMake.Text);
            car.setModel(txtModel.Text);
            car.setYear(txtYear.Text);
            car.setPrice(txtPrice.Text);

            listBox1.Items.Add(car);
            
            
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

                     
            
        }

        
        
    }
}

Recommended Answers

All 3 Replies

Hi,

I think you should place all the stuff in your Car class into a container, so you can deal with it much more easily. Place them into a list or whatever you like more :)

Try this first and if you can't solve the upcoming problems ask your question here with a code snippet.

Good luck! =]

Hi,
Seems like you always add the same object to the ListBox, overwriting every time the same fields, that way you'll end up with the same data for all the list items.
Try creating a new object every time you submit.

you want to use the click event

private void listBox1_Click(object sender, EventArgs e)
        {
            Car car = (Car)listBox1.SelectedItem;
            txtMake.Text = car.getMake();
            txtModel.Text = car.getModel();
            txtPrice.Text = car.getPrice();
            txtYear.Text = car.getYear();
        }
    }

also, you do not want to create a car object in your constructor. Instead create it when you do the submit

private void button1_Click(object sender, EventArgs e)
        {
            Car car = new Car();
            car.setMake(txtMake.Text);            
            car.setModel(txtModel.Text);            
            car.setYear(txtYear.Text);            
            car.setPrice(txtPrice.Text);             
            listBox1.Items.Add(car);          
        }

Now if instead of adding it directoy to the listbox, you instead create a datasource, like List<Car> and then map the make to the DisplayName, you will get the name of the car to show in the listbox.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.