Hiii
i am making a window application in C#, in which i want to compare the entered string word by word with the form heading(text property).

I mean when user enter the string, then on button click system should match each word of the string to the heading of the form.

If suppose form heading is of 20 words, and if around 12-15 words of the entered string will match with the heading of the form , then system should show that particular form.

Kindly help me in this.............:)

Recommended Answers

All 41 Replies

Try this:

string formText = this.Text;
            string input = textBox1.Text;
            int flag = 0;
            if (input.Length < formText.Length)
            {
                for (int i = 0; i < input.Length; i++)
                {
                    if (input[i] == formText[i])
                        flag = 1;
                    else
                        flag = 0;
                }

                if (flag == 1)
                {
                    // The string is equal to the form heading or the start of the form heading.
                    // do something.
                }
            }

Use regular expression pattern.

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

namespace daniweb
{
  public partial class frmRes : Form
  {
    public frmRes()
    {
      InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
      decimal discrimation = 0.75M;
      string formHeading = "The brown dog jumped over the red lazy river, and then took a little nap! Fun!";
      string userSearch = "brown dog nap fun";
      decimal res = CompareText(formHeading, userSearch);

      if (res >= discrimation)
      {
        MessageBox.Show("MATCH!");
      }
    }


    /// <summary>
    /// Returns a percentage of 1 on how many words were matched
    /// </summary>
    /// <returns></returns>
    private decimal CompareText(string formHeading, string userSearch)
    {
      string[] formHeadingWords;
      string[] userSearchWords;
      formHeadingWords = System.Text.RegularExpressions.Regex.Split(formHeading, @"\W");
      userSearchWords = System.Text.RegularExpressions.Regex.Split(userSearch, @"\W");

      int wordsFound = 0;
      for (int i1 = 0; i1 < userSearchWords.Length; i1++)
      {
        if (formHeadingWords.Contains(userSearchWords[i1], StringComparer.CurrentCultureIgnoreCase))
          wordsFound += 1;
      }

      return (Convert.ToDecimal(wordsFound) / Convert.ToDecimal(formHeadingWords.Length));
    }

Thank you for your Mr. Scott..........
But it is not working properly...
I want that when entered string in textBox will match to the different-different form's headings, so system should show that particular form....which text property match with the entered string...................

Can you please help me in this...............:)

Yes I think I spoke to one of your classmates. Please review this 7 page thread regarding the topic:
http://www.daniweb.com/forums/thread209864.html

[edit]
By the way you have a much better approach on scoring the matches. He wound up giving up and doing "exact matches" on text which is just about pointless in my opinion.
[/edit]

well thanks for your reply Mr. Scott
But he is not my classmate or friend by the way
i already see that thread before
but that is not my need, that is just useless solution for me.
thats exact match.
i want that if more Around half words(not less than half) of the string(not more than half) will matches of the heading of the form then only it will show the related forms.
not show only one form....it can show many forms which satisfied this condition(around half words matches)......
Now if you want so kindly reply me..............:)

Well entered string could match more than half but it can not less than half...
in that case it should give error, that entered string not found................

Will you please help me Mr. Scott? :)

>> but that is not my need, that is just useless solution for me. thats exact match. i want that if more Around half words(not less than half) of the string(not more than half) will matches of the heading of the form then only it will show the related forms.

I'm not at my desk right now but in one of the examples is a FormSearcher.cs class. You can see in there where the code calls if someText.StartsWith("someOtherText") then return true. Just gut the existing string matching logic, put your own in, and away you go!

>> not show only one form....it can show many forms which satisfied this condition(around half words matches)......

Actually the first project I did for him and uploaded was capable of showing multiple forms but he later clarified that he only wanted a single form. Download all of the projects on the thread and you will find an example where that is done. They're listed in a Listbox and you can double click and it will instantiate the form.

commented: Well said. +13

Thank you so much Mr. Scott for your suggestion.........
I will download all the projects and try to get my solution out of those.

But if i'll find any problem, then i let you know..........:)

thanks for your reply Mr.Scott
I have downloaded all that stuff .....
But I could not understand that clearly,its little confusing.........
Can u plz help me out in my problem...........
My problem is that I just want that the characters in the string matches at least 50%-60% or atmost 100%,ignoring spelling mistake or extra spacing between words, but it should display the related form if string matches at least 50-60%.

Kindly help me out.........

Sure. Upload your project and I will take a look at it.

My project is attached here.....
I have tried the code given by you........
But its not helpful....
I just want that 50-60% characters of the string matches ignoring the white spaces and display the related form if it matches.

Ok let me ask you a few more question -- You want to compare the composition of each WORD right, not the entire sentence? Doing it by the entire sentence is probably meaningless.

Also -- Do you want to search the form's title only, or do you want to search child controls too?

Yes i want to compare the composition of each WORD.
Also i want to compare only the title not the child controls.

Give this a shot

commented: Great! +13

Thank You Mr. Scott......
But it is not giving any output, it is giving a runtime error in this line...

under this function:
 public bool CompareString(string SearchText, string FormText)
    {
 if (string.IsNullOrEmpty(SearchText) && string.IsNullOrEmpty(FormText))

Kindly help me in this

What is the error..........? What are the user input values? have you modified the captions? It works fine on my end

On executing the code , when i m entering the string and clicking on search button, its giving nothing.
it just refering to the line :

if (string.IsNullOrEmpty(SearchText) && string.IsNullOrEmpty(FormText))

In FormSearcher class.

And i hav'nt changed anything

I don't understand the problem. Thats saying if BOTH strings are empty....

This error occurs when I am entering the string in the textbox......
And when I have entered the string similar to the text then also it is giving nothing..........
Can't u please help me in resolving this error.....

Attach your developed application and provide us with test case, I don't think this problem needs all those replies except you all missing something and it's not so clear.

Thanks a lot for helping me......
My project is attached here.......
In this, when I executed the project,there in textbox I entered the string which is text of any of the forms made by me.
Like I entered the string "How to compare string " which is the text of Form2. But on clicking search button,its giving nothing .
Kindly solve my problem.........

I have a break point on the line you were talking about. If you look in the designer to the left of the line you will see a red dot -- click on the red dot and it will stop breaking. I also entered the term "How to compare string" in the text box next to the search button and it returned 2 results -- please see the attached screen shot.

Thanks a lot for helping me......
My project is attached here.......
In this, when I executed the project,there in textbox I entered the string which is text of any of the forms made by me.
Like I entered the string "How to compare string " which is the text of Form2. But on clicking search button,its giving nothing .
Kindly solve my problem.........

I got it!!! that's appear on your computer or not?

No, its not appear on my computer.........

Do you have a non-english language set up on your computer? Perhaps this is some kind of encoding issue?

no,i do not have non-english language.........

What happens when you enter the search text then? Does the code window pop up at the one line you mentioned earlier? If so click the "Run" button a few times until you see results

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.