Two Country Lists

kiwis

Member
Joined
Jan 26, 2025
Messages
13
Programming Experience
Beginner
I have a WinForms project with a bunch of fields. I'm loading a profile from my database into a Person object and then loading all Persons into a LIST of persons followed by setting a list box source to that list of persons. That's now working as expected!

I have had to add two countries onto my form, using ComboBox's. I've created a small Country class with CountryName and CoundeCode properties. On form load I'm creating a countryList variable, it's a LIST of Country and loading in all my applicable countries into it.

When I load my Person I have country and countryAlt being pulled from my database. Sometimes they are different sometimes they are the same.

When I do a listBox1_SelectedIndexChanged, I want to load the Person selected. However I seem to loop though it twice and get the same country in both ComboBoxes even if the values are different. If I load the CountryName into a textbox like this

C#:
TextBox1.Text += cCountry.CountryName.ToString();

I get four countries, two of each...

What is wrong?

C#:
    Country cCountry = countryList.Find(i => i.CountryCode == thisPerson.Country.ToUpper().ToString());
    countryList_box.Text = cCountry.CountryName.ToString();

    Country aCountry = countryList.Find(i => i.CountryCode == thisPerson.CountryAlt.ToUpper().ToString());
    altCountryList_box.Text = aCountry.CountryName.ToString();
 
If I load the CountryName into a textbox like this

C#:
TextBox1.Text += cCountry.CountryName.ToString();
I get four countries, two of each...

If that code is in a loop, then it's quite possible to get 4 countries depending on how cCountry is set within the loop.

If that code is not in a loop, then that would mean that your cCountry.CountryName name has the 4 country names set in the CountryName property of a single cCountry object.

I suggest stepping through your code in the debugger. Use the "Locals" and "Automatic" view of the debugger to inspect the values in your countryList list. Perhaps is simply bad data within the list.
 
However I seem to loop though it twice and get the same country in both ComboBoxes even if the values are different.

:
What is wrong?

C#:
    Country cCountry = countryList.Find(i => i.CountryCode == thisPerson.Country.ToUpper().ToString());
    countryList_box.Text = cCountry.CountryName.ToString();

    Country aCountry = countryList.Find(i => i.CountryCode == thisPerson.CountryAlt.ToUpper().ToString());
    altCountryList_box.Text = aCountry.CountryName.ToString();

Your code looks correct. This maybe a data issue.

Are you sure that thisPerson.Country is not the same as thisPerson.CountryAlt?
Are you sure that you don't have multiple entries in the countryList pointing to the same County object instance? It's a common rookie mistake to re-use the same instance while loading from a database.

Again, stepping through your code with a debugger and inspecting values will give you valuable insight into what may going wrong.
 
I've checked my CountryList. There is definitely only one record per country I need. There is two issues here

1. The code above is firing twice when I change what I select in the ListBox
2. The first country is being overridden by the second. For example is I have South African and Singapore, I get Singapore in both.


Is there a way to debug without drilling into list.cs code etc
 
You don't need to drill all the way into list.cs. Just set breakpoints in your code.
 
As for the first code being called twice (or more), did you take some bad advice (possibly from a post here made by someone whose name starts with an R and ends with an N), which included code that use Application.DoEvents()? That can cause weird re-entrancy issues.
 
As for the first code being called twice (or more), did you take some bad advice (possibly from a post here made by someone whose name starts with an R and ends with an N), which included code that use Application.DoEvents()? That can cause weird re-entrancy issues.

Nope.
 
It does automatically, can I stop that somehow?

"Enable Just My Code"
1739482390863.png
 
Back
Top Bottom