I am new to C# and the .NET framework. I am trying to create an array of chocolate orders that shows up as a list on the main screen(MainNavigation). I have a chocolate class and a form(form1) where the user can select which type of chocolate and a new chocolate object is created. I am stuck on trying to pass the new object back to the main form and then showing it in a list on the main form.
MainNavigation form.... I would like value to be the orders that the user creates in form1.
using System;
using System.Windows.Forms;
namespace GatesCandyStore
{
public partial class MainNavigation : Form
{
public MainNavigation()
{
InitializeComponent();
Chocolate[] chocolates = new Chocolate[100];
for (int runs = 0; runs < 100; runs++)
{
chocolates[runs] = value;
}
}
private void btnProcessCandySelection_Click(object sender, EventArgs e)
{
string candy = comboBoxCandySelection.SelectedItem.ToString();
Form1 aForm1 = new Form1(textBoxName.Text, candy);
aForm1.ShowDialog();
}
}
}
Form1 where the user creates a new chocolate order.... Close(newChocolate); does not work.
using System;
using System.Windows.Forms;
namespace GatesCandyStore {
public partial class Form1 : Form
{
Chocolate newChocolate = new Chocolate();
public Form1(string name, string candy)
{
InitializeComponent();
string str = name + " selected : ";
label1.Text = str;
Console.WriteLine(name + " selected : " + candy);
}
private void Form1_Load(object sender, EventArgs e)
{
newChocolate.Flavor = comboBoxChocolateSelection.SelectedItem.ToString();
newChocolate.Cost = 12.5;
newChocolate.GiftWrap = true;
newChocolate.Quantity = 2;
}
private void button1_Click(object sender, EventArgs e)
{
Close(newChocolate);
}
} }
Chocolate Class
namespace GatesCandyStore
{
public class Chocolate
{
#region Fields
public string flavor;
public double cost;
public bool giftWrap;
public int quantity;
#endregion End of Fields
#region Constructors
public Chocolate(string flavor, double cost, bool giftWrap, int quantity)
{
Flavor = flavor;
Cost = cost;
GiftWrap = giftWrap;
Quantity = quantity;
}
#endregion End of Constructors
#region Properties
public string Flavor { get; set; }
public double Cost { get; set; }
public bool GiftWrap { get; set; }
public int Quantity { get; set; }
#endregion End Properties
}
}
here In your question, I'm still not clear what you are doing
candy
andname
in yourForm1
's constructor.Assuming you know what you are doing with them and taking your main concern "How to pass newly created object back to main form", I'm suggesting you some correction.
Create A Model class
Your Chocolate class will same as you have shown here.
Make following changes in
Program.cs
As you can see in above code,
MainNavigation
is accecpting an object ofModel
class now.MainNavigation
form.In code you shown in question, you seem to be doing something in constructor of
MainNavigation
, if array ofChocolate
is containing all added chocolates you can usem_modelObj.ChocolateList
there. But as I'm not clear what isvalue
there. I'm not adding that part in my answer.Note that, now
Form1
will accept three parameters, third as in object ofModel
classForm1
where you are adding chocolate.One more thing I will suggest, naming of class and member should be proper, It will help a lot while debugging and code reviewing.