The first thing you're missing is the concept of data binding.
Where you're doing
PHP:
// Populates the combo box with hardware types
List<HardwareInfo> allHardwareInfo = new List<HardwareInfo>();
string myLine;
using (StreamReader fileReader = new StreamReader("hardware.txt"))
{
while ((myLine = fileReader.ReadLine()) != null)
{
string [] items = myLine.Split(new char[] { ',' });
var hardwareInfo = new HardwareInfo();
hardwareInfo.Name = items[0];
hardwareInfo.PowerConsumption = double.Parse(items[1]);
hardwareInfo.TimeOn = double.Parse(items[2]);
hardwareInfo.Cost = double.Parse(items[3]);
hardwareInfo.Co2 = double.Parse(items[4]);
allHardwareInfo.Add(hardwareInfo);
// This shouldn't be here
cboHardware.Items.Add(items[0]);
}
fileReader.Close();
}
what you should be doing is actually not just populating the combo box with hardware types but loading the file data into a reusable collection.
The normal pattern for doing this in simple apps is something like this.
First create a method to fetch your data. This is usually from a database but it your case it's a file.
PHP:
public List<HardwareInfo> GetHarwareData()
{
// Populates the combo box with hardware types
List<HardwareInfo> allHardwareInfo = new List<HardwareInfo>();
string myLine;
using (StreamReader fileReader = new StreamReader("hardware.txt"))
{
while ((myLine = fileReader.ReadLine()) != null)
{
string[] items = myLine.Split(new char[] { ',' });
var hardwareInfo = new HardwareInfo();
hardwareInfo.Name = items[0];
hardwareInfo.PowerConsumption = double.Parse(items[1]);
hardwareInfo.TimeOn = double.Parse(items[2]);
hardwareInfo.Cost = double.Parse(items[3]);
hardwareInfo.Co2 = double.Parse(items[4]);
allHardwareInfo.Add(hardwareInfo);
}
fileReader.Close();
}
return allHardwareInfo;
}
Then in the form constructor you can load the data into a class level field.
e.g.
PHP:
private List<HardwareInfo> allHardwareInfo;
public Form1()
{
InitializeComponent();
this.allHardwareInfo = this.GetHarwareData();
}
You can then use the class level field to "bind" to form controls.
Which in the case of your combo box it would be something like
PHP:
private void BindHardwareCombo()
{
BindingSource hardwareInfoSource = new BindingSource();
hardwareInfoSource.DataSource = this.allHardwareInfo;
this.cboHardware.DataSource = hardwareInfoSource.DataSource;
this.cboHardware.DisplayMember = "Name";
this.cboHardware.ValueMember = "Name";
}
You can call this method in the form load method.
This really just demonstrates the broader principle of data binding. You can now bind the allHardwareInfo Collection to a whole host of control types using an almost identical method i.e.
- Create a binding source
- Set the binding source DataSource property to your data.
- Set the DataSource of the the control to match that of the binding source
I would read up binding to the DataGridView and/or using the BindingNavigator controls. Both of these will allow you to display the data in text boxes of sorts and the behavior can be modified to suite your needs.
The binding source method offers a number of advantages when navigating and displaying data. But one of the other key advantages is when you edit data in the controls it's still "connected" to the underlying data source i.e. the values are automatically updated in the "allHardwareInfo" collection.
So when your finished editing you data you can just call a SaveHardwareData() method and implement the code to write the data back to your file.