我有一个Form1,在其中创建Medic对象并将其添加到listaMedici1。在Form2中,我创建了Pacient对象,并将它们添加到listapacienti2中,并将其发送到Form1。
在Form1中,我有2个列表框:在listbox1_medici中,我显示了Medic对象的列表。 我想做的是,当我从listbox1_medici中选择一个Medic对象时,在listbox2_pacienti中显示所有Pacient对象,这些对象具有字段codM,与所选对象字段codM相同。我已经尝试过这种方法,但是它不起作用。有什么事吗谢谢。
Form1: Button3用于用Medic对象填充listbox1_medici
假定SelectieItem方法和事件SelectedIndexChanged方法显示的Pacient对象与listbox1_medici中选择的Medic对象具有相同的codM,但它们不起作用
public partial class Form1 : Form
{
public List<Medic> listaMedici1 = new List<Medic>();
public List<Pacient> listaPacienti1 = new List<Pacient>();
public ArrayList specialitati = new ArrayList();
public Form1()
{
InitializeComponent();
specialitati.Add("Endocrinologie");
specialitati.Add("Oftalmologie");
specialitati.Add("Neurologie");
specialitati.Add("Cardiologie");
for (int i = 0; i < specialitati.Count; i++)
comboBox1spec.Items.Add(specialitati[i]);
Medic m1 = new Medic(1, "Ionescu", "Endocrinologie");
Medic m2 = new Medic(2, "Popescu", "Cardiologie");
listaMedici1.Add(m1);
listaMedici1.Add(m2);
}
private void button1_Click(object sender, EventArgs e)
{//this is where i create objects using the textboxes
}
private void button2_Click(object sender, EventArgs e)
{
Form2 form = new Form2(listaMedici1);
// form.listaMedici2 = listaMedici1;
form.ShowDialog();
this.Close();
}
private void button3_Click(object sender, EventArgs e)
{
foreach (Medic m in listaMedici1)
{
if(!(listBox1_medici.Items.Contains(m)))
{
listBox1_medici.Items.Add(m.ToString());
}
}
}
private void SelectieItem(object sender, EventArgs e)
{
if(listBox1_medici.SelectedItems.Count==1)
{
Medic med = listBox1_medici.SelectedItem as Medic;
for(int i=0;i<listaPacienti1.Count;i++)
{
if(med.CodM==listaPacienti1[i].CodM)
{
listBox2_pacienti.Items.Add(listaPacienti1[i].ToString());
}
}
}
}
private void listBox2_pacienti_SelectedIndexChanged(object sender, EventArgs e)
{
SelectieItem(sender, e);
}
}
Form2:
public partial class Form2 : Form
{
public List<Medic> listaMedici2 = new List<Medic>();
public List<Pacient> listaPacienti2 = new List<Pacient>();
Form1 form1;
public Form2()
{
InitializeComponent();
Pacient p1 = new Pacient(1, "Danut", 2, 230);
Pacient p2 = new Pacient(2, "Cristi", 1, 120);
listaPacienti2.Add(p1);
listaPacienti2.Add(p2);
}
public Form2(List<Medic> listaMedici1)
{
InitializeComponent();
listaMedici2 = listaMedici1;
foreach (Medic med in listaMedici2)
{
comboBox1codmed.Items.Add(med.CodM.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{//this is where i create Pacient objects using the textboxes
}
private void button2_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.listaPacienti1 = listaPacienti2;
frm.ShowDialog();
this.Close();
}
}