C # Add a search image to RichTextBox

Source: Internet
Author: User

 

Code

Code
/*
-- Organizer: Eternal de shadow

-- Sorting time: 2010/06/08

-- Content: C # how to add a search image to RichTextBox:
*/

//★★★★★★★★★Case Analysis:★★★★★★★★★★★★★★★★★★★★★★★★★★★
// Environment: C #
// 1 I Have A RichTextBox Control with a lot of content. I want to press the button (CTRL + F) to bring up a search screen.
// 2 in the search screen, I enter a value to search for the content in RichTextBox on the parent page.
// 3 pairs of searched content are marked with special colors

//★★★★★★★★★Implementation Method:★★★★★★★★★★★★★★★★★★★★★★★★★★★
// Step 1: add the code in the richtextbox1_keydown event:
If (E. Control & E. keycode = keys. f)
{
// Select the content of all RichTextBox
This. richtextbox1.selectall ();

// Change the selected font color of RichTextBox
This. richtextbox1.selectioncolor = color. Black;

// Change the background color of the selected font of RichTextBox
This. richtextbox1.selectionbackcolor = color. moccasin;

// Select the 0 characters starting from 0 in RichTextBox
This. richtextbox1.select (0, 0 );

// Roll back the scroll bar to the focus position
This. richtextbox1.scrolltocaret ();

// The search screen is displayed.
Frmlookfor FRM = new frmlookfor ();
FRM. Show (this );
}

// Step 2: Find the image

// Variable definition and Object Instantiation
Public static string strkey = "";
Private int Index = 0;
Private int K = 1;

Strkey = This. textbox1.text;

If (strkey = "")
{
Return;
}

// Initial Stages
Frmrulerchk frm1 = (frmrulerchk) This. owner; // to get the main form in the child form

// Specify the merge character column.
Int M = system. text. regularexpressions. regEx. matches (RichTextBox) frm1.controls ["richtextbox1"]). text, strkey, system. text. regularexpressions. regexoptions. ignorecase ). count;

If (RichTextBox) frm1.controls ["richtextbox1"]). Text! = "")
{
If (k <= m)
{
While (Index = (RichTextBox) frm1.controls ["richtextbox1"]). Find (strkey, index, richtextboxfinds. None)> = 0)
{
// Select colors for text Columns
(RichTextBox) frm1.controls ["richtextbox1"]). selectioncolor = color. Red;

// Select し text column の backcolor
(RichTextBox) frm1.controls ["richtextbox1"]). selectionbackcolor = color. blue;

// Initiate a temporary migration task
(RichTextBox) frm1.controls ["richtextbox1"]). Focus ();

// Select a text Column
(RichTextBox) frm1.controls ["richtextbox1"]). Select (index, 5 );

// Please wait until there is no manual operation
(RichTextBox) frm1.controls ["richtextbox1"]). scrolltocaret ();
Index ++;
K ++;

If (k = m)
{
MessageBox. Show ("the start position of the zookeeper is zookeeper without authorization", "NOTE", messageboxbuttons. OK, messageboxicon. information );
Index = 0;
K = 1;
}

Return;
}
}
}

//★★If you want to display only one query screen and minimize the status to normal:★★★★★★★★
// Change the code in step 1:

// Instantiate the form to be opened
Public static frmlookfor frmlook = NULL;

If (E. Control & E. keycode = keys. f)
{
If (frmrulerchk. frmlook = NULL)
{
This. richtextbox1.selectall ();

This. richtextbox1.selectioncolor = color. Black;
This. richtextbox1.selectionbackcolor = color. moccasin;
This. richtextbox1.select (0, 0 );
This. richtextbox1.scrolltocaret ();
Frmrulerchk. frmlook = new frmlookfor ();
Frmrulerchk. frmlook. Show (this );
}
Else
{
Try
{
If (frmrulerchk. frmlook. windowstate = formwindowstate. Minimized)
{
Frmrulerchk. frmlook. windowstate = formwindowstate. normal;
}
Frmrulerchk. frmlook. Activate ();
}
Catch
{
This. richtextbox1.selectall ();

This. richtextbox1.selectioncolor = color. Black;
This. richtextbox1.selectionbackcolor = color. moccasin;
This. richtextbox1.select (0, 0 );
This. richtextbox1.scrolltocaret ();
Frmlookfor FRM = new frmlookfor ();
FRM. Show (this );
}
}
}

//★★If you want to change the form to run, the icon is not displayed in the taskbar:★★★★★★★★

// Modify attributes: showintaskbar = false;

// Note: This code must be added when the image search function is disabled:

Frmrulerchk. frmlook = NULL;

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.