0

I got a list of 10 items.

when a user puts in a value in "listText" its comparing to the first item in displayArraysString.

Lets say its not the first item in the displayArraysString list, then it doesnt do anything (Because I dont have a loop)

How do I create a loop that will check through my list and display the messagebox once it finds it. I tried with a try catch loop but that didnt work for me.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Arrays
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int[] numbers = new int[5];
        List<int> numbersList = new List<int> ();
        string text = System.IO.File.ReadAllText(@"C:Directory\list.txt");

        private void Form1_Load(object sender, EventArgs e)
        {
            //numbers[0] = 12;
            //numbers[1] = 10;
            //numbers[2] = 25;
            //numbers[3] = 10;
            //numbers[4] = 15;
            //numbersList.Add(23);
            //numbersList.Add(32);
            //numbersList.Add(35);
        }
        //Array Print
        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < numbers.Length; i++)
            displayArrays.Text += numbers[i].ToString() + ", ";
        }
        //List Print
        private void button2_Click(object sender, EventArgs e)
         {
            for (int o = 0; o < text.Length; o++)
            {
                displayArraysString.Text += text[o].ToString();

                if (listText.Text == displayArraysString.Text)
                {
                    MessageBox.Show("Found a match!");

                }
                else
                {
                   //Something.
                }




            }
         }
    }
}

enter image description here

enter image description here

3
  • Whats wrong with your button2 click method? in what way isnt it working, have you tried debugging it? Commented May 10, 2016 at 11:33
  • what is the type of "displayArrays" and "displayArraysString" Commented May 10, 2016 at 11:39
  • My bad people! It does work thats the thing but its only looking for the first one in the list if the textfile. let me demo! going to update my question Commented May 10, 2016 at 11:41

1 Answer 1

1

You are trying something wrong here, as the file you have read from the path in string , it will match a single character for listText, so there will be never a match, I did it with string array, to convert the text data into string array of every words in it. If you search now than match will be found for listText.

try this code:

     namespace Arrays
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int[] numbers = new int[5];
        List<int> numbersList = new List<int> ();
        string text = System.IO.File.ReadAllText.Text(@"C:\Directory\list.txt");
        string[] displayStringArrays = null; 

        private void Form1_Load(object sender, EventArgs e)
        {
            //numbers[0] = 12;
            //numbers[1] = 10;
            //numbers[2] = 25;
            //numbers[3] = 10;
            //numbers[4] = 15;
            //numbersList.Add(23);
            //numbersList.Add(32);
            //numbersList.Add(35);
        }
        //Array Print
        private void button1_Click(object sender, EventArgs e)
        {
            displayArrays.Text = listText.Text;
        }
        //List Print
        private void button2_Click(object sender, EventArgs e)
         {
            displayStringArrays = text.Split('\n').ToArray();
            foreach (var item in displayStringArrays)
            {
                displayArraysString.Text += item;

                 if (listText.Text == item.Substring(0, item.Length - 1) || listText.Text == item)
                {
                     MessageBox.Show("Found a match!");
                }
                else
                {
                    //Something.
                }
            }

         }
    }
}

replace this code with your code. I checked this it is working fine now.

Sign up to request clarification or add additional context in comments.

11 Comments

As you can see in my gif it is working I just need to loop it through the other 9 items in my Text Document
It is not working as you see, its reading a single character from the file and displaying it in the text box, but you are comparing it with the whole word which never be a match try my code, it will work.
if that not work replace Split(' ') with Split('\n') then it will work.
That code doesnt even match the first one now, dafuq.
And 1 more thing its displaying a match found message in your case for the first one because, for the first line when it gets printed in the text box it covers the whole word exactly then it shows the message. but afterwards the word exceeded with other character and never will be a macth.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.