Click here to Skip to main content
15,893,161 members

Trying to access form elements from a separate Class - Rephrased Version

Revision 3
I'm trying to access textbox details inside a class that relate to another form. I now have the code working to an extent using {get; set;} but while the method displayDetails works fine from the class Students, if I try making a public method in the Students class to add the details (by passing the student object as an argument (the code is currently located in Private void btnSubmit_Click) the code will compile but the details will not be displayed in the MessageBox. Basically what I want it the two methods in the Students class (which are simply called by the button click event) so I assume I'm making an idiotic but fixable mistake. Any help greatly appreciated. Revised code is below:
//Form1.cs - ManageStudentsWin
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ManageStudentsWin
{
    public partial class Form1 : Form
    {
        public int StudentID
        {
            get { return Convert.ToInt16(txtStudentID.Text); }
            set { txtStudentID.Text = value.ToString(); }
        }
        public string StudentFirstName
        {
            get { return txtStudentFirstName.Text; }
            set { txtStudentFirstName.Text = value; }
        }
        public string StudentLastName
        {
            get { return txtStudentSecondName.Text; }
            set { txtStudentSecondName.Text = value; }
        }
        public string StudentCourseCode
        {
            get { return txtCourseCode.Text; }
            set { txtCourseCode.Text = value; }
        }
        public int CurrentYear
        {
            get { return Convert.ToInt16(txtYear.Text); }
            set { txtYear.Text = value.ToString(); }
        }
        public Form1()
        {
            InitializeComponent();
        }


        Students s1 = new Students();
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            s1.StudentID = StudentID;
            s1.StudentFirstName = StudentFirstName;
            s1.StudentLastName = StudentLastName;
            s1.StudentCourseCode = StudentCourseCode;
            s1.CurrentYear = CurrentYear;
            
        }

        private void btnDisplay_Click(object sender, EventArgs e)
        {
            s1.displayDetails(s1);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

//Students.cs - ManageStudentsWin
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace ManageStudentsWin
{
    class Students
    {
        public string StudentFirstName;
        public int StudentID;
        public string StudentLastName;
        public string StudentCourseCode;
        public int CurrentYear;
        
       
        public void displayDetails(Students s1)
        {
            MessageBox.Show("Your ID is: " + StudentID + "\nYour First Name is: " + StudentFirstName + "\nYour Last Name is: " + StudentLastName + "\nYour Course Code is: " + StudentCourseCode + "\nYour year is: " + CurrentYear);
        }
    }
   
}
Posted 16-Sep-12 3:39am by BrianHamilton.
Tags: ,