C
C#12mo ago
olleeee

✅ List doesnt show multiple inputs

Hi i have this code, and when looping throught all the houses and the second member of each house is put in you can only see one member still in the list, why?
16 Replies
olleeee
olleeeeOP12mo ago
private void btnSortingHat_Click(object sender, RoutedEventArgs e)
{
Wizard newWizard = new ();
Wizard student = CreateNewWizard(newWizard);
txtName.Text = student.Name;
if (student.DeathEater == true)
{
chkDeatheater.IsChecked = true;
}
if (student.DumbledoorsArmy == true)
{
chkArmy.IsChecked = true;
}


House currentHouse = hogwarts.Houses[currentHouseIndex];

currentHouse.SortingHat(newWizard, currentHouse);
MessageBox.Show($"{newWizard.Name} is now member {currentHouse.Members.Count} in {currentHouse}\n" +
$"{currentHouse.HouseGhost} is going to take well care of you.");
FillLists(student);

currentHouseIndex++;
if (currentHouseIndex > 3)
{
currentHouseIndex = 0;
}
}


public void FillLists(Wizard wizard)
{
if (currentHouseIndex == 0)
{
lstGryffindor.ItemsSource = hogwarts.Griffendor.Members;
}
if (currentHouseIndex == 1)
{
lstRavenclaw.ItemsSource = hogwarts.Ravenclaw.Members;
}
if (currentHouseIndex == 2)
{
lstSlytherin.ItemsSource = hogwarts.Slytherin.Members;

}
if (currentHouseIndex == 3)
{
lstHufflepuff.ItemsSource = hogwarts.Hufflepuff.Members;

}
}
private void btnSortingHat_Click(object sender, RoutedEventArgs e)
{
Wizard newWizard = new ();
Wizard student = CreateNewWizard(newWizard);
txtName.Text = student.Name;
if (student.DeathEater == true)
{
chkDeatheater.IsChecked = true;
}
if (student.DumbledoorsArmy == true)
{
chkArmy.IsChecked = true;
}


House currentHouse = hogwarts.Houses[currentHouseIndex];

currentHouse.SortingHat(newWizard, currentHouse);
MessageBox.Show($"{newWizard.Name} is now member {currentHouse.Members.Count} in {currentHouse}\n" +
$"{currentHouse.HouseGhost} is going to take well care of you.");
FillLists(student);

currentHouseIndex++;
if (currentHouseIndex > 3)
{
currentHouseIndex = 0;
}
}


public void FillLists(Wizard wizard)
{
if (currentHouseIndex == 0)
{
lstGryffindor.ItemsSource = hogwarts.Griffendor.Members;
}
if (currentHouseIndex == 1)
{
lstRavenclaw.ItemsSource = hogwarts.Ravenclaw.Members;
}
if (currentHouseIndex == 2)
{
lstSlytherin.ItemsSource = hogwarts.Slytherin.Members;

}
if (currentHouseIndex == 3)
{
lstHufflepuff.ItemsSource = hogwarts.Hufflepuff.Members;

}
}
so in the list its supposed to be 2 "students" but only one can be seen in the list??
Pobiega
Pobiega12mo ago
currentHouse.SortingHat(newWizard, currentHouse); seems to be where you're adding the new wizard to a house yet that code was not included in your paste so how are we supposed to be able to help?
olleeee
olleeeeOP12mo ago
oh im sorry completely forgot!
private void btnSortingHat_Click(object sender, RoutedEventArgs e)
{
Wizard newWizard = new ();
Wizard student = CreateNewWizard(newWizard);
txtName.Text = student.Name;
if (student.DeathEater == true)
{
chkDeatheater.IsChecked = true;
}
if (student.DumbledoorsArmy == true)
{
chkArmy.IsChecked = true;
}


House currentHouse = hogwarts.Houses[currentHouseIndex];

currentHouse.SortingHat(newWizard, currentHouse);
MessageBox.Show($"{newWizard.Name} is now member {currentHouse.Members.Count} in {currentHouse}\n" +
$"{currentHouse.HouseGhost} is going to take well care of you.");
FillLists(student);

currentHouseIndex++;
if (currentHouseIndex > 3)
{
currentHouseIndex = 0;
}
}
private void btnSortingHat_Click(object sender, RoutedEventArgs e)
{
Wizard newWizard = new ();
Wizard student = CreateNewWizard(newWizard);
txtName.Text = student.Name;
if (student.DeathEater == true)
{
chkDeatheater.IsChecked = true;
}
if (student.DumbledoorsArmy == true)
{
chkArmy.IsChecked = true;
}


House currentHouse = hogwarts.Houses[currentHouseIndex];

currentHouse.SortingHat(newWizard, currentHouse);
MessageBox.Show($"{newWizard.Name} is now member {currentHouse.Members.Count} in {currentHouse}\n" +
$"{currentHouse.HouseGhost} is going to take well care of you.");
FillLists(student);

currentHouseIndex++;
if (currentHouseIndex > 3)
{
currentHouseIndex = 0;
}
}
Pobiega
Pobiega12mo ago
thats the same code you just pasted :p also, you have some very confusing code here
Wizard newWizard = new ();
Wizard student = CreateNewWizard(newWizard);
Wizard newWizard = new ();
Wizard student = CreateNewWizard(newWizard);
seems convoluted, why not just make it
Wizard newWizard = CreateNewWizard();
Wizard newWizard = CreateNewWizard();
olleeee
olleeeeOP12mo ago
sorry here is he hat!
public void SortingHat(Wizard wizard, House currentHouse)
{
currentHouse.Members.Add(wizard);



}
public void SortingHat(Wizard wizard, House currentHouse)
{
currentHouse.Members.Add(wizard);



}
Pobiega
Pobiega12mo ago
uh.. wait thats a method ON A HOUSE that takes a house as a parameter now that is both silly and confusing currentHouse.SortingHat(newWizard, currentHouse); note how currentHouse is specified twice here and the name is also confusing, that method only does one thing: register a wizard as a member of a house
olleeee
olleeeeOP12mo ago
i know but i have to use the name thats specified in the assigment.
Pobiega
Pobiega12mo ago
okay leave the name, but fix the rest of the issues I semi-rewrote this program as a console app, looks a bit like this
var houseIndex = 0;

for (var i = 0; i < 15; i++)
{
var wizard = CreateNewWizard();

Console.WriteLine($"New wizard: {wizard.Name}");
if (wizard.DeathEater)
{
Console.WriteLine($"{wizard.Name} is a death eater.");
}

if (wizard.DumbledoorsArmy)
{
Console.WriteLine($"{wizard.Name} is part of Dumbledoors Army.");
}

var house = _hogwarts.Houses[houseIndex];
houseIndex = (houseIndex + 1) % _hogwarts.Houses.Length;

house.SortingHat(wizard);
Console.WriteLine($"{wizard.Name} is now member {house.Members.Count} of {house.Name}!");
}
var houseIndex = 0;

for (var i = 0; i < 15; i++)
{
var wizard = CreateNewWizard();

Console.WriteLine($"New wizard: {wizard.Name}");
if (wizard.DeathEater)
{
Console.WriteLine($"{wizard.Name} is a death eater.");
}

if (wizard.DumbledoorsArmy)
{
Console.WriteLine($"{wizard.Name} is part of Dumbledoors Army.");
}

var house = _hogwarts.Houses[houseIndex];
houseIndex = (houseIndex + 1) % _hogwarts.Houses.Length;

house.SortingHat(wizard);
Console.WriteLine($"{wizard.Name} is now member {house.Members.Count} of {house.Name}!");
}
New wizard: Brice Mante
Brice Mante is now member 1 of Gryffindor!
New wizard: Gia Borer
Gia Borer is now member 1 of Slytherin!
New wizard: Lempi Torp
Lempi Torp is a death eater.
Lempi Torp is now member 1 of Hufflepuff!
New wizard: Alfonzo Bashirian
Alfonzo Bashirian is now member 1 of Ravenclaw!
New wizard: Camden Brown
Camden Brown is now member 2 of Gryffindor!
New wizard: Thad Runte
Thad Runte is now member 2 of Slytherin!
New wizard: Kian Stokes
Kian Stokes is now member 2 of Hufflepuff!
New wizard: Thelma Osinski
Thelma Osinski is part of Dumbledoors Army.
Thelma Osinski is now member 2 of Ravenclaw!
New wizard: Brice Mante
Brice Mante is now member 1 of Gryffindor!
New wizard: Gia Borer
Gia Borer is now member 1 of Slytherin!
New wizard: Lempi Torp
Lempi Torp is a death eater.
Lempi Torp is now member 1 of Hufflepuff!
New wizard: Alfonzo Bashirian
Alfonzo Bashirian is now member 1 of Ravenclaw!
New wizard: Camden Brown
Camden Brown is now member 2 of Gryffindor!
New wizard: Thad Runte
Thad Runte is now member 2 of Slytherin!
New wizard: Kian Stokes
Kian Stokes is now member 2 of Hufflepuff!
New wizard: Thelma Osinski
Thelma Osinski is part of Dumbledoors Army.
Thelma Osinski is now member 2 of Ravenclaw!
olleeee
olleeeeOP12mo ago
oh we not allowed to co console thoug so i have that they get added and says that they member two and so, but they just dont pop up in my list box.
Pobiega
Pobiega12mo ago
you have 4 list boxes as far as I can tell
olleeee
olleeeeOP12mo ago
yes
olleeee
olleeeeOP12mo ago
so i can add the first time and everything works fine, but then when i try to add the second member to the list it doesnt work..
No description
olleeee
olleeeeOP12mo ago
its just adding more elements to the listbox it looks like it overrites its self
Pobiega
Pobiega12mo ago
Ah, I see why you "update" the list by assigning its itemsource but if the itemsource is the same as it previously was, there is no reason to re-render even when the itemsource is assigned again and thats exactly whats happening here you'll need to call .Refresh() on your updated listview
olleeee
olleeeeOP12mo ago
oh i have never seen refresh before but will give it a go! where and how im a supposed t refresh? i have got it now! just put the itemssource to null first thank you for all the input!
Stringy
Stringy12mo ago
You don’t need to keep setting the ItemsSource each time you add a new wizard to your members list. Simply set it once when initialise the window / components. Secondly if you use an ObservableCollection<T> instead of a List<T>, you will get built in INotifyPropertyChange which means you’ll get two way data binding and the list view will “automatically” update when you add a member to that ObservableCollection. If you don’t use an ObservableCollection, you would need to implement INotifyPropertyChanged
Want results from more Discord servers?
Add your server