我正在做一个程序,它将测试一架飞机是否被允许飞行。这是我第一个使用Windows窗体和C#的个人项目。我取得了很大的进步,但是,我有一个巨大的问题,我想有一个功能。我有两个窗体称为Main
和FlightSetup
,第二个窗体使用一个类称为AircraftClass
,它包含构造函数,行为,和性质。
- 问题**
我会尽量让它简单易懂。我在public void addaircraftButton_Click
中调用类AircraftClass
来创建一个对象并存储所有必要的细节,如aircraftModel
,airlineName
等。然而,我有一个名为aircraftList
的列表框,它只显示飞机名称。详细信息取决于用户从名为detailsList
的TextBox内的aircraftList
中选择的内容(是的,我是这样叫它的,但它是一个文本框)。这是当问题发生时,它没有显示正确的信息,它显示了AircraftClass
的属性值
- 注**
aircraftList
& detailsList
在Main
表单里面-我确实知道这里有一些明显的错误,但我还在学习,请对我宽容点:(Main
表单内的变量
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;
主代码(仅限addbuttom
方法)
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");
}
}
主代码(仅限aircraftList
方法)
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();
}
}
飞机等级代码
{
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; }
}
}
}
1条答案
按热度按时间tzdcorbm1#
您的问题是如何将对象的详细信息显示到TextBox。
我在你的代码中看到你在重写
ToString
方法方面做得很好,在某种意义上,你回答了你自己的问题,因为这就是它所需要的!但是让我们试着把所有的代码都浓缩到一个minimal reproducible example中,这个minimal reproducible example将包含Jimi正确推荐的数据绑定。
接下来,编写主窗体代码以显示从对象到TextBox的详细信息。
一个突出的特点是,
ToString()
只能在object
上调用,因为它是virtual。您的问题还指出**我有一个名为
aircraftList
...**的列表框。当列表框绑定到
BindingList<AircraftDetails>
时,您可以选择AircraftDetails
类的属性显示为DisplayMember
属性。向主窗体的Load
事件添加代码以设置数据源和ListBox
:为响应选择更改,调用此方法在文本框中显示对象: