如何从通用阵列列表输出到列表框?

时间:2017-09-27 21:24:43

标签: c# arrays generics listbox

我正在尝试从Array列表输出到Listbox。我的问题是我认为我不知道如何将Class连接到通用数组列表中?最终结果应如下所示:

enter image description here

然后信息应该如下排序:所有信息都进入第一个列表框,然后上面的18个发送给成人,而下面的18个发送给孩子们。我的班级看起来像这样:

namespace Patients
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public String Password { get; set; }

        public Person() //Constructor
        {
            Age = 0;
            Password = "";
        }

        public Person (string name, int age, string password) //Parameters    
        {
            this.Name = name;
            this.Age = age;
            this.Password = password;    
        }

        public override string ToString()  // 
        {    
            return Name + Age.ToString() + Password;  //outputs as a string
            // return Name + " (" + Age + " years) " + Password ;
        }               
    }
}    

namespace Patients
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        public static void Page_Load(object sender, EventArgs e)
        {

        }

        public void ButtonAdd_Click(object sender, EventArgs e)
        {
            Person p = new Person();

            List<string> People = new List<string>();
            People.Add(TextBoxName.Text);
            People.Add(TextBoxAge.Text);
            People.Add(TextBoxPassword.Text);

            foreach (object Person in People)     
            {
                ListBoxAll.Items.Add(p.Name + p.Age.ToString() + p.Password);
            }

            if (p.Age > 18)
            {
                ListBoxAdults.Items.Add(p.Name + p.Age.ToString() + p.Password);    
            }                 
            else
            {    
                ListBoxKids.Items.Add(p.Name + p.Age.ToString() + p.Password);    
            }                        
        }    
    }
}

3 个答案:

答案 0 :(得分:0)

看起来你正在混合和匹配一点。 尝试这样的事情。

Person p = new Person();
p.Name = TextBoxName.Text;
p.Age= TextBoxAge.Text;
p.Password= TextBoxPassword.Text;
ListBoxAll.Items.Add(p);

答案 1 :(得分:0)

我认为您的问题是,您没有设置属性。事实上,您根本不需要列表,但您可以使用列表来保持您的病人。但它仍然没有必要:

namespace Patients
{
public partial class WebForm1 : System.Web.UI.Page
{
    // Define Property and initialize List
    public List<Person> patients{ get; } = new List<Person>();
    public static void Page_Load(object sender, EventArgs e)
    {

    }
     public void ButtonAdd_Click(object sender, EventArgs e)
    {
        // Use the Constructor with Parameters 
        Person p = new Person(TextBoxName.Text, TextBoxAge.Text, TextBoxPassword.Text);
        // Add your patient to your List
        patients.Add(p);
        // Use the ToString() of your Person
        ListBoxAll.Items.Add(p.ToString());


        if (p.Age > 18)
        {
            ListBoxAdults.Items.Add(p.ToString());    
        }                 
        else
        {    
            ListBoxKids.Items.Add(p.ToString());    
        }                        
    }    
}

}

答案 2 :(得分:0)

一些对我们很好的技巧,首先你可以为这样的属性声明默认值:

    public string Name { get; set; } = "Steve";
    public int Age { get; set; } = 1;
    public String Password { get; set; } = "password";

然而,还应该注意到&#34;&#34;是字符串的默认值,0是非可空int的默认值,因此您甚至不必担心这些默认值。

在这种情况下,在构造函数中声明Age = 0;基本上是浪费时间。 (如果它是可以为空的int,则默认值为null

接下来,由于您可以使用默认值,因此您不需要像构建器一样在属性中声明属性。

您可以完全删除构造函数,只需执行以下操作:

 var myPerson = new Person { Name = "Steve", Age = 18, Password = "Foo" };

接下来,只要退出按钮点击范围,就会失去所有现有人员。

相反,您想要声明两个不在click方法范围内的人员列表(他们坚持这种方式),例如&#34; Adults&#34;和&#34;儿童&#34;

然后或许制作一个名为&#34; PopulateLists&#34;这会做到以下几点:

  1. 清除所有列表框
  2. 在每个框中添加适用的每个组名称的列表(您可以使用列表中的Linq和Select语句创建IQueryable)
  3. 当您点击该按钮时,您应该创建一个新人,将其分配到右侧列表,然后调用PopulateLists()

    以下是您入门所需的信息:

    Linq选择获取属性列表(在这种情况下,我将把人员列表变成年龄列表,你可以用名字做同样的事情)

     var ages = People.Select(p => p.Age);
    

    ListBox的.Items属性与列表的工作方式相同,它只是直观地显示自己。它是一个专门的字符串列表。

    因此,举例来说,你可以做......

     MyListBox.Items.Clear();
     MyListBox.Items.Add(...);
     MyListBox.Items.AddRange(...);
    

    等等。

    那应该让你开始吧!