尝试从单独的类访问表元素-改写版本 [英] Trying to access form elements from a separate Class - Rephrased Version

查看:88
本文介绍了尝试从单独的类访问表元素-改写版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问与另一种形式相关的类中的文本框详细信息.我现在使用{get; set;},但方法displayDetails在Student类中可以正常工作,但是,如果我尝试在Students类中使用公共方法来添加详细信息(通过将Student对象作为参数传递(该代码当前位于Private void btnSubmit_Click中)代码会编译,但是详细信息不会显示在MessageBox中.基本上,我想要的是Student类中的两个方法(它们由按钮click事件简单地调用),所以我假设我在做一个白痴但可修复的方法错误.非常感谢您的帮助.修改后的代码如下:

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);
        }
    }
   
}

推荐答案

首先要注意的是,您根本不应该尝试.

如果直接公开表单控件(通过使它们成为public),则锁定表单的设计-您将无法再更改表单的工作方式,而不会潜在地影响使用它的任何其他代码.这不是一个好主意,这就是为什么默认情况下所有控件都是private的原因.
相反,您应该提供访问信息的公共属性,并让外部代码使用它们.然后,您可以在需要时更改表单的工作方式,并且只要属性仍返回相同的值,则不会影响外部代码:

The first thing to note is that you really shouldn''t be trying at all.

If you expose your form controls directly (by making them public) then you lock the design of your form - you can no longer change the way the form works without potentially affecting any other code which may use it. This is not a good idea, and is why all controls are private by default.

Instead, you should provide public properties which access the information, and let external code use them. You can then change the way your form works when you need to, and provided the properties still return the same values, no external code is affected:

public string StudentFirstName
   {
   get { return txtStudentFirstName.Text; }
   set { txtStudentFirstName.Text = value; }
   }



出现异常的原因可能是您试图在其自身的实例中构造表单-Form1是Visual Studio中主表单的默认名称,如果您未更改它,则尝试构造在Form1内的Form1,这意味着它试图在其中构建另一个Form1,以此类推... util耗尽了内存,并引发了异常.



The reason you are getting an exception is probably that you are trying to construct a form within an instance of itself - Form1 is the default name for the main form in Visual Studio and if you haven''t changed it then you are trying to construct Form1 inside Form1, which means that it tries to build another Form1 inside that, which... and so on util you run out of memory and it throws an exception.


这篇关于尝试从单独的类访问表元素-改写版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆