从私有void方法显示一个List

时间:2017-06-04 22:57:58

标签: c# winforms

我刚开始使用Winforms进行开发,我正在开发一个简单的配方应用程序,允许用户创建配方,提交有关配方的所有详细信息,这些详细信息将传递回主表单上的ListBox,以便用户可以稍后再次访问该配方。我现在的问题是,当我完成设置提交的配方时,我不知道如何将信息传回ListBox进行存储。我知道这必须是一个简单的答案,我感谢你的帮助。

主要表单代码:

public partial class Main : Form
{
    // Creat the recipeList here and make it public
    public static List<Recipe> recipeList = new List<Recipe>();

    public Main()
    {
        InitializeComponent();
    }

    private void newRecipeButton_Click(object sender, EventArgs e)
    {
        // When the new recipe button is clicked, create a new AddNewRecipeForm object that passed the recipeList.
        var newForm = new AddNewRecipeForm(recipeList);
        newForm.Show();
    }

    private void recipeListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        // List the New Recipe in the listbox.           
    }
}

第二个表单代码:

public partial class AddNewRecipeForm : Form
{
    // Create a private recipeList
    private List<Recipe> recipeList = new List<Recipe>();

    // Once AddNewRecipe is create, it uses the recipeList as the parameters.
    public AddNewRecipeForm(List<Recipe> recipeList)
    {
        InitializeComponent();
    }

    private void submitRecipeButton_Click(object sender, EventArgs e)
    {
        // Create a newRecipe object.
        Recipe newRecipe = new Recipe();

        // Add each entry made in the textboxes to newRecipe object.
        newRecipe.Name = nameTextBox.Text;
        newRecipe.Time = Convert.ToInt32(timeTextBox.Text);
        newRecipe.Servings = Convert.ToInt32(servingsTextBox.Text);
        newRecipe.Directions = directionsTextBox.Text;
        newRecipe.Ingredients = ingredientsTextBox.Text;

        // Add the newRecipe object to the recipeList.
        recipeList.Add(newRecipe);

        // Close the window.
        this.Close();
    }                
}

2 个答案:

答案 0 :(得分:0)

使用BindingList代替列表。每当您向其添加项目时,它都会通知您的lisbox,列表框将自动更新。确保导入using System.ComponentModel;

private BindingList<Recipe> recipeList = new BindingList<Recipe>();

DataSource的{​​{1}}设置为recipeList,如下所示:

ListBox

答案 1 :(得分:0)

            Main Form:                
                  public partial class Main : Form
                {
                    // Creat the recipeList here and make it public
                    public List<Recipe> recipeList = new List<Recipe>();

                    public Main()
                    {
                        InitializeComponent();
                    }

                    private void newRecipeButton_Click(object sender, EventArgs e)
                    {
                        // When the new recipe button is clicked, create a new AddNewRecipeForm object that passed the recipeList.
                        var newForm = new AddNewRecipeForm(recipeList);       
                        newForm.Show();
         **recipeList =newForm.ReceiptList;**
    //assign recipeList  values to list box
                    }

                    private void recipeListBox_SelectedIndexChanged(object sender, EventArgs e)
                    {
                        // List the New Recipe in the listbox.           
                    }
                }





Second Form:

public partial class AddNewRecipeForm : Form
{
    // Create a private recipeList
    **private List<Recipe> recipeList = new List<Recipe>();
public List<Recipe> RecipeList
{
 get { return recipeList; }
 set { recipeList = value; }
}**

    // Once AddNewRecipe is create, it uses the recipeList as the parameters.
    public AddNewRecipeForm(List<Recipe> recipeList)
    {
        InitializeComponent();
    }

    private void submitRecipeButton_Click(object sender, EventArgs e)
    {
        // Create a newRecipe object.
        Recipe newRecipe = new Recipe();

        // Add each entry made in the textboxes to newRecipe object.
        newRecipe.Name = nameTextBox.Text;
        newRecipe.Time = Convert.ToInt32(timeTextBox.Text);
        newRecipe.Servings = Convert.ToInt32(servingsTextBox.Text);
        newRecipe.Directions = directionsTextBox.Text;
        newRecipe.Ingredients = ingredientsTextBox.Text;

        // Add the newRecipe object to the recipeList.
        recipeList.Add(newRecipe);

        // Close the window.
        this.Close();
    }                
}