【问题标题】:How can I access these controls from a different class?如何从不同的类访问这些控件?
【发布时间】:2025-12-18 22:15:01
【问题描述】:

我有一个表单(编辑),其中一些文本框控件作为私有成员,具有我想从另一个类(PatientService)访问的公共属性,但我不知道如何克服“当前文本框不存在我尝试访问的 8 个控件的上下文“错误。另外,通过构造函数传递这些值是一种很好的方法吗?除了与数据库交互的 PatientService 类之外,我的项目中不能有任何其他部分。谢谢,有问题的文本框以粗体显示。

public partial class Edit : XtraForm

    {
        private string patientID;
        private string firstName;
        private string lastName;
        private string address;
        private string city;
        private string state;
        private string zipCode;
        private string phone;

        public Edit(string PatientID, string FirstName, string LastName, string Address, string City, string State, string ZipCode, string Phone)
        {
            InitializeComponent();
            patientID = txtPatientID.Text;
            firstName = txtFirstName.Text;
            lastName = txtLastName.Text;
            address = txtAddress.Text;
            city = txtCity.Text;
            state = txtState.Text;
            zipCode = txtZipCode.Text;
            phone = txtPhone.Text;

        }

        public string PatientID 
        {

            get { return patientID; }
            set { patientID = value; }


        }
        public string FirstName
        {

            get { return firstName; }
            set { firstName = value; }

        }
        public string LastName
        {

            get { return lastName; }
            set { lastName = value; }

        }
        public string Address
        {

            get { return address; }
            set { address = value; }

        }
        public string City
        {

            get { return city; }
            set { city = value; }

        }
        public string State
        {

            get { return state; }
            set { state = value; }

        }
        public string ZipCode
        {

            get { return txtZipCode.Text; }
            set { txtZipCode.Text = value; }

        }
        public string Phone
        {

            get { return phone; }
            set { phone = value; }

        }

public void CreatePatient()
            {

                //SAConnection conn = new SAConnection("dsn={SQL Anywhere 10};uid=dba;pwd=sql;databasefile=C:\\Users\\Kbaker1\\Desktop\\Training1.db;");
                //SACommand cmd = new SACommand("INSERT INTO patient(patient_id, first_name, last_name, address, city, state, zipcode, phone) VALUES(); ");

                using (SAConnection conn = new SAConnection())
                {
                  conn.ConnectionString = "dsn={SQL Anywhere 10};uid=dba;pwd=sql;databasefile=C:\\Users\\Kbaker1\\Desktop\\Training1.db;";
                    conn.Open();

                    using (SACommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText =
                        "insert into patient(\n" +
                        "  patient_id,\n" +
                        "  first_name,\n" +
                        "  last_name,\n" +
                        "  address,\n" +
                        "  city,\n" +
                        "  state,\n" +
                        "  zipcode,\n" +
                        "  phone)\n" +
                        "  values(\n" +
                        "  @prm_patient_id,\n" +
                        "  @prm_first_name,\n" +
                        "  @prm_last_name,\n" +
                        "  @prm_address,\n" +
                        "  @prm_city,\n" +
                        "  @prm_state,\n" +
                        "  @prm_zipcode,\n" +
                        "  @prm_phone)";

cmd.Parameters.Add("@prm_patient_id", SADbType.VarChar, 80).Value = **txtPatientID.Text**;
cmd.Parameters.Add("@prm_first_name", SADbType.VarChar, 80).Value = **txtFirstName.Text**;
cmd.Parameters.Add("@prm_last_name", SADbType.VarChar, 80).Value = **txtLastName.Text**;
cmd.Parameters.Add("@prm_address", SADbType.VarChar, 80).Value = **txtAddress.Text**;
cmd.Parameters.Add("@prm_city", SADbType.VarChar, 80).Value = **txtCity.Text**;
cmd.Parameters.Add("@prm_state", SADbType.VarChar, 80).Value = **txtState.Text**;
cmd.Parameters.Add("@prm_zipode", SADbType.VarChar, 80).Value = **txtZipCode.Text**;
cmd.Parameters.Add("@prm_phone", SADbType.VarChar, 80).Value = **txtPhone.Text**;

                   cmd.ExecuteNonQuery();

                    }
                }
            } 

好的,所以我还是有点困惑。我创建了 Patient 类,并像这样在 Edit 表单中对其进行了实例化。

公开病人拍;

    public Edit(Patient patient)
    {
        InitializeComponent();
        pat = patient;
    }

当我单击“确定”按钮时,我正试图让它通过 PatientService 类中的 CreatePatient 方法插入到数据库中。

下面是 Edit Form 中调用 PatientService 类中的 CreatePatient 方法的方法:

private void btnOK_Click(object sender, EventArgs e) {

        PatientService ps = new PatientService();
        ps.CreatePatient();
     }

我的 Patient 类如下所示:

公开课病人 {

    List<Patient> patList = new List<Patient>();
    private string patientID;
    private string firstName;
    private string lastName;
    private string address;
    private string city;
    private string state;
    private string zipCode;
    private string phone;
    private int classificationID;
    protected object Dispose;

    public Patient(string PatientID, string FirstName, string LastName, string Address, string City, string State, string ZipCode, string Phone, int ClassificationID)
    {
        this.patientID = PatientID;
        this.firstName = FirstName;
        this.lastName = LastName;
        this.address = Address;
        this.city = City;
        this.state = State;
        this.zipCode = ZipCode;
        this.phone = Phone;
        this.classificationID = ClassificationID;

    }


      public string PatientId
    {
        get { return patientID; }
        set { patientID = value; }
    }

    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

    public string LastName
    {
        get { return lastName; }
        set { lastName = value; }
    }

    public string Address
    {
        get { return address; }
        set { address = value; }
    }

    public string City
    {
        get { return city; }
        set { city = value; }
    }

    public string State
    {
        get { return state; }
        set { state = value; }
    }

    public string ZipCode
    {
        get { return zipCode; }
        set { zipCode = value; }
    }

    public string Phone
    {
        get { return phone; }
        set { phone = value; }
    }

    public int ClassificationID
    {
        get { return classificationID; }
        set { classificationID = value; }

    }

    public Patient(string PatientID)
    {
        this.patientID = PatientID;
    }

    public Patient()
    {

    }

}

}

所以考虑到我不再像一开始那样通过 Edit 构造函数传递值,我将如何利用 Patient 类来获取发送到数据库的文本框值?

【问题讨论】:

    标签: c#


    【解决方案1】:

    您不需要从其他表单或类访问您的控件。 由于您有公共属性,因此您可以从父表单访问它:

    Edit form = new Edit(patientId, ...);
    //after using form
    string patientId = form.PatientID;
    

    更好的选择是将字段包装到单个对象中,例如实体

    public class Patient
    {
            private string patientID;
            private string firstName;
            private string lastName;
            private string address;
            private string city;
            private string state;
            private string zipCode;
            private string phone;
    
            //put here your properties
    }
    

    在您的编辑表单中使用它

    public partial class Edit : XtraForm
    {
        public Patient Patient;
    
        public Edit() //empty constructor if you want to pass data manually via property
        {
            InitializeComponent();
        }
    
        public Edit(Patient patient)
        {
            InitializeComponent();
            Patient = patient;
        }
    
        //full code here
    }
    

    您始终可以在文本框中使用EditValueChanged 事件将实际数据保存在 Patient 对象中(据我所知,您正在使用 DevExpress 控件,例如 XtraForm)。例如:

    private void txtPatientID_EditValueChanged(object sender, EventArgs e)
    {
        Patient.patientId = txtPatientID.Text;
    }
    

    【讨论】:

    • 我不确定 "" 中的字符串值应该代表什么。代码会不会像这样: Edit ed = new Edit(); ed.PatientID = "1"; ed.FirstName = "2"; ed.LastName = "3"; ed.Address = "4"; ed.City = "5"; ed.State = "6"; ed.ZipCode = "7"; ed.Phone = "8";
    【解决方案2】:

    您需要将表单类传递给PatientService 例如:

    public class PatientService
    {
        //your code
        public Edit EditForm{get;set;}
    }
    

    现在您可以将 Edit 传递给 PatientService: 某处:

    var svc = new PatientService();
    svc.EditForm = existEditForm;
    

    您现在可以从患者服务处访问游览编辑表单。像这样的:

    EditForm.PatientId = "0";
    

    【讨论】:

    • 感谢您的帮助。我会尝试两个!