Link to home
Start Free TrialLog in
Avatar of jpmc_cmsops
jpmc_cmsopsFlag for United States of America

asked on

How do I display array results in a message box using C#?

I have written the code below; How do I display array results lastName and firstName in a message box using C#? Thank you for any help!
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()
        {
            return ((Student)allStudents).ToString;
        }
 
        
    }
 
 
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)
        {
            this.lblDisplayAddedStudents.Text = "((Student)allStudents).FirstName";       
            /*
            foreach (Student aStudent in allStudents)
            { outputStr to ((Student)allStudents[i]).FirstName + "" +
                               ((Student)allStudents[i].LastName + "\n";
            }
            */
            
            this.lblDisplayStudents.Text = "Bruce Springstein" + "\n" + "Neil Armstrong";
            //  this.lblDisplayAddedStudents.Text += ((Student)allStudents).FirstName + " " +
            //                                 ((Student)allStudents).LastName + "\n";
            
            
            
            /*
            outputStr to ((Student)allStudents[i]).FirstName + "" +
                               ((Student)allStudents[i].LastName + "\n";
            */
             
            
            
            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");
            
             
            
            /*
             * MessageBox.Show((Student)allStudents[i]).FirstName + "" +
                               ((Student)allStudents[i].LastName + "\n";
             */
 
             
                /*
                * this.lblDisplayAddedStudents.Text += ((Student)allStudents.Equals).FirstName +
                *                              ((Student)allStudents.Equals).LastName + "\n";
                */
            
         }  
            
        
        
        
    }

Open in new window

Avatar of carlsiy
carlsiy
Flag of Philippines image

loop for each values in the array and save it into a string

e.g.
            string[] strArray = new string[5] { "Ronnie", "Jack", "Lori", "Max", "Tricky" };
            string newstring = string.Empty;
            foreach (string x in strArray)
            {
                newstring += x.ToString() + "\r\n";
            }
            MessageBox.Show(newstring.ToString());
SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jpmc_cmsops

ASKER

I am trying to add First and Last names in a window form for user input for first and last name. I think I am still missing a piece of the puzzle to accept user entries of first and last names and then trying to display them in a window when I click on "Display Students" . I appreciate any help.

I also have this code snippet:


class Student 
    {
        // Declaration of Variables        
        private String firstName;
        private String lastName;
 
        // Constructor
        public Student()
        {
        }
 
        // Getters and Setters
        public string FirstName
        {
            
            set {firstName = value;}
            get { return firstName;}
           
        }
    
        public string LastName
        {
            
            set {lastName = value;}
            get { return lastName; }
        }
        
    }

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It still will not let me enter new first and last names like for example Benny Goodman and then print it out in a window messagebox. How can I fix this to make it work? Thank you for any help.
what is exactly your code?
did you try the example above?
Excellent Help! Thank you! After a little more work I was able to make it work.