Hi ,I have made an application in which I am matching a string entered in textbox with the Text property of the forms.In that , I am matching 0.75% characters of strings and the text of the result forms are displayed in listview.
But the problem is that the string which is entered in textbox and if there is any exact matching formtext for that ,then that string is not appearing at 1st place.
And I want that if the string exactly matches with the form Text ,then that form should appear at 1st place in the listview and rest other forms can appear at any place in the listview.

Also ,the characters which I have entered in the form text are without spacing and they can be combination of special characters , math symbols etc. Since, in form text I cannot enter symbols for squareroot , fraction etc . so for that I am entering terms like "sqrt", "frac" etc..So , when the text is displayed in the listview , it is not easily understandable by the user.Is there any way to display the text in proper way so that it is easily understandable by the user and user could easily search the required form.

Kindly help me,I am not able to find any solution for that.

And I want that if the string exactly matches with the form Text ,then that form should appear at 1st place in the listview and rest other forms can appear at any place in the listview.

You can first find out how much % each string(form text string ) matches with the string entered in the textbox and collect it in an array and then perform sorting on it.So that the maximum percentage matched string displays on the top......
Hope It works.

thanks For your reply, but Can anyone tell me how can i perform this........

Also ,the characters which I have entered in the form text are without spacing and they can be combination of special characters , math symbols etc. Since, in form text I cannot enter symbols for squareroot , fraction etc . so for that I am entering terms like "sqrt", "frac" etc..So , when the text is displayed in the listview , it is not easily understandable by the user.Is there any way to display the text in proper way so that it is easily understandable by the user and user could easily search the required form.

You can create images for each form text and instead of displaying form name or form text display those images.
Its just an idea ,try it .Hope you find a solution.
Good luck........

Not sure if this is what you are looking for and I didn't test this, but it should work OK:

void ReorderItemToTop(string name)
            {
                // Locate item beginning with name's text...
                ListViewItem item = listView1.FindItemWithText(name);
                
                // If exact match, move it to the top of the List...
                if (item != null && name == item.Text)
                {
                    item.Remove(); // remove from current location...
                    listView1.Items.Insert(0, item); // insert at top...
                }
            }

Thanks for your reply Ddoubled...
i am making the characters match about 60-70%,
For example in the textbox if i am entering :"solve 4x^4+3x^3-2x^2+1=0." , then press search button. Then in the listview there appears 5-6 results,
And the entered string occurs in the result listview, but appears at 4th place, when it should be at the 1st place.
Basically i want that if the exact matching string exist in my application, then it should display it on top, otherwise show the closest result related to entered string at the top...
And my application is attested here..........
Kindly solve my problem.........:pretty:

OK, is this what you want? Still not tested, but you can look at comments and logic to determine if I have understood your request:

void ReorderItemToTop(string name)
            {
                // sort items so our FindItemWithText finds first closest match index...
                // this will put them in the best order based on ascending...
                // if there is an exact match to name, it will be found first in code below...
                listView1.Sort();

                // Locate item beginning with name's text...
                ListViewItem item = listView1.FindItemWithText(name, false, 0, true);
                
                // if found and more than one item, begin moving them up the list....
                // If an exact match was found, it will be put at the top...
                if (item != null && listView1.Items.Count > 1)
                {
                    // begin moving matching items up...
                    int index = 0; // this is the top where we will put the first/best matching item
                    do
                    {
                        // move item up to next top position...
                        item.Remove();
                        listView1.Items.Insert(index, item);

                        // find next item beginning at index + 1 (skip one we just moved)...
                        index++;
                        item = listView1.FindItemWithText(name, false, index, true);
                    
                    } while (item != null); // until no more matches...
                }
            }

or perhaps this?:

public void MakeItemTopVisible(string name)
            {
                // Locate item beginning with name's text...
                ListViewItem item = listView1.FindItemWithText(name, false, 0, true);
                // make item first (top) item viewable in the list...
                listView1.TopItem = item;
            }

I have tried the code but could not get any fruitful result.
My application is attached here........
Can you please modify it?:$

Here you go....

private void DisplayResults()
    {
      textBoxCurrentPage.Text = curPage.ToString();
      int recStart = 0 + (RECORDS_PER_PAGE * (curPage - 1));
      int recEnd = recStart + RECORDS_PER_PAGE;
      listView1.BeginUpdate();
      listView1.Items.Clear();
      if (currentSearchResults != null)
      {
        for (int i1 = recStart; i1 < recEnd; i1++)
        {
          if (currentSearchResults.Length > i1)
          {
            ListViewItem lvi = listView1.Items.Add(currentSearchResults[i1].FormType.Name);
            lvi.SubItems.Add(currentSearchResults[i1].Caption);
            lvi.Tag = currentSearchResults[i1];
          }
          else
            break;
        }
          // reorder matching search item to be at top
          ReorderItemToTop(textBox1.Text);
      }
      listView1.EndUpdate();
      buttonNext.Enabled = (curPage < maxPage);
      buttonPrev.Enabled = (curPage > 1);
    }

    void ReorderItemToTop(string name)
    {
        // sort items so our FindItemWithText finds first closest match index...
        // this will put them in the best order based on ascending...
        // if there is an exact match to name, it will be found first in code below...
        listView1.Sort();

        // Locate item beginning with name's text...
        ListViewItem item = listView1.FindItemWithText(name, true, 0, true);

        // if found and more than one item, begin moving them up the list....
        // If an exact match was found, it will be put at the top...
        if (item != null && listView1.Items.Count > 1)
        {
            // begin moving matching items up...
            int index = 0; // this is the top where we will put the first/best matching item
            do
            {
                // move item up to next top position...
                item.Remove();
                listView1.Items.Insert(index, item);

                // find next item beginning at index + 1 (skip one we just moved)...
                index++;
                item = listView1.FindItemWithText(name, true, index, true);

            } while (item != null); // until no more matches...
        }
    }

thanks for ur reply

I need one more help.In my application I have applied functionality for searching various string(form text) which are the combination of characters and math symbols.
Now for varoius symbols like squareroot, cuberoot , fraction, exponent, power,we cannot enter them directly from keyboard.So I have made form text in such a way so that user do not find it difficult to enter these symbols.Like , for squareroot there is sqrt() , for cuberoot there is cuberoot() and so on .
Also to ignore the problem of whitespacing while matching the two strings, i have entered form text without spacing.
But the problem is that when user enters any string and searches it ,the search results appears in such a way which are not easily understandable by the user and user could not find required string (result).
Can you tell me some way or idea for how to display the result that user can understand it and could easily find the required string.

Please mark this thread as solved and create a new thread with question about matching string expressions.

Cheers!

Hi DdoubleD ,
The code which you have given me is not working properly........
According to your code if the string entered in the textbox exactly matches with the form text
only then it shows that result on top otherwise not.....
Well In my application i match the textbox string with each form text and calculate how much percent it
matches.So is it possible that After finding the percents match for each string collect all the percentages in an
array and then sort them in decreasing order.
In this way the form which matches by maximum perecntage will be on top in all cases.
Can you please tell me how to implement this..........

Hi DdoubleD ,
The code which you have given me is not working properly........
According to your code if the string entered in the textbox exactly matches with the form text
only then it shows that result on top otherwise not.....
Well In my application i match the textbox string with each form text and calculate how much percent it
matches.So is it possible that After finding the percents match for each string collect all the percentages in an
array and then sort them in decreasing order.
In this way the form which matches by maximum perecntage will be on top in all cases.
Can you please tell me how to implement this..........

If I remember correctly, the snippet I gave you reorders the list based on matching the beginning of the text. I believe that I performed a sort on the list first, so it should have began moving up matches to the top beginning with:

1) any exact match it found,
2) any partial string matches at the beginning text.

Is it not doing this?

If I remember correctly, the snippet I gave you reorders the list based on matching the beginning of the text. I believe that I performed a sort on the list first, so it should have began moving up matches to the top beginning with:

1) any exact match it found,
2) any partial string matches at the beginning text.

Is it not doing this?

No actually problem is, let i have a form text "evaluate 2/3+5/6", So when user write this string in textbox like this "evaluate 2/3 + 5/6."
So here the difference between both is just spacing difference after 2/3 and before 5/6.
So i want if user enters more spaces or less spaces may be(sometime like this"evaluate2/3+5/6.(there is no space in this string)" So he still get the closest result on top, but the code you gave to me is not doing this. I mean if i write "evaluate 2/3 + 5/6."
So it is not given at top..................
I hope my problem is clear to you........
Kindly help me in this...................

No actually problem is, let i have a form text "evaluate 2/3+5/6", So when user write this string in textbox like this "evaluate 2/3 + 5/6."
So here the difference between both is just spacing difference after 2/3 and before 5/6.
So i want if user enters more spaces or less spaces may be(sometime like this"evaluate2/3+5/6.(there is no space in this string)" So he still get the closest result on top, but the code you gave to me is not doing this. I mean if i write "evaluate 2/3 + 5/6."
So it is not given at top..................
I hope my problem is clear to you........
Kindly help me in this...................

So, for this example you gave, all you need to do is strip those spaces before comparing to list and reordering list:

string s = "evaluate 2/3 + 5/6"; // original text entered by user...
            string sCmpr = s.Substring(0, 9); // retain beginning of string...
            sCmpr += s.Substring(9).Replace(" ", string.Empty); // remove spaces in formula...
            // sCmpr will now look like: "evaluate 2/3+5/6"

then use sCmpr in the call to the void ReorderItemToTop(string name) reordering method I already gave you...

So, for this example you gave, all you need to do is strip those spaces before comparing to list and reordering list:

string s = "evaluate 2/3 + 5/6"; // original text entered by user...
            string sCmpr = s.Substring(0, 9); // retain beginning of string...
            sCmpr += s.Substring(9).Replace(" ", string.Empty); // remove spaces in formula...
            // sCmpr will now look like: "evaluate 2/3+5/6"

then use sCmpr in the call to the void ReorderItemToTop(string name) reordering method I already gave you...

Thanks for your reply DdoubleD........
But the given code is not working......
Can you please help me regarding my problem, it is not giving the result on top so far.

The original search method I wrote in that application used a percentage of result but you requested it to be an exact match so the program was modified. Now you want the percentage back? Take a look at the old threads, you will find the code.

The original search method I wrote in that application used a percentage of result but you requested it to be an exact match so the program was modified. Now you want the percentage back? Take a look at the old threads, you will find the code.

Thanks for your reply Sknake, Well i m using same code which u given to me. But along with that for bringing the result on the top i m using following code:

private void DisplayResults()
    {
      textBoxCurrentPage.Text = curPage.ToString();
      int recStart = 0 + (RECORDS_PER_PAGE * (curPage - 1));
      int recEnd = recStart + RECORDS_PER_PAGE;
      listView1.BeginUpdate();
      listView1.Items.Clear();
      if (currentSearchResults != null)
      {
        for (int i1 = recStart; i1 < recEnd; i1++)
        {
          if (currentSearchResults.Length > i1)
          {
            ListViewItem lvi = listView1.Items.Add(currentSearchResults[i1].FormType.Name);
            lvi.SubItems.Add(currentSearchResults[i1].Caption);
            lvi.Tag = currentSearchResults[i1];
          }
          else
            break;
        }
          // reorder matching search item to be at top
          ReorderItemToTop(textBox1.Text);
      }
      listView1.EndUpdate();
      buttonNext.Enabled = (curPage < maxPage);
      buttonPrev.Enabled = (curPage > 1);
    }
 
    void ReorderItemToTop(string name)
    {
        // sort items so our FindItemWithText finds first closest match index...
        // this will put them in the best order based on ascending...
        // if there is an exact match to name, it will be found first in code below...
        listView1.Sort();
 
        // Locate item beginning with name's text...
        ListViewItem item = listView1.FindItemWithText(name, true, 0, true);
 
        // if found and more than one item, begin moving them up the list....
        // If an exact match was found, it will be put at the top...
        if (item != null && listView1.Items.Count > 1)
        {
            // begin moving matching items up...
            int index = 0; // this is the top where we will put the first/best matching item
            do
            {
                // move item up to next top position...
                item.Remove();
                listView1.Items.Insert(index, item);
 
                // find next item beginning at index + 1 (skip one we just moved)...
                index++;
                item = listView1.FindItemWithText(name, true, index, true);
 
            } while (item != null); // until no more matches...
        }
    }

Well in this code if the string entered in textbox gives closest match with the form text, it gives the result on top.
But in the case if user gives extra spaces then the required result is not shown at the top, but it goes below.And other result comes on top.
like if i have two string on form text, one is: "evaluate 2/3+5/6." And evaluate "2/5+8/9", And if user entered same string as i written above, so it gives respective result on top. But let suppose i have written some extra spaces in the string like " evaluate 2 / 3 + 5 / 6."
So in this case it will not show this string on top .
Here i m entering the string of closest match only but due to extra spaces it is showing other results on the top rather then this matched string.................
Kindly provide me some solution so that it matches all the characters and remove the spaces..................

Thanks for your reply Sknake, Well i m using same code which u given to me. But along with that for bringing the result on the top i m using following code:

private void DisplayResults()
    {
      textBoxCurrentPage.Text = curPage.ToString();
      int recStart = 0 + (RECORDS_PER_PAGE * (curPage - 1));
      int recEnd = recStart + RECORDS_PER_PAGE;
      listView1.BeginUpdate();
      listView1.Items.Clear();
      if (currentSearchResults != null)
      {
        for (int i1 = recStart; i1 < recEnd; i1++)
        {
          if (currentSearchResults.Length > i1)
          {
            ListViewItem lvi = listView1.Items.Add(currentSearchResults[i1].FormType.Name);
            lvi.SubItems.Add(currentSearchResults[i1].Caption);
            lvi.Tag = currentSearchResults[i1];
          }
          else
            break;
        }
          // reorder matching search item to be at top
          ReorderItemToTop(textBox1.Text);
      }
      listView1.EndUpdate();
      buttonNext.Enabled = (curPage < maxPage);
      buttonPrev.Enabled = (curPage > 1);
    }
 
    void ReorderItemToTop(string name)
    {
        // sort items so our FindItemWithText finds first closest match index...
        // this will put them in the best order based on ascending...
        // if there is an exact match to name, it will be found first in code below...
        listView1.Sort();
 
        // Locate item beginning with name's text...
        ListViewItem item = listView1.FindItemWithText(name, true, 0, true);
 
        // if found and more than one item, begin moving them up the list....
        // If an exact match was found, it will be put at the top...
        if (item != null && listView1.Items.Count > 1)
        {
            // begin moving matching items up...
            int index = 0; // this is the top where we will put the first/best matching item
            do
            {
                // move item up to next top position...
                item.Remove();
                listView1.Items.Insert(index, item);
 
                // find next item beginning at index + 1 (skip one we just moved)...
                index++;
                item = listView1.FindItemWithText(name, true, index, true);
 
            } while (item != null); // until no more matches...
        }
    }

Well in this code if the string entered in textbox gives closest match with the form text, it gives the result on top.
But in the case if user gives extra spaces then the required result is not shown at the top, but it goes below.And other result comes on top.
like if i have two string on form text, one is: "evaluate 2/3+5/6." And evaluate "2/5+8/9", And if user entered same string as i written above, so it gives respective result on top. But let suppose i have written some extra spaces in the string like " evaluate 2 / 3 + 5 / 6."
So in this case it will not show this string on top .
Here i m entering the string of closest match only but due to extra spaces it is showing other results on the top rather then this matched string.................
Kindly provide me some solution so that it matches all the characters and remove the spaces..................

As I have understood, your form text is static and you already have the spaces stripped from the text following the prefix "evaluate ".

So, as I have also understood, it is the spaces the user enters in a textbox that you need to ignore, which is why I provided this:

string s = "evaluate 2/3 + 5/6"; // original text entered by user...
            string sCmpr = s.Substring(0, 9); // retain beginning of string...
            sCmpr += s.Substring(9).Replace(" ", string.Empty); // remove spaces in formula...
            // sCmpr will now look like: "evaluate 2/3+5/6"

So, if I have understood correctly, you only need to modify the code to use the above prior to the call to ReorderItemToTop . Like prior to line 22, this is how I expected you to modify the code:

string s = textBox1.Text; // original text entered by user...
            string sCmpr = s.Substring(0, 9); // retain beginning of string...
            sCmpr += s.Substring(9).Replace(" ", string.Empty); // remove spaces in formula...
            // sCmpr will now look like: "evaluate 2/3+5/6", instead of "evaluate 2 / 3 + 5 / 6"

// Now call reorder with spaces removed from user's text...
          // reorder matching search item to be at top
          ReorderItemToTop(sCmpr); // currently at line 22

I replied to your PM this morning asking you to clarify further why all of the pieces you have asked for don't do what you want, but you have not provided any further details...

hi DdoubleD
Here is my application..........
Kindly help me in this.................

hi DdoubleD
Here is my application..........
Kindly help me in this.................

I'm running your app. You removed the code I gave you, modified existing code, but now I'm at a point where I think I know where to make changes to fix this "once and for all".

I have a question: if the user types "eval" instead of "Eval", it will not find any matches--is that how you want it to work, or should that be changed too?

I'm running your app. You removed the code I gave you, modified existing code, but now I'm at a point where I think I know where to make changes to fix this "once and for all".

I have a question: if the user types "eval" instead of "Eval", it will not find any matches--is that how you want it to work, or should that be changed too?

No, I want to show the matches, no matter the user types lower case or upper case letters................
I want two things basically, that closest match should display on top and others are below
And
if user enter any number of spaces between characters and numbers in the string , So still in that case it should display the result, It should just consider the entered letters(Any type of Characters), not spaces. It should remove the spaces as getString() function does.
Kindly help me in this................

No, I want to show the matches, no matter the user types lower case or upper case letters................
I want two things basically, that closest match should display on top and others are below
And
if user enter any number of spaces between characters and numbers in the string , So still in that case it should display the result, It should just consider the entered letters(Any type of Characters), not spaces. It should remove the spaces as getString() function does.
Kindly help me in this................

So, these should all match (and note case diff for P(X)):
Evaluate 2 / 3
evaluate 2/3
evaluate2/3
P(X) = x
P(x)=x
p(X)=X


but, these should not match:
eval 2/3
eval2/3

Is that correct?

Well i have send you a private message, But i dont know why that is not reached...........
Well here is what i want:
Yes, these should all match (and note case diff for P(X)):
Evaluate 2 / 3
evaluate 2/3
evaluate2/3
P(X) = x
P(x)=x
p(X)=X
And since i am doing 50% match in my application, So if entered string is match 50%, So it should display too, And if it is not match, So will not display.
Like on my form text i have "evaluate2/3". And if user enters "eval 2 / 3", So it should be display because it is matches 50% from the form text, And if user enters "ev 2 /3", So it will not show, because it is not match 50%.
I think now it is clear to you...........
I basically want that no matter user enter any number of spaces between characters or numbers , after that it will match the result and closest result show on top.

OK, I think I understand now. It's a good thing I asked, because I didn't realize you expected "eval 2/3" to match "evaluate 2/3". Anyway, I know either sknake or Ryshad provided you code to match a percentage of the text. Tell me where that is so I can incorporate into the rebuilding of the list.

One last question, if I understood your last response, you now want every list item that does NOT match to be removed from your list instead of just moving the matching items to the top?

OK, I think I understand now. It's a good thing I asked, because I didn't realize you expected "eval 2/3" to match "evaluate 2/3". Anyway, I know either sknake or Ryshad provided you code to match a percentage of the text. Tell me where that is so I can incorporate into the rebuilding of the list.

One last question, if I understood your last response, you now want every list item that does NOT match to be removed from your list instead of just moving the matching items to the top?

Thanks for your reply.............

My application is attached here and in this the percentage match code is in "FormSearcher Class " .
I just want if the entered string matches100% ,then it will be on top or if it matches 50% or more with the formText ,then maximum matched result should be at top while others below( in the decreasing order of match percentage ).Like if the one formtext matches 90% ,second 85%,third 75% and fourth 50% while other formtext do not match at all, then 90%matched result should be at top ,then 85%,then 75%,then 50%. while others which do not match at all or match less than 50% should not be displayed.All the results which matches 50% or more should display ..................
Hope you understand....

Thanks for your reply.............

My application is attached here and in this the percentage match code is in "FormSearcher Class " .
I just want if the entered string matches100% ,then it will be on top or if it matches 50% or more with the formText ,then maximum matched result should be at top while others below( in the decreasing order of match percentage ).Like if the one formtext matches 90% ,second 85%,third 75% and fourth 50% while other formtext do not match at all, then 90%matched result should be at top ,then 85%,then 75%,then 50%. while others which do not match at all or match less than 50% should not be displayed.All the results which matches 50% or more should display ..................
Hope you understand....

I will take a look at this today. Having no idea the exact rules constituting those percentages you mention, I'll just "cross my fingers" and hope that whatever routine is already written to match an "acceptable percentage" agrees with your perception of what does and does not partially match.

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.