Solved

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

Posted on 2008-10-07
8
4,477 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:jpmc_cmsops
8 Comments
 
LVL 6

Expert Comment

by:carlsiy
ID: 22665257
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());
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 150 total points
ID: 22665300
using a StringBuilder will be more efficient in both memory usage and performance:

public string studentList()
{
       StringBuilder sb = new StringBuilder();
       foreach (Student student in allStudents)
       {
              sb.Append(......);
       }
       return sb.ToString();
}

you will need:

using System.Text;

you can use as:
MessageBox.Show(yourList.studentList());
0
 
LVL 4

Accepted Solution

by:
mur_alidhar earned 200 total points
ID: 22665306
foreach (Student aStudent in allStudents)
            {
             string strFstname =(Student)aStudent.FirstName;
             string strLastname=(Student)aStudent.LastName;
             MessageBox.Show(strFstName + "-" + strLastname);
            }
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:jpmc_cmsops
ID: 22665713
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

0
 
LVL 6

Assisted Solution

by:carlsiy
carlsiy earned 150 total points
ID: 22666637
change your class to this

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



in your form event. use this:

            Student.FirstName = "Firstname";
            Student.LastName = "LastName";
            MessageBox.Show("The Lastname is: " + Student.LastName.ToString() + "\r\n" +
                             "The Firstname is: " + Student.FirstName.ToString());
0
 

Author Comment

by:jpmc_cmsops
ID: 22667566
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.
0
 
LVL 6

Expert Comment

by:carlsiy
ID: 22674490
what is exactly your code?
did you try the example above?
0
 

Author Closing Comment

by:jpmc_cmsops
ID: 31504077
Excellent Help! Thank you! After a little more work I was able to make it work.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net How to Exit Sub - Exit Form??? 5 57
C#.NET and microsoft certification. 3 40
SQL Help 27 45
Get number of Files in Directory and Sub Directories 2 40
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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