using c# - WinForms, .net Framework 4.5, VS 2012
Try to create small app with some entity. I create separate class for my entity and put some simple code inside:
public class Car
{
public string Color {get; set;}
public string Make { get; set; }
public string CarModel { get; set; }
}
Then from main form i create some specimen of class Car
(creating can be geted by clicking button from main form, after clicking new form with 3 text boxes will be opened, if information entered and button Ok clicked - new Car
sample must be created and returned to main form).
For this i try to use next code:
public Car myCar = new Car();
private void buttonAdd_Click(object sender, EventArgs e)
{
myCar.Color = textBoxColor.Text;
myCar.Make = textBoxMake.Text;
myCar.CarModel = textBoxModel.Text;
this.DialogResult = DialogResult.OK;
this.Close();
MessageBox.Show("Added");
this.Close();
}
For moving data from new form to main form I use public field public Car myCar = new Car();
, but this is not the best way to do this, due to using of public
field.
Another way I found - in main form create next method
static List<Car> carInStock = null;
public static void myCar(string color, string make, string model)
{
Car myCar = new Car
{
Color = color,
CarModel = model,
Make = make
};
MainForm.carInStock.Add(myNewCar);
}
and for button can use method like:
private void buttonAdd_Click(object sender, EventArgs e)
{
MainForm.myCar(textBoxColor.Text,
textBoxMake.Text,
textBoxModel.Text);
MessageBox.Show("Added");
this.Close();
}
But think varian also not hte best and prefered.
Question: What is the best way to move created entity (in this case entity of Car
, represented as myCar
) from one form to another?