3 Replies - 822 Views - Last Post: 14 June 2013 - 08:27 AM Rate Topic: -----

#1 CY5  Icon User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 315
  • Joined: 28-September 12

Notepad Find option in C# ..Unable to search the text in textbox

Posted 13 June 2013 - 11:33 PM

I am trying to create a notepad like application in C# by using a Textbox. I want to implement find function in it. I want an ability to search the text entered in textbox of Find form in textbox of Form1 and then search it and highlight it. Please help i am unable to do it
Iam not able to search the text i get the :gun_bandana:/> idx=-1

Form1.cs

    private void findToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Find f = new Find();

        f.Show();

    }

    public void find()
    {

        int idx = 0;
        while((idx=textBox1.Text.IndexOf(text))!=-1)
        {
            textBox1.Select(idx, text.Length);//Select the text which are found
        }
    } 

Find.cs

   public partial class Find : Form
    {
    Form1 f = new Form1();
    public Find()
    {
        InitializeComponent();
    }


    private void Cancelbutton2_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void Findbutton1_Click(object sender, EventArgs e)
    {
        f.text =textBox1.Text;
        f.find();

    }

This post has been edited by Curtis Rutland: 14 June 2013 - 06:22 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Notepad Find option in C# ..Unable to search the text in textbox

#2 Michael26  Icon User is offline

  • Radar love
  • member icon

Reputation: 332
  • View blog
  • Posts: 1,420
  • Joined: 08-April 09

Re: Notepad Find option in C# ..Unable to search the text in textbox

Posted 14 June 2013 - 01:02 AM

First use code tags
Second use the Richtextbox as it already has Find method
Was This Post Helpful? 1
  • +
  • -

#3 CY5  Icon User is offline

  • D.I.C Regular

Reputation: 10
  • View blog
  • Posts: 315
  • Joined: 28-September 12

Re: Notepad Find option in C# ..Unable to search the text in textbox

Posted 14 June 2013 - 08:24 AM

i wanted to use textbox.
As from the Question or code
(idx=textBox1.Text.IndexOf(text)


why i am gettin idx=-1 what is the error
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10185
  • View blog
  • Posts: 37,600
  • Joined: 27-December 08

Re: Notepad Find option in C# ..Unable to search the text in textbox

Posted 14 June 2013 - 08:27 AM

Per the documentation:

Quote

The zero-based index position of value if that string is found, or -1 if it is not. If value is String.Empty, the return value is 0.

http://msdn.microsof...y/k8b1470s.aspx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1