This is a problem I am having in C# Visual studio 2019, and it should be relatively simple for somebody more experienced to help me, and I appreciate all help. I'm going crazy trying to figure this out! I have two listboxes, one called boyBox and one called GirlBox. I used to populate the listboxes with the text files:
private void populateBoys()
{
//Try to access the file, if problems arise an error message will show
try
{
//Declare object to access file
StreamReader inputFile;
inputFile = File.OpenText("..\\..\\BoyNames.txt");
//Loop to run through all items in the text file
while (!inputFile.EndOfStream)
{
//read lines from the file and add to the listbox
boyBox.Items.Add(inputFile.ReadLine());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void populateGirls()
{
//Try to access the file, if problems arise an error message will show
try
{
//Declare object to access file
StreamReader inputFile;
inputFile = File.OpenText("..\\..\\GirlNames.txt");
//Loop to run through all items in the text file
while (!inputFile.EndOfStream)
{
//read lines from the file and add to the listbox
girlBox.Items.Add(inputFile.ReadLine());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
To populate the listboxes with a list of popular boy and girl names called BoyNames.txt and GirlNames.txt. This seems simple enough, but I am being driven insane trying to find something that works for me to simply type a name into a textbox and have the program tell me if the name was in the list or not. The closest I got was :
private void boyButton_Click(object sender, EventArgs e)
{
string boyname = boyBox2.Text;
bool found = false;
for(int n = 0; n < boyBox.Items.Count; n++)
{
if (boyBox.Items[n] == boyname)
{
found = true;
break;
}
}
if (found)
MessageBox.Show("popular");
else
MessageBox.Show("not popular");
}
But it just says "Not Popular" even if I typed one of the names that IS in the list into the textbox "boyBox2". The listbox is "boyBox". I'm just trying to find help to figure out what code I can use to get it to work for letting me know if what I type into the textbox "boyBox2" is a name that is in the listbox/file or not. Thank you!