i created a button and i did this :
LFunc.ToArray();
LFunc.Sort()
but i got an error : Failed to compare two elements of the matrix
but also, doing this i dont sort the list by code.
Each employer have 7 fields, code, name, adress,phone,etc.
and i need to order in a listbox the employers by code..
Im trying to do the sort of the employer list but until now i can´t do it !
Can you Please seeing the code and try to find the better way to sort my list?
Sorry for putting all this code but i dont know what to do.
Very Thanks!!
public partial class FormEmployer : Form
{
List<employer> LEmp = new List<employer>();
public FormEmployer()
{
InitializeComponent();
ReadEmployers();
}
private void bttonRemove_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex == -1)
return;
LEmp.Remove(LFunc[listBox1.SelectedIndex]);
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
SaveEmployer();
}
private void bttonAdd_Click(object sender, EventArgs e)
{
if (cb_type.Text == "")
{
MessageBox.Show("Please put the type of employer", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else if (cb_type.Text == "" || tb_code.Text == "" || tb_Name.Text == "" || dateTimePicker1.Text == "" || cb_Sex.Text == "" || tb_Adress.Text == "" || tb_tphone.Text == "" || tb_Email.Text == "")
{
MessageBox.Show("Fields empty", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else if (cb_type.Text == "Vet")
{
Vet V = new Vet(Convert.ToInt32(tb_Code.Text), tb_Name.Text, Convert.ToDateTime(dateTimePicker1.Text), cb_Sex.Text, tb_Adress.Text, Convert.ToInt32(tb_phone.Text), tb_Email.Text, pictureBox1.Image);
LEmp.Add(V);
listBox1.Items.Add(V.getName());
listBox1.Show();
}
else if (cb_tipo.Text == "Admin")
{
Admin Adm = new Admin(Convert.ToInt32(tb_Code.Text), tb_Name.Text, Convert.ToDateTime(dateTimePicker1.Text), cb_Sex.Text, tb_Adress.Text, Convert.ToInt32(tb_phone.Text), tb_Email.Text, pictureBox1.Image);
LEmp.Add(Adm);
listBox1.Items.Add(Adm.getNome());
listBox1.Show();
}
else if (cb_type.Text == "Treater")
{
Treater T = new Treater(Convert.ToInt32(tb_Code.Text), tb_Name.Text, Convert.ToDateTime(dateTimePicker1.Text), cb_Sex.Text, tb_Adress.Text, Convert.ToInt32(tb_phone.Text), tb_Email.Text, pictureBox1.Image);
LEmp.Add(T);
listBox1.Items.Add(T.getName());
listBox1.Show();
}
SaveEmployers();
}
}
private void bttonClearFIelds_Click(object sender, EventArgs e)
{
cb_tipo.Text = "";
tb_BI.Text = "";
tb_Nome.Text = "";
cb_Sexo.Text = "";
tb_Morada.Text = "";
tb_Morada.Text = "";
tb_telefone.Text = "";
pictureBox1.Image = pictureBox1.InitialImage;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex == -1)
return;
cb_type.Text = LEmp[listBox1.SelectedIndex].getType();
tb_COde.Text = LEmp[listBox1.SelectedIndex].getBi().ToString();
tb_Name.Text = LEmp[listBox1.SelectedIndex].getName();
dateTimePicker1.Text=LEmp[listBox1.SelectedIndex].getBorn().ToString();
cb_Sex.Text = LEmp[listBox1.SelectedIndex].getSex();
tb_Adress.Text = LEmp[listBox1.SelectedIndex].getAdress();
tb_Phone.Text = LEmp[listBox1.SelectedIndex].getPhone().ToString();
tb_Email.Text = LEmp[listBox1.SelectedIndex].getEmail();
pictureBox1.Image = LEmp[listBox1.SelectedIndex].getPhoto();
}
private void bttonAddPhoto_Click_1(object sender, EventArgs e)
{
OpenFileDialog importarImagem = new OpenFileDialog();
importarImagem.Filter = "Jpg files(*.jpg) | *.jpg";
importarImagem.InitialDirectory = @"C:\";
if (importarImagem.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(importarImagem.FileName);
}
}
public void SaveEmployer()
{
string gravar = "DadosFuncionarios.txt";
StreamWriter textOut = new StreamWriter(new FileStream(gravar, FileMode.Create, FileAccess.Write));
foreach (Funcionario F in LFunc)
{
F.GravarFicheiro(textOut);
}
textOut.Close();
}
private void FormFuncionario_Deactivate(object sender, EventArgs e)
{
GravarFuncionario();
}
public void ReadEmployer()
{
string ler = "DadosFuncionarios.txt";
if (!File.Exists(ler))
return;
StreamReader txtln = new StreamReader(new FileStream(ler, FileMode.Open, FileAccess.Read));
LFunc.Clear();
listBox1.Items.Clear();
while (txtln.Peek() != -1)
{
string linha = txtln.ReadLine();
string[] campos = linha.Split('|');
Funcionario Func;
if (campos[0] == "Administrativo")
{
Func = new Administrativo(Convert.ToInt32(campos[1]), campos[2], Convert.ToDateTime(campos[3]), campos[4], campos[5], Convert.ToInt32(campos[6]), campos[6], pictureBox1.Image);
}
else if (campos[0] == "Tratador")
{
Func = new Tratador(Convert.ToInt32(campos[1]), campos[2], Convert.ToDateTime(campos[3]), campos[4], campos[5], Convert.ToInt32(campos[6]), campos[6], pictureBox1.Image);
}
else //if (campos[0] == "Veterinário") não é preciso porque é o que resta basta o else
{
Func = new Veterinario(Convert.ToInt32(campos[1]), campos[2], Convert.ToDateTime(campos[3]), campos[4], campos[5], Convert.ToInt32(campos[6]), campos[6], pictureBox1.Image);
}
LFunc.Add(Func);
listBox1.Items.Add(Func.getNome());
}
txtln.Close();
}
}