-2

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)
    {

    }
}

enter image description here

MethodMan
  • 18,625
  • 6
  • 34
  • 52
Relaxsingh
  • 11
  • 5
  • do a simple google search.. also edit your code and format it properly you have `{ }` used improperly ..this is not difficult – MethodMan May 25 '17 at 16:42
  • Thanks for your response, yes ive tried to google search but because of my limited level of skill I'm unable to understand. Will you be able to help? – Relaxsingh May 25 '17 at 16:46
  • take time out to google some `C# Basics Tutorials for beginners` is where I would start.. plenty of free online resources / tutorials / videos – MethodMan May 25 '17 at 18:07

1 Answers1

0

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.

TomServo
  • 7,248
  • 5
  • 30
  • 47
  • Hi, I really really appreciate your response! I'm getting red squiggly lines at: (found). heres the error message: "use of unassigned local variable 'found'.". – Relaxsingh May 25 '17 at 18:21
  • Just made an edit that should correct that. Would love an upvote and accept on my answer if it helps you. ;) – TomServo May 25 '17 at 18:24
  • Hi again, Great! it works. I'm getting another error but now with green squiggly lines at: (boyNameListbox.Items[n] == boyname). after this is solved, I believe the app should work just fine. Here is the error message: "possible unintended reference comparison, to get a value comparison, cast the left hand side to type 'string'." Thanks again! – Relaxsingh May 25 '17 at 18:39
  • Yeah just use a cast or ToString(). I forgot about that. Glad to help. With a little help and a little study soon this will all seem elementary to you. – TomServo May 25 '17 at 18:42
  • How do I use a cast or ToString()?. Yeah, seems like its gonna take a long time considering my current skill level – Relaxsingh May 25 '17 at 18:54
  • Nevermind, I figured that last part out myself. Thank you very much, you have no idea how much you've healped me! – Relaxsingh May 25 '17 at 19:13