Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to highlight syntax as soon as loading text file in to richtextbox

how to highlight syntax as soon as loading text file in to richtextbox

Scheduled Pinned Locked Moved C#
csharpdatabaseregextutorial
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    veluru krishna
    wrote on last edited by
    #1

    hi all, I am able to highlight the syntax when I type the text ie c# code in richtextbox using the following code code: // Calculate the starting position of the current line. int start = 0, end = 0; for (start = richTextBox1.SelectionStart - 1; start > 0; start--) { if (richTextBox1.Text[start] == '\n') { start++; break; } } // Calculate the end position of the current line. for (end = richTextBox1.SelectionStart; end < richTextBox1.Text.Length; end++) { if (richTextBox1.Text[end] == '\n') break; } // Extract the current line that is being edited. String line = richTextBox1.Text.Substring(start, end - start); // Backup the users current selection point. int selectionStart = richTextBox1.SelectionStart; int selectionLength = richTextBox1.SelectionLength; // Split the line into tokens. Regex r = new Regex("([ \\t{}();])"); string [] tokens = r.Split(line); int index = start; foreach (string token in tokens) { // Set the token's default color and font. richTextBox1.SelectionStart = index; richTextBox1.SelectionLength = token.Length; richTextBox1.SelectionColor = Color.Black; richTextBox1.SelectionFont = new Font("Courier New", 10, FontStyle.Regular); // Check whether the token is a keyword. String [] keywords = {"abstract","base","bool","break","byte","case","catch",,...."using","virtual","void" }; for (int i = 0; i < keywords.Length; i++) { if (keywords[i] == token) { // Apply alternative color and font to highlight keyword. richTextBox1.SelectionColor = Color.Blue; richTextBox1.SelectionFont = new Font("Courier New", 10, FontStyle.Bold); break; } } index += token.Length; } // Restore the users current selection point. richTextBox1.SelectionStart = selectionStart; richTextBox1.SelectionLength = selectionLength; But my aim is to write c# code in text file and load that text file into a richtext

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups