win form input till array i annan klass C#
Hur får man användarens input i IngridientForm till arrayen ingredientArray i klassen Recipe? När användaren skriver in en ingredient ska den sparas till arrayen och arrayen ska visas i listboxen. Här är min kod såhär lång:
IngridientForm:
public partial class FormIngridients : Form
{
Recipe m_recipe;
public FormIngridients(Recipe recipe)
{
InitializeComponent();
m_recipe = recipe;
if (string.IsNullOrEmpty(recipe.Name))
this.Text = "No recipe name";
else
{
this.Text = recipe.Name + "Add ingridients";
}
InitializeGUI();
}
public Recipe Recipe
{
get { return m_recipe; }
set
{
if (value != null)
m_recipe = value;
}
}
public void InitializeGUI()
{
if (m_recipe.Ingredients == null)
m_recipe.Ingredients = new string[m_recipe.MaxNumOfIngredients];
listIngredients.Items.Clear();
number.Text = m_recipe.CurrentNumOfIngredients.ToString();
}
public void UpdateGUI()
{
listIngredients.Items.Clear();
int i;
for (i = 0; i < m_recipe.MaxNumOfIngredients; i++)
if (!string.IsNullOrEmpty(m_recipe.Ingredients[i]))
listIngredients.Items.Add(m_recipe.Ingredients[i]);
number.Text = m_recipe.GetCurrentNumberOfIngredients().ToString();
}
private void buttonAdd_Click(object sender, EventArgs e)
{
string ingredient = CheckInput();
if( !string.IsNullOrEmpty(ingredient))
{
m_recipe.AddIngredient(ingredient);
UpdateGUI();
}
}
private string CheckInput()
{
string input = textIngridients.Text;
if(string.IsNullOrEmpty(input))
{
MessageBox.Show("No ingredients!");
return null;
}
return input;
}
Recipe:
public class Recipe
{
private string[] ingredientArray;
private string name;
private FoodCategory category;
private string description;
public Recipe(int maxNumOfIngredients)
{
ingredientArray = new string[maxNumOfIngredients];
DefaultValues();
}
public string Description
{
get { return description; }
set
{
if (!string.IsNullOrEmpty(value))
description = value;
}
}
public string Name
{
get { return name; }
set
{
if (!string.IsNullOrEmpty(value))
name = value;
}
}
public FoodCategory Category
{
get { return category; }
set
{
category = value;
}
}
public int MaxNumOfIngredients
{
get { return ingredientArray.Length; }
}
public string[] Ingredients
{
get { return ingredientArray; }
set
{
ingredientArray = value;
}
}
public int FindVacantPosition()
{
for (int index = 0; index < ingredientArray.Length; index++)
{
if (ingredientArray[index] == null)
return index;
}
return -1;
}
public bool CheckIndex(int index)
{
return (index >= 0) && (index < ingredientArray.Length);
}
public bool AddIngredient(string value)
{
bool ok = true;
int index = FindVacantPosition();
if (index >= 0)
ingredientArray[index] = value;
else
ok = false;
return ok;
}
public int GetCurrentNumberOfIngredients()
{
int count = 0;
int index;
for (index = 0; index < ingredientArray.Length; index++)
{
if (!string.IsNullOrEmpty(ingredientArray[index]))
count++;
}
return count;
}
public override string ToString()
{
string textOut = string.Format("{0,-20} {1,4} {2,-12}{3,-15}", name, CurrentNumOfIngredients, category.ToString(), descriptionText);
return textOut;
}