Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,362 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,260 people online right now. Registration is fast and FREE... Join Now!




Find and Replace in C#

 
Reply to this topicStart new topic

Find and Replace in C#

PsychoCoder
post 30 Aug, 2008 - 12:04 PM
Post #1


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,923



Thanked 118 times

Dream Kudos: 8475

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


For the small application Im working on (the snippets manager) Im implementing find & replace. The code I have finds the string properly, but it isn't highlighting the text, even though I tell it to. Here's the code for Find

csharp

public static void Find(string text, bool matchCase, RichTextBox rtb, System.Windows.Forms.Button btn)
{
try
{
int startPos;
StringComparison type;

type = matchCase == true ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;

startPos = rtb.Text.IndexOf(text, type);

if (!(startPos > 0))
{
MessageBox.Show("Search text: '" + text + "' could not be found", "Text Not Found", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return;
}
else
{
rtb.Select(startPos, text.Length);
rtb.ScrollToCaret();
btn.Enabled = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Search Error");
}
}


That code finds the text, even scrolls the caret to the text, but it isn't highlighting it. Any ideas?
User is offlineProfile CardPM

Go to the top of the page

gbertoli3
post 30 Aug, 2008 - 12:23 PM
Post #2


DIC at Heart + Code

Group Icon
Joined: 23 Jun, 2008
Posts: 1,026



Thanked 16 times

Dream Kudos: 950
My Contributions


Try deleting the RichTextBox / TextBox then drag a new one onto the form with same properties.

This might help.

It is not selecting the text, but it is going to the start of the word(s).

Hope this helps

This post has been edited by gbertoli3: 30 Aug, 2008 - 12:31 PM
User is offlineProfile CardPM

Go to the top of the page

Jayman
post 30 Aug, 2008 - 05:23 PM
Post #3


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,839



Thanked 38 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


You need to give the RTB focus to see the highlighted text.
CODE

public static void Find(string text, bool matchCase, RichTextBox rtb, System.Windows.Forms.Button btn)
{
    try
    {
        int startPos;
        StringComparison type;

        type = matchCase == true ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;

        startPos = rtb.Text.IndexOf(text, type);

        if (!(startPos > 0))
        {
            MessageBox.Show("Search text: '" + text + "' could not be found", "Text Not Found", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            return;
        }
        else
        {
            rtb.Select(startPos, text.Length);
            rtb.ScrollToCaret();
            // Give it focus here
            rtb.Focus();
            btn.Enabled = true;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Search Error");
    }
}
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 30 Aug, 2008 - 05:45 PM
Post #4


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,923



Thanked 118 times

Dream Kudos: 8475

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


I swear I'm a dork sometimes! Thanks Jay smile.gif
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 04:58AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month