I'm trying to create an application that searches for names in a listbox. Basically, I have 2 .txt files (BoyNames, GirlNames) one file contains a set of boy names and the other girl names. Ive managed to display the boy names to boyNameListBox and girl names to girlNameListBox. please refer to the image ive attached. I'm trying to add the function where if the user types a boys name (in the boy textbox) and the name is lised in the list box, the app will return a messagebox showing "popular"; if the name is not listed, the app will show a messagebox showing not popular. I wish to include the same search function but for girl names. I'm very new to programming and your help will be very much appreciated. Thanks in advance!!
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void readButton_Click(object sender, EventArgs e)
{
{
//local variables
string line1;
//Catch Boy Names File
System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\Harra\Documents\Visual Studio 2017\Projects\Name Search\BoyNames.txt");
//display items BoyNames file to Listbox
while ((line1 = file.ReadLine()) != null)
boyNameListbox.Items.Add(line1);
}
{
//local variales
string line2;
//Catch Girl Names File
System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\Harra\Documents\Visual Studio 2017\Projects\Name Search\GirlNames.txt");
//display items GirlNames file to Listbox
while ((line2 = file.ReadLine()) != null)
girlNameListbox.Items.Add(line2);
}
}
private void boyButton_Click(object sender, EventArgs e)
{
}
private void girlButton_Click(object sender, EventArgs e)
{
}
}
Something like:
private void boyButton_Click(object sender, EventArgs e)
{
string boyname = boyTextBox.Text;
bool found = false;
for(int n = 0; n < boyNameListbox.Items.Count; n++)
{
if (boyNameListbox.Items[n] == boyname)
{
found = true;
break;
}
}
if (found)
MessageBox.Show("popular");
else
MessageBox.Show("not popular");
}
Mind you, I didn't code the whole form, so maybe small errors but hope you get the idea from this example. Hopefully this is close enough to get you started and be the accepted answer.