8 Replies - 15719 Views - Last Post: 11 March 2011 - 03:13 AM Rate Topic: -----

#1 pandukal21  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 08-March 11

Find/FindNext feature -- Notepad Application in C#

Posted 08 March 2011 - 11:58 PM

Hi,

I'm developing a Notepad Application using C#/Windows Form Application. I've implemented all the basic features of a windows notepad. Now I'm trying to add "Find" and "FindNext" feature, and not able to figure out how to implement??

Rgds,
pandukal21
Is This A Good Question/Topic? 0
  • +

Replies To: Find/FindNext feature -- Notepad Application in C#

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: Find/FindNext feature -- Notepad Application in C#

Posted 09 March 2011 - 01:58 AM

Hi,

Have you used a rich text box? If not, I would recommend doing so because the rich text box class has a useful method called Find() that could really help you out with this.

Have a look at these:

Find()

Select()


Note that the Find() method also has overloads that take a starting position as an arguement, and that Find() returns the starting position of the word that has been found. You can also specify different search conditions using the RickTextBoxFinds enumeration...

RichTextBox Class


Also, one problem I see you encountering is that the 'Select method doesn't select the word'. To overcome this, you need to use the Focus() method of the rich text box to give the control focus as selected text only appears if the control has focus.

:)

This post has been edited by CodingSup3rnatur@l-360: 09 March 2011 - 02:46 AM

Was This Post Helpful? 0
  • +
  • -

#3 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: Find/FindNext feature -- Notepad Application in C#

Posted 09 March 2011 - 08:13 AM

Well, the entire text of whatever is is in your document is a string, and all strings have a Contains() method. Also, you will probally find the Substring() and IndexOf() methods useful.

Using these three methods, you can find if the text contains a search string, get the index of the search string (find), and then create a new string from the index of where the current search string is found and the end of text. This new string can then be seached in order to find the next occurance of the search string (find next).

You just need to make sure you keep track of the index of the first where the current instance of the search string lies in your text :)

This post has been edited by ragingben: 09 March 2011 - 08:13 AM

Was This Post Helpful? 0
  • +
  • -

#4 pandukal21  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 08-March 11

Re: Find/FindNext feature -- Notepad Application in C#

Posted 10 March 2011 - 11:37 PM

Hi CodingSup3rnatur@l-360,

Thanks for your info...'ve got it almost done. But the thing is I'm not able to set the focus after searching a string. Anywhere 'm going wrong??

rgds,
pandukal21
Was This Post Helpful? 0
  • +
  • -

#5 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: Find/FindNext feature -- Notepad Application in C#

Posted 11 March 2011 - 01:38 AM

Hi,

Show us the code you have at the moment and we'll see if
we can help :).

You would set the focus like this:

this.textBoxNameHere.Focus();

This post has been edited by CodingSup3rnatur@l-360: 11 March 2011 - 02:21 AM

Was This Post Helpful? 0
  • +
  • -

#6 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: Find/FindNext feature -- Notepad Application in C#

Posted 11 March 2011 - 02:41 AM

If I have read this correctly I hink pandukal21 is trying to highlight the text in the text box, not focus on the control. If this is the case both TextBox and richTextBox have a Select() method...

If I have read this correctly I hink pandukal21 is trying to highlight the text in the text box, not focus on the control. If this is the case both TextBox and richTextBox have a Select() method...
Was This Post Helpful? 0
  • +
  • -

#7 pandukal21  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 08-March 11

Re: Find/FindNext feature -- Notepad Application in C#

Posted 11 March 2011 - 03:04 AM

Hi ragingben/CodingSup3rnatur@l-360,

I've developed a "notepad clone- windows form application" which consists of all the features except "Find". So inorder to implement "Find", I've created a new Form called FindForm which pops-up on the click event of the "Find" menu in the "Notepad clone" form.
So,for the "FindToolStripMenu_Click" event, my code is as follows,

 private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            FindForm ff = new FindForm();
            ff.Show();
        }



The above code pops-up the "FindForm" on the click on "Find" menu. The "FindForm" is similar to the "Find" menu pop-up of the windows notepad, with same controls and textbox. Now I'd like to search the string contained in the "Notepad" form by entering the string in the "FindForm". So I'd like to know how to bind these two forms so that I could use my "search string code" in Notepad.cs..??


Rgds,
pandukal21
Was This Post Helpful? 1
  • +
  • -

#8 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: Find/FindNext feature -- Notepad Application in C#

Posted 11 March 2011 - 03:09 AM

Hi ragingben,

Yes, you are right, and both text box controls have a Select method. However, my thinking behind using the RichTextBox is that it has a Find method that works perfectly together with the Select method to produce the Find/Find Next functionality.

Plus, the Find method can accept an enumeration as an arguement which allows you to specify search criteria (like searching from bottom to top, matching case etc), thus allowing you to mimic the real Notepad's Find/FindNext feature much more closely and easily.

My thinking behind the focus thing is that if you call the Select method and the text box hasn't got focus, I don't think the text in the text box will be visibly highlighted. Therefore, I reckon you would have to give it focus for the highlighting to become apparent (because, don't forget, the user will probably be pressing a button to use this feature, therefore the button will have focus, not the text box. Therefore, the selected text will not be visibly hightlighted until you give the text box focus again).

I could be wrong, as I haven't tested it. That would be my guess though :)
Was This Post Helpful? 2
  • +
  • -

#9 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: Find/FindNext feature -- Notepad Application in C#

Posted 11 March 2011 - 03:13 AM

Assuming you are loading you "FindForm" from your "NotePad" form, just create a property, say SearchText, on your "FindForm" which is set to whatever the search text is specified as, and then on your "Notepad" form when you create the "FindFrom" subscribe to the Closing event of the "FindForm", and then in the event handler you can read back the SearchText property to get the search text that was entered. Then you can search for it on your NotePad form :)

CodingSup3rnatur@l-360 - I'm with you on that. I havn't tested it either but good point
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1