0

This piece of code gets the information from a customer and sorts it out to different classes (types of car - standard or wide). I was wondering how can I use this information and be able to to list it in a listBox located on a different Windows Form?

private void buttonSave_Click(object sender, EventArgs e)
{   
    if (radioWide.Checked)
    {
        Car newCar = new Wide(textCustomerName.Text, textCarLicense.Text, textCarMake.Text, textCarColour.Text);
        currentCar = newCar;
    }
    else if (radioStandard.Checked)
    {
        Car newCar = new Standard(textCustomerName.Text, textCarLicense.Text, textCarMake.Text, textCarColour.Text);
        currentCar = newCar;
    }

"(textCustomerName.Text, textCarLicense.Text, textCarMake.Text, textCarColour.Text);" - these are text boxes that the user will type in, and radioWide and radioStandard are the radio buttons to give you a little visualization

My listbox right now is empty and has different columns to separate the information that I have received from the user this is what the code with the columns currently looks like

    //Set up columns in ListView
    public static void SetListColumns(ListView listCar)
    {   

        listCar.Columns.Add("Customer Name");
        listCar.Columns.Add("Car License Plate");
        listCar.Columns.Add("Car Make");
        listCar.Columns.Add("Car Colour");
        listCar.Columns.Add("Parking Space");

    }


    //Set up columns in ListView
    public static void SetListColumns(ListView listCar)
    {   

        listCar.Columns.Add("Customer Name");
        listCar.Columns.Add("Car License Plate");
        listCar.Columns.Add("Car Make");
        listCar.Columns.Add("Car Colour");

    }

    //Closing the window using the Close button
    private void buttonClose_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    //Method to refresh view of the window
    private void RefreshView()
    {

        //Display decarled  columns customer variables
        ListAllEntries.SetListColumns(listWide);
        ListAllEntries.SetListColumns(listStandard);

    }

listStandard and listWide are both the names of the listView.

The only problem is that I'm not sure how to display the userInput and be able to print them in the listboxes

T.S.
  • 18,195
  • 11
  • 58
  • 78
Jay
  • 1
  • 2
  • I would bind the collection of objects to the listbox. Eg. http://stackoverflow.com/questions/2675067/binding-listbox-to-listobject Also, you did not mention what your interface is. It's important because binding to a control is done slightly differently, depending on which you are using. Eg. XAML vs WinForms – James Shaw Dec 11 '14 at 16:22

1 Answers1

0

I hope, I understood your question right. You need to collect info on one form and display it in another, correct?

Lets do some pseudo-coding

class CarInfo
{
   // here your properties of the car
}

// here you have your listview with accumulated car data
class CarDisplay : Form    
{

    private List<CarInfo> _carList = new List<CarInfo>(); 

    // here you popup the data entry form and wire to the saving callback
    void buttonShowDataForm_Click (sender, e)
    {
        // yes - this is best way to show forms because it will dispose them too
        using (f = new formDataCollect(CarInfoSubmitCallback))
        {
            f.ShowModal();
        }
    }

    // here you have your view populated and any operations that you want to add a car
    void CarInfoSubmitCallback (CarInfo info)
    {
        _carList.Add(info);
        // optionally here you can do your UI stuff or save to DB, whatever
    }

}


// this is your car data entry form
class CarDataEntry : Form
{

    private Action<CarInfo> _addCarCallback;

    // your constructor passes method that will be called later when user's data is to be saved. In your case, this method will be executed on the form that has your list
    public CarDataEntry (Action<CarInfo> callBack) 
    {
        _addCarCallback = callBack;
    }

    // here you fill your car object and invoke callback, so it executes on another form
    void buttonAddCar_Click (sender, e)
    {
         var ci = new CarInfo();
         ci.Name = txtName.Text;
         // and so on with properties
         . . . . . . 
         _callBack(ci);
    }


}

This is one way. I don't say there aren't others. You can use events and so on.

T.S.
  • 18,195
  • 11
  • 58
  • 78