I'm making a simple BankingApp that keeps customers and their accounts in two separate list-boxes. When Selecting one customer in a list-box related accounts will be shown in the second list-box and the user should be able to add,remove, update customer and accounts. Whenever I try to add, remove or update a customer to the List-box it keeps locking to just one Item in the customer List-Box. The app doesn't crash just freezes on that certain item. But I'm still able to choose another customer and see its information even if its already locked on another customer item.?? Any Ideas of what I am doing wrong in my code?
My butto for deleting customers: private void buttonTaBort_Click(object sender, EventArgs e) is not working and my button: private void buttonOkUppdatera_Click(object sender, EventArgs e) for updating customers is not working?
I know my code is a bit messy, I just started learning a couple of weeks ago :)Trying my best to get the hang of things.
[Serializable]
public partial class BankenAppen : Form
{
List<Kund> KundLista = new List<Kund>();
Kund aktuellKund = new Kund();
Konto aktuelltKonto = new Konto();
List<Konto> KontoLista = new List<Konto>();
public BankenAppen()
{
InitializeComponent();
FileStream fs = new FileStream("BankenAppenLista.bin", FileMode.OpenOrCreate, FileAccess.Read);
BinaryFormatter bf = new BinaryFormatter();
try
{
KundLista = (List<Kund>)bf.Deserialize(fs);
}
catch
{
MessageBox.Show("Tomt");
}
fs.Close();
fs.Dispose();
textBoxKontoNr.Enabled = false;
textBoxSaldo.Enabled = false;
textBoxRänta.Enabled = false;
listBoxKunder.DataSource = KundLista;
}
public void EnableMetod()
{
textBoxFörNamn.Enabled = true;
textBoxEfternamn.Enabled = true;
textBoxPersonnummer.Enabled = true;
textBoxGatuAdress.Enabled = true;
textBoxTelefon.Enabled = true;
textBoxMobil.Enabled = true;
}
public void DisableMetod()
{
textBoxFörNamn.Enabled = false;
textBoxEfternamn.Enabled = false;
textBoxPersonnummer.Enabled = false;
textBoxGatuAdress.Enabled = false;
textBoxTelefon.Enabled = false;
textBoxMobil.Enabled = false;
}
public void ClearMetod()
{
textBoxFörNamn.Clear();
textBoxEfternamn.Clear();
textBoxPersonnummer.Clear();
textBoxGatuAdress.Clear();
textBoxTelefon.Clear();
textBoxMobil.Clear();
}
public void SparaMetod()
{
FileStream fs = new FileStream("BankenAppenLista.bin", FileMode.OpenOrCreate, FileAccess.Write);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, KundLista);
fs.Close();
}
private void Kunderna()
{
listBoxKunder.DataSource = null;
listBoxKunder.Items.Clear();
foreach (Kund kunder in KundLista)
{
listBoxKunder.Items.Add(kunder);
}
}
private void Konton()
{
listBoxKonto.Items.Clear();
foreach (Konto konton in aktuellKund.kontolista)
{
listBoxKonto.Items.Add(konton);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void buttonOkLäggTill_Click(object sender, EventArgs e)
{
listBoxKunder.DataSource = null;
Kund Kunder = new Kund
{
Förnamn = textBoxFörNamn.Text,
Efternamn = textBoxEfternamn.Text,
Personnummer = textBoxPersonnummer.Text,
GatuAdress = textBoxGatuAdress.Text,
Telefon = textBoxTelefon.Text,
Mobil = textBoxMobil.Text
};
KundLista.Add(Kunder);
listBoxKunder.Items.Add(Kunder);
listBoxKunder.DataSource = KundLista;
SparaMetod();
DisableMetod();
ClearMetod();
}
private void buttonLäggTill_Click(object sender, EventArgs e)
{
EnableMetod();
ClearMetod();
}
private void listBoxKunder_SelectedIndexChanged(object sender, EventArgs e)
{
aktuellKund = (Kund)listBoxKunder.SelectedItem;
listBoxKunder.DataSource = KundLista;
listBoxKonto.Items.Clear();
foreach (Konto item in aktuellKund.kontolista)
{
listBoxKonto.Items.Add(item);
}
if(aktuellKund!= null)
{
textBoxFörNamn.Text = aktuellKund.Förnamn;
textBoxEfternamn.Text = aktuellKund.Efternamn;
textBoxPersonnummer.Text = aktuellKund.Personnummer;
textBoxGatuAdress.Text = aktuellKund.GatuAdress;
textBoxTelefon.Text = aktuellKund.Telefon;
textBoxMobil.Text = aktuellKund.Mobil;
}
}
private void buttonTaBort_Click(object sender, EventArgs e)
{
listBoxKunder.DataSource = null;
listBoxKunder.Items.Clear();
KundLista.Remove(aktuellKund);
SparaMetod();
Kunderna();
ClearMetod();
listBoxKunder.DataSource = KundLista;
}
private void buttonUppdatera_Click(object sender, EventArgs e)
{
EnableMetod();
textBoxPersonnummer.Enabled = false;
}
private void buttonOkUppdatera_Click(object sender, EventArgs e)
{
aktuellKund.BytaAdress(textBoxFörNamn.Text, textBoxEfternamn.Text, textBoxGatuAdress.Text, textBoxTelefon.Text, textBoxMobil.Text);
SparaMetod();
Kunderna();
DisableMetod();
}
private void buttonLäggTillKonto_Click(object sender, EventArgs e)
{
textBoxKontoNr.Enabled = true;
textBoxSaldo.Enabled = true;
textBoxRänta.Enabled = true;
textBoxKontoNr.Clear();
textBoxSaldo.Clear();
textBoxRänta.Clear();
}
private void buttonOkÖppnaKonto_Click(object sender, EventArgs e)
{
listBoxKonto.DataSource = null;
if (String.IsNullOrEmpty(textBoxKontoNr.Text) && String.IsNullOrEmpty(textBoxSaldo.Text) && String.IsNullOrEmpty(textBoxRänta.Text))
{
MessageBox.Show("Fyll i Kontouppgifter Tack!");
}
else
{
Konto nyttkonto;
if (radioButtonPrivat.Checked)
{
Privat Pkonto = new Privat
{
KontoNummer = textBoxKontoNr.Text,
Saldo = int.Parse(textBoxSaldo.Text),
Ränta = double.Parse(textBoxRänta.Text)
};
nyttkonto = Pkonto;
textBoxKontoNr.Text = "";
textBoxSaldo.Text = "";
textBoxRänta.Text = "";
}
else if (radioButtonFramtid.Checked)
{
Framtid Fkonto = new Framtid
{
KontoNummer = textBoxKontoNr.Text,
Saldo = int.Parse(textBoxSaldo.Text),
Ränta = double.Parse(textBoxRänta.Text)
};
nyttkonto = Fkonto;
}
else
{
Service Skonto = new Service
{
KontoNummer = textBoxKontoNr.Text,
Saldo = int.Parse(textBoxSaldo.Text),
Ränta = double.Parse(textBoxRänta.Text)
};
nyttkonto = Skonto;
}
aktuellKund.ÖppnaKonto(nyttkonto);
SparaMetod();
Konton();
textBoxKontoNr.Enabled = false;
textBoxSaldo.Enabled = false;
textBoxRänta.Enabled = false;
}
}
private void listBoxKonto_SelectedIndexChanged(object sender, EventArgs e)
{
aktuelltKonto = (Konto)listBoxKonto.SelectedItem;
if (aktuelltKonto != null)
{
textBoxKontoNr.Text = aktuelltKonto.KontoNummer;
textBoxSaldo.Text = aktuelltKonto.Saldo.ToString();
textBoxRänta.Text = aktuelltKonto.Ränta.ToString();
}
}
private void buttonInUt_Click(object sender, EventArgs e)
{
int insättning = int.Parse(textBoxInsättning.Text);
aktuelltKonto.Saldo = aktuelltKonto.Saldo + insättning;
aktuelltKonto.Insättning(aktuelltKonto.Saldo);
textBoxKontoNr.Text = aktuelltKonto.KontoNummer;
textBoxSaldo.Text = aktuelltKonto.Saldo.ToString();
textBoxRänta.Text = aktuelltKonto.Ränta.ToString();
SparaMetod();
textBoxInsättning.Clear();
}
private void buttonUttag_Click(object sender, EventArgs e)
{
int uttag = int.Parse(textBoxUttag.Text);
aktuelltKonto.Saldo = aktuelltKonto.Saldo - uttag;
aktuelltKonto.Insättning(aktuelltKonto.Saldo);
textBoxKontoNr.Text = aktuelltKonto.KontoNummer;
textBoxSaldo.Text = aktuelltKonto.Saldo.ToString();
textBoxRänta.Text = aktuelltKonto.Ränta.ToString();
SparaMetod();
textBoxUttag.Clear();
}
private void buttonAvslutaKonto_Click(object sender, EventArgs e)
{
//listBoxKonto.DataSource = null;
aktuelltKonto = (Konto)listBoxKonto.SelectedItem;
KontoLista.Remove(aktuelltKonto);
// listBoxKonto.DataSource = KontoLista;
SparaMetod();
Konton();
textBoxKontoNr.Clear();
textBoxRänta.Clear();
textBoxSaldo.Clear();
}
}
And my Customer Class
[Serializable]
class Kund
{
//Properties
public string Förnamn { get; set; }
public string Efternamn { get; set; }
public string Personnummer { get; set; }
public string GatuAdress { get; set; }
public string PostAdress { get; set; }
public string Telefon { get; set; }
public string Mobil { get; set; }
public List<Konto> kontolista
{
get; set;
}
public Kund()
{
kontolista = new List<Konto>();
}
// Metoder
public void BytaAdress(string nyttFörnamn, string nyttEfternamn, string nyGatuAdress, string nyTelefon, string nyMobil)
{
Förnamn = nyttFörnamn;
Efternamn = nyttEfternamn;
GatuAdress = nyGatuAdress;
Telefon = nyTelefon;
Mobil = nyMobil;
}
public void ÖppnaKonto(Konto nyttKonto)
{
kontolista.Add(nyttKonto);
}
public override string ToString()
{
return Förnamn + " " + Efternamn;
}
}