win form input till array i annan klass C#

Permalänk
Medlem

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; }

Permalänk
Medlem

Du har ju redan koden för att göra det du beskriver?

Lägga till ny ingrediens

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; }

Lista alla ingredienser

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(); }

Har du bara copy pastat någon annans projekt utan att titta på koden?

Visa signatur

AW3423DW QD-OLED - Ryzen 5800x - MSI Gaming Trio X 3090 - 64GB 3600@cl16 - Samsung 980 Pro 2TB/WD Black SN850 2TB

Permalänk
Medlem

Nä det har jag inte, problemet är att jag inte får upp någon text i listboxen, inget blir inlagt i arrayen

Permalänk
Medlem
Skrivet av nelle_87:

Nä det har jag inte, problemet är att jag inte får upp någon text i listboxen, inget blir inlagt i arrayen

Sätt en breakpoint där du tror att det blir fel så kan du se vad som händer.

Visa signatur

AW3423DW QD-OLED - Ryzen 5800x - MSI Gaming Trio X 3090 - 64GB 3600@cl16 - Samsung 980 Pro 2TB/WD Black SN850 2TB

Permalänk
Medlem
Skrivet av nelle_87:

Nä det har jag inte, problemet är att jag inte får upp någon text i listboxen, inget blir inlagt i arrayen

Testa lägga in något enklare först.
Kontrollera först att Modifier på Listboxen är satt till public så du lättare kan felsöka.
Jag kan inte undvika att kika på den här:

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();
}

I den ovan skriver du själva objektet till listboxen, borde du inte kolla om returneringen från din tostring ger ett faktiskt värde?

public void UpdateGUI()
{
listIngredients.Items.Clear();
for (int i = 0; i < m_recipe.MaxNumOfIngredients; i++)
if (!string.IsNullOrEmpty(m_recipe.Ingredients[i].ToString()))
listIngredients.Items.Add(m_recipe.Ingredients[i].ToString());
number.Text = m_recipe.GetCurrentNumberOfIngredients().ToString();
}

Jag ska testa din kod och se vad jag får resultat senare ikväll.

Visa signatur

Citera om du vill ha svar, hjälpte jag dig, gilla svaret!
Felkod40