Beispiel #1
0
        // perform search and replace using Selection properties
        void replaceUsingSelection_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Replacing 'work' with 'labor' using Selection.");

            // re-load document
            this.c1Editor1.LoadXml(Properties.Resources.tesla, null);

            // strings to search and replace
            string search  = "work";
            string replace = "labor";

            // do the work
            int         count         = 0;
            int         previousStart = 0;
            C1TextRange range         = this.c1Editor1.CreateRange();

            for (int start = 0; ; start += search.Length)
            {
                // get text (notice new line handling)
                string txt = this.c1Editor1.Text;
                txt = txt.Replace("\r\n", "\n");

                // find text, break when done
                start = txt.IndexOf(search, start, StringComparison.OrdinalIgnoreCase);
                if (start < 0)
                {
                    break;
                }

                // select match
                // recommended way (better performance): move range relative to its current position
                range.Move(start - previousStart, search.Length);
                range.Select();

                // not recommended way (slower): using the Select method
                //this.c1Editor1.Select(start, search.Length);

                // tell user we're about to replace
                string msg = string.Format("About to replace instance {0}.", ++count);
                if (MessageBox.Show(msg, "Replace", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                {
                    break;
                }

                // second part of the code moving range
                range.Text = replace;
                range.ApplyTag("strong");
                previousStart = start;

                // second part of the code using the Select method
                //this.c1Editor1.Selection.Text = replace;
                //this.c1Editor1.Selection.ApplyTag("strong");
            }

            // done
            MessageBox.Show(string.Format("Done, {0} instances found.", count));
        }
Beispiel #2
0
        //-------------------------------------------------------------------------------------
        #region ** implementation

        void UpdateSyntax()
        {
            // get text
            string text = c1Editor1.Text;

            // remove formatting
            C1TextRange rng = c1Editor1.CreateRange(0, text.Length);

            rng.RemoveTag("span");

            // apply all syntax descriptors
            foreach (SyntaxDescriptor sd in GetSyntaxDescriptors())
            {
                // initialize
                int start = 0;
                rng.Start.Move(MoveUnit.Character, -50000000);
                rng.End.MoveTo(rng.Start);

                // if there's a group called 'match', use it;
                // otherwise, use the first one
                int gIndex = sd.Regex.GroupNumberFromName("match");
                if (gIndex < 0)
                {
                    gIndex = 0;
                }

                // find all matches for this descriptor
                for (Match match = sd.Regex.Match(text); match.Success; match = match.NextMatch())
                {
                    // apply descriptor style for this match
                    Group g = match.Groups[gIndex];
                    foreach (Capture c in g.Captures)
                    {
                        rng.Move(c.Index - start, c.Length);

                        // avoid nested spans (e.g. reserved words in comments, etc)
                        if (rng.Node.ParentNode.Name != "span")
                        {
                            rng.ApplyClass(sd.ClassName, C1StyleType.Character);
                        }

                        start = c.Index;
                    }
                }
            }
        }