将列表集合显示到文本框

时间:2015-05-06 15:43:51

标签: c# wpf visual-studio textbox

我有这个班的学生

#region Person

    public class Person
    {
        public string firstName { get; set; }
        public string lastName { get; set; }
        public string Address { get; set; }
        public virtual void GetInfo()
        {
            Console.WriteLine("First Name: {0}", firstName);
            Console.WriteLine("First Name: {0}", lastName);
            Console.WriteLine("Address: {0}", Address);
        }
    }
    #endregion
    class

    #region Student
    public class Student : Person
    {

        public override void GetInfo()
        {
            // Calling the base class GetInfo method: 
            base.GetInfo();
        }
        public void TakeTest()
        {
            throw new NotImplementedException();
        }



        private List<Student> AllStudents = new List<Student>();
        public Student()
        {
            AllStudents.Add(this);

        }
        public List<Student> AllStudentsList
        {
            get
            {
                return AllStudents;
            }
            set
            {
                AllStudents = value;
            }
        }


    }
    #endregion

使用WPF,我将启动几个学生输入文本框,第一个名称有文本框,姓氏也有文本框,并创建一个列表集合来存储这些学生对象。我创建了一个事件处理程序,将学生添加到列表集合newStudent。

public List<Student> newStudent = new List<Student>();
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnCreateStudent_Click(object sender, RoutedEventArgs e)
        {
            Student student = new Student();
            student.firstName = txtFirstName.Text;
            student.lastName = txtLastName.Text;
            newStudent.Add(student);

            txtFirstName.Text = "";
            txtLastName.Text = "";

        }

我的问题是,当我尝试将其显示回文本框(另一个事件处理程序)时,列表中存储了什么,结果不是我期望的名字和姓氏。我应该如何实现它,以便为我提供已发起学生的名字和姓氏。

作为背景,由于我的声誉没有通过配额发布图片,每次我在文本框中输入时,我都会单击“创建学生”按钮,将学生姓名添加到列表集合中。在创建了几个学生之后,我希望他们在按下另一个按钮时将列表中的最后一个元素显示到文本框中(假设我们称之为btnPrevious。我希望有人可以帮助我。

这是我的另一个事件处理程序,它将显示列表中的所有学生。

private void btnNext_Click(object sender, RoutedEventArgs e)
        {

            //Student student = new Student();
            //foreach (Student i in student.AllStudentsList)
            //{
            //    MessageBox.Show(i.Name);
            //}
            //MessageBox.Show(student.AllStudentsList[0].ToString());
        }

2 个答案:

答案 0 :(得分:0)

如果您想将名称保留在文本框中,则应从_Click处理程序中删除这些行:

txtFirstName.Text = "";
txtLastName.Text = "";

至于显示学生列表,如果您使用单独的控件,则无法清楚您的问题。我不确定您是否要显示每个名称部分只有一个文本框控件的用户列表。请澄清。

如果你想要的只是在你已经检索到一个学生后(在你的&#34;其他处理程序&#34;中)显示文本框中的名字和姓氏,你只需要:

txtFirstName.Text = student.firstName;
txtLastName.Text = student.lastName;

[更新]以下是一些辅助功能,可以显示学生和所有学生。您可以修改这些以使用您选择用于显示用户信息的任何控件。把它们放在你的学生班上。

public void PrintStudent()
{
    int i = AllStudents.Count - 1; //last student
    Student thisStudent = this.AllStudents[i];
    string fName = thisStudent.firstName;
    string lName = thisStudent.lastName;
    Console.WriteLine(string.Format("{0}. {1} {2}", i+1, fName, lName));
}

public void DisplayAll(List<Student> someStudents)
{
    StringBuilder formattedStudents = new StringBuilder();
    int lastIndex = someStudents.Count - 1;
    for (int i = 0; i < someStudents.Count; i++)
    {                
        if (i == 0)
        {
            formattedStudents.AppendLine("Complete List of Students:");                    
        }
        string fName = someStudents[i].firstName;
        string lName = someStudents[i].lastName;
        formattedStudents.AppendLine(string.Format("{0}. {1} {2}", i + 1, fName, lName));

        if (i == lastIndex)
        {
            formattedStudents.AppendLine();
            formattedStudents.AppendLine("Have a nice day.");
        }

    }
    Console.Write(formattedStudents);
}

现在,在你的处理程序中(或者在我的情况下,我的小测试控制台应用程序的主要部分),你可以像这样调用它们:

List<Student> listStudents = new List<Student>();

Student first = new Student();
first.firstName = "Mike";
first.lastName = "Smith";

Student second = new Student();
second.firstName = "Joe";
second.lastName = "Murphy";

Student last = new Student();
last.firstName = "Matthew";
last.lastName = "Mahern";

listStudents.Add(first);
listStudents.Add(second);
listStudents.Add(last);

Student lastStudent = listStudents[listStudents.Count - 1]; //return the LAST Student in the list of All Students
lastStudent.PrintStudent();

//now, display all students in the list
lastStudent.DisplayAll(listStudents);

答案 1 :(得分:0)

在选择学生之前,您需要预览它们,为此您需要使用ListBox或ListView。

    // Use ObservableCollection for store and preview data in view controls (see more MVVM)
    public ObservableCollection<Student> newStudents = new ObservableCollection<Student>(); 

    public MainWindow()
    {
        InitializeComponent();
        MyListBox.ItemsSource = newStudents; // MyListBox you may create in code or using XAML
    }

使用SelectionChanged事件从List控件中选择学生。

private void SelectionChangedHandler(object sender, SelectionChangedEventArgs e){
         Student st = MyListBox.SelectedItem as Student;

         if (st == null) return;

         txtFirstName.Text = st.firstName;
         txtLastName.Text = st.lastName;
}

<强> UPD#1

你的主要问题是Student模型类中的代码逻辑错误。

1)下面的代码演示了静态类的逻辑,但它不是静态类。

private List<Student> AllStudents = new List<Student>();
    public Student()
    {
        AllStudents.Add(this);

    }
    public List<Student> AllStudentsList
    {
        get
        {
            return AllStudents;
        }
        set
        {
            AllStudents = value;
        }
    }

2)在MainWindow代码中,您使用外部列表newStudent,但引用内部类变量AllStudentsList。

private void btnNext_Click(object sender, RoutedEventArgs e)
    {

        //Student student = new Student();
        //foreach (Student i in student.AllStudentsList)
        //{
        //    MessageBox.Show(i.Name);
        //}
        //MessageBox.Show(student.AllStudentsList[0].ToString());
    }

收集student.AllStudentsList为空,因为从未使用过。尝试使用此代码。

    private void btnNext_Click(object sender, RoutedEventArgs e)
    {
        if(newStudent == null) MessageBox.Show("Students collection is null.");
        foreach (Student i in newStudent)
        {
            MessageBox.Show(string.Format("First name: {0} Last name: {1}", i.firstName, i.lastName));
        }
        //MessageBox.Show(student.AllStudentsList[0].ToString());
    }