les dejo el codigo que tengo
ESTE ES EL DISEÑO https://ibb.co/CzRWjLd
lo que quiero hacer es el boton de mostrar para que al hacer click aparescan los datos de los alumnos en el list box ya que los puedo visualizar en txt.
Código:
public partial class Form1 : Form
{
GestorAlumnos altaalumno = new GestorAlumnos();
public Form1()
{
InitializeComponent();
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void Btn_Alta_Click(object sender, EventArgs e)
{
Alumno mialuno = new Alumno(Convert.ToInt64(txtdni.Text));
mialuno.Apellido = txtapellido.Text;
mialuno.Nombre = txtnombre.Text;
altaalumno.Alta(mialuno);
MessageBox.Show("se dio de alta"); //error de muestra el dni ya esta registrado y despues que se dio el alta
}
private void Btn_Baja_Click(object sender, EventArgs e)
{
GestorAlumnos bajaalumno = new GestorAlumnos();
bajaalumno.Baja(Convert.ToInt64(txtdni.Text));
MessageBox.Show("Se dio de baja correctamente");
txtdni.Clear();
txtnombre.Clear();
txtapellido.Clear();
}
private void Btn_Mostrar_Click(object sender, EventArgs e)
{
string ubicacion = (@"C:\Users\sebas\Desktop\C# ejercicios\Tp_Con_Nota\Tp_Con_Nota\bin\Debug\alumnos.txt"); //falta que el programa muestre
string leer = File.ReadAllText(ubicacion); //todo los datos cargados en el
//listbox
//esto no funciona
}
private void Btn_BorrarTodo_Click(object sender, EventArgs e)
{
Lista.Items.Remove(Lista.SelectedIndex);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
clase alumno:
{
public Alumno(long dni)
{
//creo un alumno, con el dni obligatoriamente
this.DNI = dni;
}
public Alumno(string linea)
{
//reconstruyo el objeto a partir de los datos levantados del archivo
string[] datos = linea.Split(',');
this.DNI = long.Parse(datos[0]);
this.Apellido = datos[1];
this.Nombre = datos[2];
}
public long DNI;
public string Nombre;
public string Apellido;
public string GenerarRegistro()
{
//genero el registro de datos que guardaré en el archivo para poder reconstruir el objeto en unfuturo
return $"{DNI},{Apellido},{Nombre}";
}
}
Código:
clase gestoralumno:
Código:
public class GestorAlumnos
{
string archivo = "alumnos.txt";
public void Alta(Alumno unAlumno)
{
TextReader leer = new StreamReader("alumnos.txt");
string linea = leer.ReadLine();
while (linea != null)
{
Alumno Alumno = new Alumno(linea);
if (unAlumno.DNI == Alumno.DNI)
{
MessageBox.Show("El dni ya esta registrado");
leer.Close();
return;
}
else
{
linea = leer.ReadLine();
}
}
leer.Close();
FileStream fs = new FileStream(archivo, FileMode.Append, FileAccess.Write);
using (StreamWriter writer = new StreamWriter(fs))
{
//obtengo el registro que guardaré en el archivo a partir del objeto recibido
writer.WriteLine(unAlumno.GenerarRegistro());
}
fs.Close();
}
public void Baja(long DNI)
{
//para dar de baja, es necesario crear un nuevo set de datos sin el dato que estamos queriendo borrar
string output = string.Empty;
FileStream fs = new FileStream(archivo, FileMode.OpenOrCreate, FileAccess.Read);
using (StreamReader reader = new StreamReader(fs))
{
string linea = reader.ReadLine();
while (linea != null)
{
Alumno unAlumno = new Alumno(linea);
if (unAlumno.DNI != DNI)
{
output += linea + Environment.NewLine;
}
linea = reader.ReadLine();
}
}
fs.Close();
fs = new FileStream(archivo, FileMode.Truncate, FileAccess.Write);
using (StreamWriter writer = new StreamWriter(fs))
{
writer.Write(output);
}
fs.Close();
}
public List<Alumno> Lista()
{
//creamos ua lista
List<Alumno> lista = new List<Alumno>();
//abrimos el archivo
FileStream fs = new FileStream(archivo, FileMode.OpenOrCreate, FileAccess.Read);
//leeemos linea por línea y, por cada linea, creamos un objeto alumno que guardaremos en la lista
using (StreamReader reader = new StreamReader(fs))
{
string linea = reader.ReadLine();
while (linea != null)
{
Alumno unAlumno = new Alumno(linea);
lista.Add(unAlumno);
linea = reader.ReadLine();
}
}
//cerramos el arcivo
fs.Close();
return lista;
}
}