Home > Enterprise >  How to display details from an object to a TextBox
How to display details from an object to a TextBox

Time:01-07

I am making a program which will test if an airplane is allowed to fly or not. It's my very first personal project using Windows Form and C#. I made a lot of progress, however, I am having a huge problem with a feature I want to have. I have two forms called Main & FlightSetup and the second form uses a class called AircraftClass which contains constructors, behaviours, and properties.

form


PROBLEM

I will try to make it simple and understandable. I am calling class AircraftClass inside public void addaircraftButton_Click to make an object and store all necessary details like aircraftModel,airlineName, etc. However, I have a ListBox called aircraftList which shows only the aircraft name. The idea is to display all that data/details depending on what the user selects from the aircraftList inside a TextBox called detailsList (yes, I called it like that, but it's a textbox). This is when the issue happens, it is not displaying the correct information, and it's showing the properties value for AircraftClass


NOTES

aircraftList & detailsList are inside Main Form - I do know that there are some obvious mistakes here, but I am still learning, please go easy on me :(

Variables Inside Main Form

        int planeCount = 0; int tailNumber = 0; int planeIndex;
        string planeModel = "NONE"; string airline = "NONE";
        double distanceM = 0.0; double fuelG = 0.0; double maxKG = 0.0; double totalKG = 0.0;
        int passengers= 0; int bag1=0; int bag2 = 0;

Main Code (Only addbuttom method)

        public void addaircraftButton_Click(object sender, EventArgs e)
        {

            FlightSetup addAircraft = new FlightSetup(); //CREATES INFO INSIDE DETAILS

            string nl = "\r\n";

            using (addAircraft)
            {
                DialogResult result = addAircraft.ShowDialog();

                planeModel = addAircraft.planeModel_textbox.Text;
                airline = addAircraft.airline_textbox.Text;

                tailNumber = int.Parse(addAircraft.tailno_textbox.Text);
                passengers= int.Parse(addAircraft.passengerTextBox.Text);
                bag1 = int.Parse(addAircraft.carryonTextBox.Text);
                bag2= int.Parse(addAircraft.checkedBagsTextBox.Text);

                distanceM = double.Parse(addAircraft.distance_textbox.Text);
                fuelG = double.Parse(addAircraft.fuel_textbox.Text);
                maxKG = double.Parse(addAircraft.maxweight_textbox.Text);

                AircraftDetails test = new AircraftDetails(planeModel,airline,tailNumber,passengers,bag1,bag2,distanceM,fuelG,maxKG,totalKG);
                test.AircraftModel = addAircraft.planeModel_textbox.Text;
                MessageBox.Show("Flight Setup is COMPLETED! \n\nPerfom Unit Testing For Take-Off Permission", "FLIGHT SAVED!");

                planeCount  ;
                noaircraft_label.Text = planeCount.ToString();
                aircraftList.Items.Add(planeModel   " - REGULAR ROLE");
            }

        }

Main Code (Only aircraftList method)

  public void aircraftList_SelectedIndexChanged(object sender, EventArgs e) //PENDING SOLUTION
        {
            AircraftDetails getData = new AircraftDetails();
            StringBuilder insertData = new StringBuilder(String.Empty);
            string nl = "\r\n";

            planeIndex = aircraftList.SelectedIndex;

            if (planeIndex > -1)
            {
                insertData.Append(getData.AircraftModel.ToString());
                insertData.Append(nl);

                detailsList.Text = insertData.ToString();
            }
        }

AircraftClass Code

{
    public class AircraftDetails
    {
        string aircraftModel = "UNKNOWN";
        string airlineName = "UNKNOWN";

        int id = 0; //not in use

        int tailNumber = 0;
        int passengerNo = 0;
        int carrybag = 0;
        int checkbag = 0;
        double distance = 0.0;
        double fuel = 0.0;
        double mWeight = 0.0;
        double totalWeight = 0.0;

        int errorCount = 0;
        
        //Constructors
        public AircraftDetails()
        {
            aircraftModel = "UNKNOWN";
            airlineName = "UNKNOWN";
            id = 0;
            tailNumber = 0;
            passengerNo = 0;
            carrybag = 0;
            checkbag = 0;
            distance = 0.0;
            fuel = 0.0;
            mWeight = 0.0;
            totalWeight = 0.0;
        }
        public AircraftDetails(string aircraftModel, string airlineName, int tailNumber, int passengerNo, int carrybag, int checkbag, double distance, double fuel, double mWeight, double totalWeight)
        {
            this.aircraftModel = aircraftModel;
            this.airlineName = airlineName;
            this.tailNumber = tailNumber;
            this.passengerNo = passengerNo;
            this.carrybag = carrybag;
            this.checkbag = checkbag;
            this.distance = distance;
            this.fuel = fuel;
            this.mWeight = mWeight;
            this.totalWeight = totalWeight;
        }

        //BEHAVIOUR
        public override string ToString()
        {
            if (this.passengerNo >= 1)
            return "Aircraft: "   aircraftModel   "\r\n Tail Number:"   tailNumber   "\r\nOperator: "   airlineName   "\r\nAircraft: "   "\r\nFlight Distance: "   distance   "\r\nFuel: "   fuel  
                "\r\nMax Weight Allowed: "   mWeight   "\r\nTotal Weight: "   totalWeight   "\r\nPassengers: " passengerNo  "\r\nCarry-On Bags: " carrybag  "\r\nChecked Bags: " checkbag;
            else
            {
                errorCount  ;
                return "Passengers were less than 0\r\nIt means error was recorded\r\nError Count: " errorCount;
            }
        }

        //Properties
        public string AircraftModel
        {
            get { return aircraftModel; }
            set { aircraftModel = value; }
        }
        public string Airline
        {
            get { return airlineName; }
            set { airlineName = value; }
        }
        public int TailNumber
        {
            get { return tailNumber; }
            set { tailNumber = value; }
        }
        public int PassengerNo
        {
            get { return passengerNo; }
            set { passengerNo = value; }
        }
        public int OnBoardBags
        {
            get { return carrybag; }
            set { carrybag = value; }
        }
        public int CheckedBags
        {
            get { return checkbag; }
            set { checkbag = value; }
        }
        public double FlightDistance
        {
            get { return distance; }
            set { distance = value; }
        }
        public double Fuel
        {
            get { return fuel; }
            set { fuel = value; }
        }
        public double MaxWeight
        {
            get { return mWeight; }
            set { mWeight = value; }
        }
    }
}

CodePudding user response:

Your question is how to display details from an object to a TextBox.

I see in your code that you've done a good job of overriding the ToString method. In a sense, you answered your own question because that's what it takes!

But let's try and boil all that code into a show from button

One salient point is that ToString() can just be called on object because it's show from listbox

When a listbox is bound to BindingList<AircraftDetails> you can choose a property of your AircraftDetails class to show as the DisplayMember property. Add code to the main form's Load event to set up the data source and the ListBox:

public partial class MainForm : Form
{
    .
    .
    .

    BindingList<AircraftDetails> Details = new BindingList<AircraftDetails>();
    protected override void onl oad(EventArgs e)
    {
        base.OnLoad(e);
        aircraftDetails.DataSource = Details;
        aircraftDetails.DisplayMember = nameof(AircraftDetails.Flight);
        aircraftDetails.SelectedIndexChanged  = onAircraftDetailsSelection;

        // Add a few flights
        Details.Add(new AircraftDetails
        {
            Flight = "UA6026",
            Airline = Airline.United,
            Fuel = 45250,
            OnboardBags = 155,
            CheckedBags = 97,
        });
        Details.Add(new AircraftDetails
        {
            Flight = "UA8888",
            Airline = Airline.United,
            Fuel = 27620,
            OnboardBags = 301,
            CheckedBags = 134,
        });
        Details.Add(new AircraftDetails
        {
            Flight = "WN8610",
            Airline = Airline.Southwest,
            Fuel = 200,
            OnboardBags = 5,
            CheckedBags = 614,
        });
        aircraftDetails.SelectedIndex = -1;
    }

In response to selection changes, call this method to show object in textbox:

    private void onAircraftDetailsSelection(object? sender, EventArgs e)
    {
        if(aircraftDetails.SelectedItem != null)
        {
            textBoxMultiline.Text = aircraftDetails.SelectedItem.ToString();
        }
    }
}
  • Related