更新当前打开的表单中的控件

时间:2015-04-14 05:24:17

标签: c# forms

我正在写一个C#应用程序,我可以添加各种类型的学生 - 普通学生,学术社会学生,或艺术和文化社会学生。在主表格上,我有3个数据网格(一个列出学术学生,一个列出艺术和文化学生,另一个列出正常学生)。为了让用户指定有关学生的其他信息(如果他们是学术社会学生,或艺术和文化学生,或两者兼有),将打开另一个表格,要求用户添加其他信息。

在指定信息之后,我想获取该信息,并将其添加到相关数据网格,换句话说,更新主窗体中的数据网格。

我怎么想我会解决这个问题:

  1. 在主窗体中创建一个方法,以向数据网格添加新条目
  2. 将主窗体对象保存到窗体对象
  3. 有一个方法可以将新的数据行添加到步骤2中提到的表单对象中
  4. 使用我保存的表单对象更新当前打开的主窗体。
  5. 我尝试了以上操作,但我得到了错误:

      

    错误1可访问性不一致:参数类型'ONT2000_Practical_05.AcademicSocieties'不如方法'ONT2000_Practical_05.Form1.addAcademicStudentRow(ONT2000_Practical_05.AcademicSocieties)'c:\ users \ okuhle \ documents \ visual studio 2013 \ Projects \ ONT2000 Practical 05 \ ONT2000 Practical 05 \ Form1.cs 35 21 ONT2000 Practical 05

    我有3个班级 - 学术团体,ArtsAndCultureSociety和学生......学术团体和ArtsAndCultureSociety都继承了学生班级。以下是课程的代码:

    THE STUDENT CLASS: 
    
        using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ONT2000_Practical_05
    {
        public class Student
        {
            private String studentNumber;
            private String studentName;
            private String studentDegree;
    
            public Student(string number, string name, string degree)
            {
                studentNumber = number;
                studentName = name;
                studentDegree = degree;
            }
    
            public String getStudentName()
            {
                return studentName;
            }
    
            public String getStudentNumber()
            {
                return studentNumber;
            }
    
            public String getStudentDegree()
            {
                return studentDegree;
            }
    
        }
    }
    

    THE ACADEMICSSOCIETY CLASS:

        using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ONT2000_Practical_05
    {
        public class AcademicSocieties : Student
        {
            private String courseCode;
            private String societyName;
    
            public AcademicSocieties(String studentName, String studentNumber, String studentDegree, String courseCode, String societyName) : base(studentNumber, studentName, studentDegree)
            {   
                this.courseCode = courseCode;
                this.societyName = societyName;
            }
    
            public String getCourseCode()
            {
                return courseCode;
            }
    
            public String getSocietyName()
            {
                return societyName;
            }
        }
    }
    

    ARTSANDCULTURE SOCIETY CLASS

        using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ONT2000_Practical_05
    {
        class ArtsAndCultureSociety : Student
        {
            private int experienceLevel;
            private int competitionWins;
            private String societyName;
            private Boolean colours;
    
            public ArtsAndCultureSociety(int level, int wins, string societyName, String studentNumber, String studentName, String studentDegree) : base(studentNumber, studentName, studentDegree)
            {
                experienceLevel = level;
                competitionWins = wins;
                this.societyName = societyName;
            }
        }
    }
    

    主要表格:

        using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace ONT2000_Practical_05
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void academicSocietiesToolStripMenuItem_Click(object sender, EventArgs e)
            {
    
            }
    
            private void degreeLabel_Click(object sender, EventArgs e)
            {
    
            }
    
            private void exitApplicationToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Environment.Exit(0);
            }
    
            public void addAcademicStudentRow(AcademicSocieties thisStudent) //This is where the Error Occurs
            {
                academicSocietiesDataGrid.Rows.Add(thisStudent.getStudentName(), thisStudent.getSocietyName(), thisStudent.getCourseCode());
            }
    
            private void addStudentButton_Click(object sender, EventArgs e)
            {
                String studentName = ProgramFunctions.validateTextBoxData(studentNameTextBox);
                String studentNumber = ProgramFunctions.validateStudentNumber(studentNumberTextBox);
                String studentDegree = ProgramFunctions.validateTextBoxData(degreeTextBox);
    
                if (studentName.Equals(null) || studentNumber.Equals(null) || studentDegree.Equals(null) || studentDegree.Equals("null")) //Error 1 is on this line
                {
                    ProgramFunctions.displayMessage("Data Integrity Error", "As a result of one or more fields failing data validation, this application will not continue processing data");
                } else
                {
                    if (artsAndCultureCheckBox.Checked && academicCheckBox.Checked)
                    {
                        Student newStudent = new Student(studentNumber, studentName, studentDegree);
                        StudentData.setCurrentStudent(newStudent);
                        ProgramFunctions.saveCurrentForm(this);
                        GeneralStudentSocietyForm generalForm = new GeneralStudentSocietyForm();
                        generalForm.Visible = true;
                        generalForm.Focus();
    
                    } else if (academicCheckBox.Checked)
                    {
                        Student newStudent = new Student(studentNumber, studentName, studentDegree);
                        StudentData.setCurrentStudent(newStudent);
                        ProgramFunctions.saveCurrentForm(this);
                        AcademicSocietyForm academics = new AcademicSocietyForm();
                        academics.Visible = true;
                        academics.Focus();     
                    } else if (artsAndCultureCheckBox.Checked)
                    {
                        Student newStudent = new Student(studentNumber, studentName, studentDegree);
                        StudentData.setCurrentStudent(newStudent);
                        ProgramFunctions.saveCurrentForm(this);
                        ArtsAndCultureForm artsAndCulture = new ArtsAndCultureForm();
                        artsAndCulture.Visible = true;
                        artsAndCulture.Focus(); 
                    } else
                    {
                        Student newStudent = new Student(studentNumber, studentName, studentDegree);
                        StudentData.addNewStudent(newStudent);
                        ProgramFunctions.displayMessage("Student Added", "A New Student has successfully been added to the database. Click OK to continue");
    
                        studentDataDataGird.Rows.Add(newStudent.getStudentName(), newStudent.getStudentNumber(), newStudent.getStudentDegree());
                    }
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                ProgramFunctions.addNewAcademicSociety("Accounting Society");
                ProgramFunctions.addNewAcademicSociety("Law Student Society");
                ProgramFunctions.addNewAcademicSociety("Science Student Society");
                ProgramFunctions.addNewAcademicSociety("Information Technology Student Society");
                ProgramFunctions.addNewAcademicSociety("Business Science Student Society");
    
                ProgramFunctions.addNewArtsAndCultureSociety("Choir Society");
                ProgramFunctions.addNewArtsAndCultureSociety("Hip Hop Society");
                ProgramFunctions.addNewArtsAndCultureSociety("Anime Society");
                ProgramFunctions.addNewArtsAndCultureSociety("The Hockey Society");
            }
    
            private void studentDataDataGird_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
    
            }
    
            public void academicSocietiesDataGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
    
            }
        }
    }
    

    PROGRAMFUNCTIONS类(这是我保存表单对象的地方):

      using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace ONT2000_Practical_05
    {
        class ProgramFunctions
        {
            private static List<String> academicSocieties = new List<String>();
            private static List<String> artsAndCultureSocieties = new List<String>();
    
            private static Form1 formObject;
    
            public static void saveCurrentForm(Form1 formData)
            {
                formObject = formData;
            }
    
            public static void academicStudentDataGridRow(AcademicSocieties newStudent)
            {
                formObject.addAcademicStudentRow(newStudent);
            }
    
            public static Form1 updateMainForm()
            {
                return formObject;
            }
    
            public static void addNewAcademicSociety(String societyName)
            {
                academicSocieties.Add(societyName);
            }
    
            public static void addNewArtsAndCultureSociety(String societyName)
            {
                artsAndCultureSocieties.Add(societyName);
            }
    
            public static void displayMessage(String title, String Message)
            {
                MessageBox.Show(Message, title);
            }
    
            public static String validateTextBoxData(TextBox thisTextBox)
            {
                if (thisTextBox.Text.Equals(null) || thisTextBox.Text.Equals("") || thisTextBox.Text.Equals(" "))
                {
                    displayMessage("Empty Data Detected!", "You may not specify empty data for the student");
                    return null;
                } else
                {
                    thisTextBox.Text = thisTextBox.Text.Trim();
                    thisTextBox.Text = thisTextBox.Text.ToUpper();
                    return thisTextBox.Text;
                }
            }
    
            public static String getSelectedItem(ComboBox thisComboBox)
            {
                return thisComboBox.SelectedItem.ToString();
            }
    
            public static int getArtsAndCultureCount()
            {
                return artsAndCultureSocieties.Count;
            }
    
            public static int getAcademicSocietyCount()
            {
                return academicSocieties.Count;
            }
    
            public static String getAcademicSociety(int index)
            {
                return academicSocieties[index];
            }
    
            public static String getArtsAndCultureSociety(int index)
            {
                return artsAndCultureSocieties[index];
            }
    
            public static String validateStudentNumber(TextBox studentNumberTextBox)
            {
                if (studentNumberTextBox.Text.Equals(null) || studentNumberTextBox.Text.Equals("") || studentNumberTextBox.Text.Equals(" "))
                {
                    displayMessage("Empty Data Detected!", "You did not input any data...Please be sure you do specify some data");
                    return null;
                } else
                {
                    if (!studentNumberTextBox.Text.StartsWith("s"))
                    {
                        displayMessage("Invalid Student Number", "You have entered an invalid student number. Please be sure this student number follows the correct format. The student number must begin with a 's' character. ");
                        return null;
                    }
    
                    if (studentNumberTextBox.Text.Length != 10)
                    {
                        displayMessage("Invalid Student Number", "The student number specified may not be longer than 10 characters");
                        return null;
                    }
    
                    return studentNumberTextBox.Text;
                }
            }
    
        }
    }
    

2 个答案:

答案 0 :(得分:0)

您是否在某些类定义中缺少访问修饰符'public'?如果你没有在ArtsAndCultureSociety之前添加公开,那么它将是私有的。

答案 1 :(得分:0)

将ProgramFunctions,ArtsAndCultureSociety类的访问修饰符更改为public。