开发者

How would I create a find/search button on form2 to search and highlight text in a rich text box on form1 in c#

I am currently developing a small RTF editor in Visual Studio 2008 in C#

I have compleeted almost all of it accept the find/find next buttons on the second form

Form1 contains a rich text box

form2 contains a textbox for the text to search and two buttons - Find and Find Next

When you click find, the text you enter into the textbox on form2 will be highlighted in form1

I have spent hours and hours trying to figure this out and cannot come to a conclusion

Is anyone able to give me a hand here? Thanks in advance

ive managed to get hold of a friend for some help and this is now what i have for my find button, im not sure if this is the best way to do it.

I know there is a richtextb开发者_开发百科ox.find method but i dont have a clue how to use that ` public partial class frmFind : Form { frmRTFEditor frmRTFEditor;

    public frmFind()
    {
        InitializeComponent();
    }

    public frmFind(frmRTFEditor form)
    {
        InitializeComponent();
        frmRTFEditor = form;
        this.TopMost = true;
    }

    private void cmdFind_Click(object sender, EventArgs e)
    {
        lblMatch.Visible = false;
        try
        {
            if (txtSearch.Text == "")
            {
                lblMatch.Visible = true;
            }
            else
            {
                int StartPosition;
                StringComparison SearchType;

                SearchType = StringComparison.OrdinalIgnoreCase;

                StartPosition = frmRTFEditor.rtbDoc.Text.IndexOf(txtSearch.Text, SearchType);

                if (StartPosition < 0)
                {
                    lblMatch.Visible = true;
                }
                else
                {

                    frmRTFEditor.rtbDoc.Select(StartPosition, txtSearch.Text.Length);
                    frmRTFEditor.rtbDoc.ScrollToCaret();
                    frmRTFEditor.Focus();
                    cmdFindNext.Enabled = true;
                }
            }
        }
        catch (Exception)
        {
            lblMatch.Visible = true;
            cmdFindNext.Enabled = false;
        }
    }`


From the 'sound' of your question I suspect you're trying to write the search and highlight code in the code behind for Form2 or that you are perhaps trying to create a circular reference somewhere.

I would use Form2 as UI only. Create an instance of it from Form1, show Form2, read the input values back in to your code in Form1. From there you can access all the text in rich text box on form1.

The quickest/simplest way to find the search text might be to have a loop that contains calls to richTextBox1.Text.IndexOf(..)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜