Solved

How do I display my array results properly in my list box?

Posted on 2008-10-10
9
252 Views
Last Modified: 2013-12-17
What coding should I use to display my array results properly in my list box in Visual Studio 2005 C# Windows Application? I have attached all of my code. Thank you for any help.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
 
namespace Chp08StudentWinApp.Problem_Domain
{
    class Student
    {
        // Declaration of Variables        
        private String firstName;
        private String lastName;
 
        // Constructor
        public Student()
        {
        }
        public override string ToString()
        {
            return this.FirstName + " " + this.LastName;
        }
        // Getters and Setters
        public string FirstName
        {
 
            set { firstName = value; }
            get
            {
 
                return firstName.ToUpper();
            }
 
        }
 
        public string LastName
        {
 
            set { lastName = value; }
            get { return lastName.ToUpper(); }
        }
 
    }
}
 
 
// Students.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
 
namespace Chp08StudentWinApp.Problem_Domain
{
    class Students
    {
        // constructor
        public Students()
        {
        }
 
        // Array declaration
        private ArrayList allStudents = new ArrayList();
        public void addStudent(Student aStudent)
        {
            allStudents.Add(aStudent);
        }
 
        // ArrayList Students.allStudents
        public int studentCount()
        {
            return allStudents.Count;
        }
 
 
        public string studentList()
        {
            string outputString = "";
            foreach (Student aStudent in allStudents)
            {
                outputString += aStudent.ToString() + "\n";
            }
            return outputString;
        }
        
 
    }
}
 
 
// Form1.cs
 
using System;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Chp08StudentWinApp.Problem_Domain;
 
namespace Chp08StudentWinApp
{
    public partial class frmStudentCounter : Form
    {
        private Students allStudents = new Students();
        public frmStudentCounter()
        {
            InitializeComponent();
        }
 
        private void lblFirstName_Click(object sender, EventArgs e)
        {
 
        }
 
        private void txtBoxFirstName_TextChanged(object sender, EventArgs e)
        {
 
        }
 
        private void lblLastName_Click(object sender, EventArgs e)
        {
 
        }
 
        private void txtBoxLastName_TextChanged(object sender, EventArgs e)
        {
 
        }
 
        private void btnStudentAdd_Click(object sender, EventArgs e)
        {
            Student thisStudent = new Student();
            thisStudent.FirstName = this.txtBoxFirstName.Text;
            thisStudent.LastName = this.txtBoxLastName.Text;
            allStudents.addStudent(thisStudent);
        }
 
        private void btnStudentDis_Click(object sender, EventArgs e)
        {
            this.lblStudentCount.Text = "You have " + allStudents.studentCount() +
                " Student(s) in Class";
 
        }
 
        private void frmStudentCounter_Load(object sender, EventArgs e)
        {
            MessageBox.Show("Welcome to Student Win App By Rolan Logan!");
        }
 
        /*
        private void btnDisplayStudents_Click(object sender, EventArgs e)
        {
            {
                string[] strArray = new string[5] { "Ronnie Millsap", "Jack London", "Lori Newman", "Max Headroom", "Abraham Lincoln" };
                string newstring = string.Empty;
                foreach (string x in strArray)
                {
                    newstring += x.ToString() + "\r\n";
                }
                MessageBox.Show(newstring.ToString());
            }
 
 
            this.lblDisplayAddedStudents.Text = "Bruce Springstein" + "\n" + "Neil Armstrong";
 
 
 
            MessageBox.Show("More MSU Students on the way");
 
 
            string[] studentMsgBoxList = { "Bruce Springstein", "Neil Armstrong" };
            foreach (string val in studentMsgBoxList)
                MessageBox.Show(val);
 
            MessageBox.Show("Bruce Springstein" + "\n" + "Neil Armstrong");
        }
        */
        
 
        private void frmStudentCounter_Load_1(object sender, EventArgs e)
        {
 
        }
 
        //private void label1_Click(object sender, EventArgs e)
        private void lblDisplayAddedStudents_Click(object sender, EventArgs e)
        {
 
        }
 
        private void btnDispStudInLbl_Click(object sender, EventArgs e)
        {
            this.lblDisplayAddedStudents.Text =
 
                "You have " + allStudents.studentCount() +
                " Student(s) in Class: \n\n";
 
            this.lblDisplayAddedStudents.Text += allStudents.studentList();
 
 
        }
 
        private void lblStudentCount_Click(object sender, EventArgs e)
        {
 
        }
 
        private void btnDispStudInListBox_Click(object sender, EventArgs e)
        {
            listBoxStudentDisplay.Items.Add(allStudents.studentList());
        }
 
        private void btnClearStudListBox_Click(object sender, EventArgs e)
        {
            listBoxStudentDisplay.Items.Clear();
        }
 
             
        
 
 
 
 
    }
}
 
 
// Program.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
 
namespace Chp08StudentWinApp
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmStudentCounter());
        }
    }
}

Open in new window

0
Comment
Question by:jpmc_cmsops
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22686341
i make use of datasource property.
 Student st = new Student();
            st.FirstName = "Sameer";
            st.LastName = "Jagdale";
 
            Student st1 = new Student();
            st1.FirstName = "Sameer1";
            st1.LastName = "Jagdale1";
 
            Student[] sts = new Student[] { st,st1 };
 
            this.listBox1.DataSource = sts;

Open in new window

0
 
LVL 8

Expert Comment

by:tiagosalgado
ID: 22686471
Use AddRange method to add all arraylist entries.

listBox1.Items.AddRange(allStudents.ToArray());

Open in new window

0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22686895
the following link provides a sample how to bind an arraylist to a listbox
http://www.java2s.com/Tutorial/CSharp/0460__GUI-Windows-Forms/BindArrayListtoListBox.htm
0
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 

Author Comment

by:jpmc_cmsops
ID: 22692710
I still cannot get it to work with the solutions given. Appreciate any help!
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22692934
I got your problem
I ran your code and the fort student addition is ok but when the second student is added then your face the issue as the second record is getting added to the same row

I will suggest you to use DataGridView instead of ListView as it will give you more freedom of databinding and will serve your purpose more
I will be happy to help if you face any difficulities
0
 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22693527
i did a couple of changes to your code, check if it works!
 

 private void btnDispStudInListBox_Click(object sender, EventArgs e)
        {
            //listBoxStudentDisplay.Items.Add(allStudents.studentList());
            listBoxStudentDisplay.DataSource = allStudents.studentList();            
        }
 
-----
 public ArrayList studentList()
        {
            //string outputString = "";
            //foreach (Student aStudent in allStudents)
            //{
            //    outputString += aStudent.ToString();
            //}
            //return outputString;
            return allStudents;
        }

Open in new window

0
 

Author Comment

by:jpmc_cmsops
ID: 22693750
raqi0017, thank you I am trying to use the DataGridView. Can you be more specific on the coding? I am trying to get the user entry data from my array allStudents into the DataGridView but I am having a little difficulty; thank you for any help.
 
0
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 500 total points
ID: 22694536
i am attaching sample files for datagridview binding as i coudnt find any useful samples on the net
coudnt upload .cs files so changed the extension to .txt
files.zip
0
 

Author Closing Comment

by:jpmc_cmsops
ID: 31504982
Thank you for the great help!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question