Click here to Skip to main content
16,021,112 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Back to basics!

I just started digging deeper into arrays and it is amazing what you can do with it (I wont have to tell you am I right?)


What I am trying to accomplish is a simple software that will compare the 10 lines of text in my Text Document with the users input in a textBox in the software.

What has been working and what has not been working so far.


What has been working.

I can make the software fully read from the textfile and display it onto a richTextBox in visual studio.



What has not been working.

I can not seem to make the users input compare to the text in the textfiles.


C#
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:\Users\Developer\Documents\Visual Studio 2015\Projects\Arrays\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 == displayArraysString)
                {
                    MessageBox.Show("Found a match!");
                }
            }
         }
    }
}


As you can see, the users input is matching up with the first one in the Textbox Text

<pre lang="text">https://i.gyazo.com/d9e8ed4d6845f6c67a45bbb203d45a22.png Here is a link to a image





NEW LOOP



C#
//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
        {
         //Im not sure what to put here, I want it to loop to the next one and see if that one amtches
//I tried using "continue;" but that didnt work. I also tried a try catch loop but couldnt get that to work either.
        }


What I have tried:

I've tried changing thigs up by trying to convert it to a string see if that changed anything but it didnt.
Posted
Updated 10-May-16 1:20am
v2
Comments
W Balboos, GHB 10-May-16 6:38am    
if(listText == displayArraysString)

Aren't you comparing pointers and not their values ?
BladeLogan 10-May-16 6:39am    
I thought I was comparing thier values.
W Balboos, GHB 10-May-16 6:44am    
OK, see this (from your code):
<pre>
displayArraysString.Text += text[o].ToString();

if(listText == displayArraysString)
</pre>

Notice how you put the value into the .Text component of the object and then do the comparison to the object without the .Text.
BladeLogan 10-May-16 7:07am    
I think I know what you mean but how do I make is so I compare what the user puts into the listText to the displayArrayString?

1 solution

With this code...

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


you are asking "Is the listText control the same control as displayArraysString" and that answer to that is "false" and always will be as they refer to different instances on textboxes.

This code...

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


is asking "Is the text in listText the same as the text in displayArraysString" which is what you really want to know.
 
Share this answer
 
Comments
BladeLogan 10-May-16 7:10am    
Ooh! I wasnt refering to the "value" which in this case was shown by ".Text" sorry for my non programatical terms, but I see the difference!

Also, as long as its ".Texr" it can read anything right ? even special characters like "?" and "@" right? Since Int can only store numbers and string can only store characters like "a,b,c" and text can store anything from "a - 9 - @" right?
F-ES Sitecore 10-May-16 7:16am    
Yes Text can store anything, but remember that even if the data "looks like" another type it is still a string, so if you have "123" then that isn't the number one hundred and twenty three, it is the ascii characters "1", "2" and "3" one after each other. Likewise if it has "1/1/20016" then that isn't seen as a date, it is still just a string.
BladeLogan 10-May-16 7:18am    
Is there anyway to bypass that? What would the correct term be for getting passeds something like that so I can read up about it!

Also, it does work now but I need to make it loop through because it only detects if its the first one, if the user were to input another line maybe line 4 it wouldnt recognize it because its not looping through..

This is what my loop looks like right now il update the question.
F-ES Sitecore 10-May-16 9:19am    
Restricting what a user can enter into a texbox is called an "input mask".

As for the rest, I don't really understand as we don't know what your data inputs are, what is in your text file, etc etc. If we don't know what data you're working with it's hard to visualise what you're trying to do.
BladeLogan 10-May-16 9:49am    
Its actually alright, I had a friend who helped me write this new line of code, It can now detect any of the lines, hard to explain but il create a new question soon because there were some stuff I didnt understand, going to try to read up about it first. Will be back soon

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900