I have a problem in C#.Net 2008. Just about everything in this program is complete except for some redundant code that I will clean up later. Anyway, my problem is I need to display the contents of a student object that is derived from clsStudent in a listbox object, but all it does is display "clsStudent" in the listbox when a student is added. How do I get it to display the contents of the object like student.firstname, student.lastname, etc instead. The code is as follows.
Main form:
Code:
using System;
using System.IO;
using System.Windows.Forms;
public class frmMain : Form
{
private bool save = false;
private string mStudentsFileName;
private ListBox lstStudent;
private Label label1;
private Label label2;
private TextBox txtFirstName;
private Label label3;
private TextBox txtStudentID;
private Label label4;
private TextBox txtSection;
private Label label5;
private TextBox txtClassification;
private Label label6;
private TextBox txtGrade;
private Label label7;
private Button btnAdd;
private Button btnEdit;
private Button btnDelete;
private MenuStrip menuStrip1;
private ToolStripMenuItem fileToolStripMenuItem;
private ToolStripMenuItem mnuOpen;
private ToolStripMenuItem mnuSave;
private ToolStripMenuItem mnuExit;
private TextBox txtLastName;
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.lstStudent = new System.Windows.Forms.ListBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtLastName = new System.Windows.Forms.TextBox();
this.txtFirstName = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.txtStudentID = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.txtSection = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.txtClassification = new System.Windows.Forms.TextBox();
this.label6 = new System.Windows.Forms.Label();
this.txtGrade = new System.Windows.Forms.TextBox();
this.label7 = new System.Windows.Forms.Label();
this.btnAdd = new System.Windows.Forms.Button();
this.btnEdit = new System.Windows.Forms.Button();
this.btnDelete = new System.Windows.Forms.Button();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.mnuOpen = new System.Windows.Forms.ToolStripMenuItem();
this.mnuSave = new System.Windows.Forms.ToolStripMenuItem();
this.mnuExit = new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// lstStudent
//
this.lstStudent.FormattingEnabled = true;
this.lstStudent.Location = new System.Drawing.Point(15, 54);
this.lstStudent.Name = "lstStudent";
this.lstStudent.Size = new System.Drawing.Size(440, 290);
this.lstStudent.TabIndex = 0;
this.lstStudent.TabStop = false;
this.lstStudent.SelectedIndexChanged += new System.EventHandler(this.lstStudent_SelectedIndexChanged);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(63, 13);
this.label1.TabIndex = 1;
this.label1.Text = "Student List";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(461, 54);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(61, 13);
this.label2.TabIndex = 2;
this.label2.Text = "Last Name:";
//
// txtLastName
//
this.txtLastName.Location = new System.Drawing.Point(464, 70);
this.txtLastName.Name = "txtLastName";
this.txtLastName.Size = new System.Drawing.Size(156, 20);
this.txtLastName.TabIndex = 1;
//
// txtFirstName
//
this.txtFirstName.Location = new System.Drawing.Point(464, 109);
this.txtFirstName.Name = "txtFirstName";
this.txtFirstName.Size = new System.Drawing.Size(156, 20);
this.txtFirstName.TabIndex = 2;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(461, 93);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(60, 13);
this.label3.TabIndex = 4;
this.label3.Text = "First Name:";
//
// txtStudentID
//
this.txtStudentID.Location = new System.Drawing.Point(464, 148);
this.txtStudentID.Name = "txtStudentID";
this.txtStudentID.Size = new System.Drawing.Size(156, 20);
this.txtStudentID.TabIndex = 3;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(461, 132);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(61, 13);
this.label4.TabIndex = 6;
this.label4.Text = "Student ID:";
//
// txtSection
//
this.txtSection.Location = new System.Drawing.Point(464, 187);
this.txtSection.Name = "txtSection";
this.txtSection.Size = new System.Drawing.Size(156, 20);
this.txtSection.TabIndex = 4;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(461, 171);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(46, 13);
this.label5.TabIndex = 8;
this.label5.Text = "Section:";
//
// txtClassification
//
this.txtClassification.Location = new System.Drawing.Point(464, 226);
this.txtClassification.Name = "txtClassification";
this.txtClassification.Size = new System.Drawing.Size(156, 20);
this.txtClassification.TabIndex = 5;
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(461, 210);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(71, 13);
this.label6.TabIndex = 10;
this.label6.Text = "Classification:";
//
// txtGrade
//
this.txtGrade.Location = new System.Drawing.Point(464, 265);
this.txtGrade.Name = "txtGrade";
this.txtGrade.Size = new System.Drawing.Size(156, 20);
this.txtGrade.TabIndex = 6;
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(461, 249);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(39, 13);
this.label7.TabIndex = 12;
this.label7.Text = "Grade:";
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(464, 291);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(75, 23);
this.btnAdd.TabIndex = 7;
this.btnAdd.Text = "Add";
this.btnAdd.UseVisualStyleBackColor = true;
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnEdit
//
this.btnEdit.Location = new System.Drawing.Point(464, 320);
this.btnEdit.Name = "btnEdit";
this.btnEdit.Size = new System.Drawing.Size(75, 23);
this.btnEdit.TabIndex = 9;
this.btnEdit.Text = "Edit";
this.btnEdit.UseVisualStyleBackColor = true;
this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click);
//
// btnDelete
//
this.btnDelete.Location = new System.Drawing.Point(545, 291);
this.btnDelete.Name = "btnDelete";
this.btnDelete.Size = new System.Drawing.Size(75, 23);
this.btnDelete.TabIndex = 8;
this.btnDelete.Text = "Delete";
this.btnDelete.UseVisualStyleBackColor = true;
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(627, 24);
this.menuStrip1.TabIndex = 13;
this.menuStrip1.Text = "menuStrip1";
//
// fileToolStripMenuItem
//
this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.mnuOpen,
this.mnuSave,
this.mnuExit});
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(35, 20);
this.fileToolStripMenuItem.Text = "File";
//
// mnuOpen
//
this.mnuOpen.Name = "mnuOpen";
this.mnuOpen.Size = new System.Drawing.Size(130, 22);
this.mnuOpen.Text = "Open File";
this.mnuOpen.Click += new System.EventHandler(this.mnuOpen_Click);
//
// mnuSave
//
this.mnuSave.Name = "mnuSave";
this.mnuSave.Size = new System.Drawing.Size(130, 22);
this.mnuSave.Text = "Save File";
this.mnuSave.Click += new System.EventHandler(this.mnuSave_Click);
//
// mnuExit
//
this.mnuExit.Name = "mnuExit";
this.mnuExit.Size = new System.Drawing.Size(130, 22);
this.mnuExit.Text = "Exit";
this.mnuExit.Click += new System.EventHandler(this.mnuExit_Click);
//
// frmMain
//
this.ClientSize = new System.Drawing.Size(627, 369);
this.Controls.Add(this.btnDelete);
this.Controls.Add(this.btnEdit);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.txtGrade);
this.Controls.Add(this.label7);
this.Controls.Add(this.txtClassification);
this.Controls.Add(this.label6);
this.Controls.Add(this.txtSection);
this.Controls.Add(this.label5);
this.Controls.Add(this.txtStudentID);
this.Controls.Add(this.label4);
this.Controls.Add(this.txtFirstName);
this.Controls.Add(this.label3);
this.Controls.Add(this.txtLastName);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.lstStudent);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "frmMain";
this.Text = "Student Roster";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.frmMain_FormClosing);
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
[STAThread]
public static void Main()
{
frmMain main = new frmMain();
Application.Run(main);
}
public frmMain()
{
InitializeComponent();
}
private void mnuExit_Click(object sender, EventArgs e)
{
Close();
}
private void btnAdd_Click(object sender, EventArgs e)
{
clsStudent student = new clsStudent();
if (txtLastName.Text == "" || txtFirstName.Text == "" ||
txtStudentID.Text == "" || txtSection.Text == "" ||
txtClassification.Text == "" || txtGrade.Text == "")
{
messageBoxOK("Please fill in all the fields.");
return;
}
student.LastName = txtLastName.Text;
student.FirstName = txtFirstName.Text;
student.ID = txtStudentID.Text;
student.Section = txtSection.Text;
student.Classification = txtClassification.Text;
student.Grade = txtGrade.Text;
// Add new student to the list
lstStudent.Items.Add(student);
clearTextBoxes();
save = false;
}
public void saveStudents()
{
// Save current student information to text file
StreamWriter sw = null;
try
{
sw = new StreamWriter(mStudentsFileName);
foreach (clsStudent student in lstStudent.Items)
{
sw.WriteLine(student.Serialize());
}
}
catch (Exception ex)
{
messageBoxOK("Error saving students to file "
+ mStudentsFileName + ". " + ex.Message);
}
finally
{
if (sw != null)
{
sw.Close();
}
}
clearTextBoxes();
}
private void messageBoxOK(string message)
{
MessageBox.Show(message, Text, MessageBoxButtons.OK,
MessageBoxIcon.Information);
return;
}
private void clearTextBoxes()
{
txtLastName.Text = "";
txtFirstName.Text = "";
txtStudentID.Text = "";
txtSection.Text = "";
txtClassification.Text = "";
txtGrade.Text = "";
txtLastName.Focus();
}
private void restoreStudents()
{
StreamReader sr = null;
string inputLine;
clsStudent student;
lstStudent.Items.Clear();
try
{
if (File.Exists(mStudentsFileName) == true)
{
sr = new StreamReader(mStudentsFileName);
inputLine = sr.ReadLine();
while (inputLine != null)
{
student = new clsStudent();
student.Deserialize(inputLine);
lstStudent.Items.Add(student);
inputLine = sr.ReadLine();
}
}
}
catch (Exception ex)
{
messageBoxOK("Error restoring contacts from file "
+ mStudentsFileName + ", " + ex.Message);
}
finally
{
if (sr != null)
{
sr.Close();
}
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
// Make sure item is selected if true delete display
// message otherwise
if (lstStudent.SelectedIndex != -1)
{
lstStudent.Items.RemoveAt(lstStudent.SelectedIndex);
}
else
{
messageBoxOK("Please select a student to remove first.");
return;
}
clearTextBoxes();
save = false;
}
private void mnuSave_Click(object sender, EventArgs e)
{
saveFile();
}
private void mnuOpen_Click(object sender, EventArgs e)
{
OpenFileDialog ofd;
ofd = new OpenFileDialog();
ofd.Title = "Select a Students file to open";
ofd.Filter = "Students (*.txt)|*.txt|All files (*.*)|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
// If the user specified a new filename, save the existing
// students (if a filename is present), and restore the
// students from the new file.
if (mStudentsFileName != null)
{
saveStudents();
}
mStudentsFileName = ofd.FileName;
restoreStudents();
}
clearTextBoxes();
}
private void btnEdit_Click(object sender, EventArgs e)
{
clsStudent student = new clsStudent();
if (txtLastName.Text == "" || txtFirstName.Text == "" ||
txtStudentID.Text == "" || txtSection.Text == "" ||
txtClassification.Text == "" || txtGrade.Text == "")
{
messageBoxOK("Please fill in all the fields.");
return;
}
student.LastName = txtLastName.Text;
student.FirstName = txtFirstName.Text;
student.ID = txtStudentID.Text;
student.Section = txtSection.Text;
student.Classification = txtClassification.Text;
student.Grade = txtGrade.Text;
// Edit student on the list
int tempSelectedIndex = lstStudent.SelectedIndex;
lstStudent.Items.RemoveAt(tempSelectedIndex);
lstStudent.Items.Insert(tempSelectedIndex, student);
clearTextBoxes();
save = false;
}
private void lstStudent_SelectedIndexChanged(object sender, EventArgs e)
{
string inputValue;
clsStudent student = new clsStudent();
if (lstStudent.SelectedIndex != -1)
{
inputValue = lstStudent.Items[lstStudent.SelectedIndex].ToString();
student.Deserialize(inputValue);
// Display selected values in text boxes.
txtLastName.Text = student.LastName;
txtFirstName.Text = student.FirstName;
txtStudentID.Text = student.ID;
txtSection.Text = student.Section;
txtClassification.Text = student.Classification;
txtGrade.Text = student.Grade;
}
}
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (save == false)
{
if (MessageBox.Show("Changes on the form have not been saved. Save?",
Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
saveFile();
}
}
}
private void saveFile()
{
SaveFileDialog sfd;
sfd = new SaveFileDialog();
sfd.Title = "Enter a name and location for the new students file";
sfd.Filter = "Students (*.txt)|*.txt|All files (*.*)|*.*";
if (sfd.ShowDialog() == DialogResult.OK)
{
// If the user specified a new filename, save the existing
// students (if a filename is present), store the new filename,
// and clear the user interface.
if (mStudentsFileName != null)
{
saveStudents();
}
mStudentsFileName = sfd.FileName;
}
save = true;
}
}
clsStudent:
Code: using System;
public class clsStudent
{
// List of class data members
private string mLastName;
private string mFirstName;
private string mID;
private string mSection;
private string mClassification;
private string mGrade;
// -------------- Accessor methods for data members ---------------
// Purpose: to read or write the mLastName data member
public string LastName
{
get
{
return mLastName;
}
set
{
string temp;
// Make everything lowercase
value = value.ToLower();
//Pull out the first letter
temp = value.Substring(0, 1);
// Build by making first letter upper case
value = temp.ToUpper() + value.Substring(1, value.Length - 1);
mLastName = value;
}
}
// Purpose: to read or write the mFirstName data member
public string FirstName
{
get
{
return mFirstName;
}
set
{
string temp;
// Make everything lowercase
value = value.ToLower();
//Pull out the first letter
temp = value.Substring(0, 1);
// Build by making first letter upper case
value = temp.ToUpper() + value.Substring(1, value.Length - 1);
mFirstName = value;
}
}
// Purpose: to read or write the mID data member
public string ID
{
get
{
return mID;
}
set
{
mID = value;
}
}
// Purpose: to read or write the mSection data member
public string Section
{
get
{
return mSection;
}
set
{
mSection = value;
}
}
// Purpose: to read or write the mClassification data member
public string Classification
{
get
{
return mClassification;
}
set
{
mClassification = value;
}
}
// Purpose: to read or write the mGrade data member
public string Grade
{
get
{
return mGrade;
}
set
{
mGrade = value;
}
}
// --------------------- Class Methods -------------------------
public string Serialize()
{
return mLastName + "\t"
+ mFirstName + "\t"
+ mID + "\t"
+ mSection + "\t"
+ mClassification + "\t"
+ mGrade + "\t";
}
public void Deserialize(string serializedStudent)
{
string[] values = serializedStudent.Split('\t');
try
{
mLastName = values[0];
mFirstName = values[1];
mID = values[2];
mSection = values[3];
mClassification = values[4];
mGrade = values[5];
}
catch
{
return;
}
}
}
Last edited by Guest on 15 Apr 2008 08:44:49 pm; edited 1 time in total |