我有一个表单(编辑),其中一些文本框控件作为私有成员,具有我想从另一个类(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方法将文本框控件插入到数据库中。
以下是编辑表单中调用PatientService类中的CreatePatient方法的方法:
private void btnOK_Click(object sender,EventArgs e) {
PatientService ps = new PatientService();
ps.CreatePatient();
}
我的患者课程如下:
公共课患者 {
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类来获取发送到数据库的文本框值?
答案 0 :(得分: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;
}
答案 1 :(得分:1)
您需要将表单类传递给PatientService
例如:
public class PatientService
{
//your code
public Edit EditForm{get;set;}
}
现在您可以将Edit传递给PatientService: 某处:
var svc = new PatientService();
svc.EditForm = existEditForm;
您现在可以从患者服务中访问旅游编辑表格。像这样:
EditForm.PatientId = "0";