public static void ShowSaveAsDialog(TabPage tabPage) { if (TabControlMethods.IsEmpty()) { return; } TypingArea currentTextArea = (tabPage.Controls[0] as MyRichTextBox).TypingArea; //Create a save dialog SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.Filter = "txt Files (*txt)|*txt"; saveDialog.DefaultExt = "txt"; //Pop up the save dialog check if user press save button if (saveDialog.ShowDialog() == DialogResult.OK) { using (Stream s = File.Open(saveDialog.FileName, FileMode.Create)) { using (StreamWriter sw = new StreamWriter(s)) { sw.Write(currentTextArea.Text); tabPage.Text = Path.GetFileName(saveDialog.FileName); tabPage.Name = saveDialog.FileName; } } } //dispose for sure saveDialog.Dispose(); }
private void findNextButton_Click(object sender, EventArgs e) { TypingArea currentTextArea = TabControlMethods.CurrentTextArea; //Check if the current string hasn't been search last turns if (previousText != currentTextArea.Text) { previousText = currentTextArea.Text; textsFound.Clear(); textsFound = currentTextArea.FindAll(searchTextbox.Text); } if (textsFound.Count != 0) { if (searchTextbox.Text.Length == 0) { return; } indexOfSearchText++; if (indexOfSearchText == textsFound.Count) { indexOfSearchText = 0; } //Each every text found next will be highlighted currentTextArea.Select(textsFound[indexOfSearchText], searchTextbox.Text.Length); } currentTextArea.Focus(); }
private void findPreviousButton_Click(object sender, EventArgs e) { TypingArea currentTextArea = TabControlMethods.CurrentTextArea; if (previousText != currentTextArea.Text) { previousText = currentTextArea.Text; textsFound.Clear(); textsFound = currentTextArea.FindAll(searchTextbox.Text); } if (textsFound.Count != 0) { if (searchTextbox.Text.Length == 0) { return; } indexOfSearchText--; if (indexOfSearchText <= -1) { indexOfSearchText = textsFound.Count - 1; } //Each every text found privous will be highlighted currentTextArea.Select(textsFound[indexOfSearchText], searchTextbox.Text.Length); } currentTextArea.Focus(); }
public static void ShowSaveDialog(TabPage tabPage) { if (TabControlMethods.IsEmpty()) { return; } //Choose the current typing area TypingArea currentTextArea = (tabPage.Controls[0] as MyRichTextBox).TypingArea; //If the tab opening typing area already has a name, just save it if (tabPage.Name != "") { using (Stream s = File.Open(tabPage.Name, FileMode.Create)) { //get the streamwriter of the new file using (StreamWriter sw = new StreamWriter(s)) { //Get the text of the current typing area and write it to streamwriter sw.Write(currentTextArea.Text); tabPage.Text = Path.GetFileName(tabPage.Name); return; } } } //Create a save file Dialog SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.Filter = "txt Files (*txt)|*txt"; saveDialog.DefaultExt = "txt"; //Open save dialog and check if user press save button if (saveDialog.ShowDialog() == DialogResult.OK) { //Declare a Stream variable to hold the open file to write in using (Stream s = File.Open(saveDialog.FileName, FileMode.Create)) { //get the streamwriter of the new file using (StreamWriter sw = new StreamWriter(s)) { sw.Write(currentTextArea.Text); //change the text title of the tab by file name tabPage.Text = Path.GetFileName(saveDialog.FileName); //In the next time, if this tab page already has a name, just save it tabPage.Name = saveDialog.FileName; } } } //dispose for sure saveDialog.Dispose(); }
/// <summary> /// Raise when a new tab page added /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tabControl_ControlAdded(object sender, ControlEventArgs e) { //We cannot use TabControlMethods.CurrentArea //Because this event raise before the SelectedTab is set. MyRichTextBox currentRTB = tabControl.TabPages[tabControl.TabPages.Count - 1].Controls[0] as MyRichTextBox; TypingArea currentArea = currentRTB.TypingArea; //Set the event handler helping update status bar currentArea.TextChanged += TextArea_TextChanged; }
private void FindingForm_FormClosing(object sender, FormClosingEventArgs e) { foreach (TabPage tabPage in TabControlMethods.TabControl.TabPages) { TypingArea textArea = (tabPage.Controls[0] as MyRichTextBox).TypingArea; textArea.ClearBackColor(textArea.BackColor); } this.Visible = false; e.Cancel = true; }
private void findButton_Click(object sender, EventArgs e) { TypingArea currentTextArea = TabControlMethods.CurrentTextArea; previousText = currentTextArea.Text; //Remove the highlight backcolor of the privous search currentTextArea.ClearBackColor(currentTextArea.BackColor); textsFound.Clear(); textsFound = currentTextArea.FindAndColorAll(searchTextbox.Text, AllFoundTextBackColor); indexOfSearchText = -1; this.Focus(); }
private void replaceAllButton_Click(object sender, EventArgs e) { //Check if this string has been replaced or this string has nothing if (searchTextbox.Text.Equals(replacementTextbox.Text)) { return; } TypingArea currentTextArea = TabControlMethods.CurrentTextArea; //Replace all the selected found text by replacement text string textToReplace = currentTextArea.Text.Replace(searchTextbox.Text, replacementTextbox.Text); currentTextArea.Select(0, currentTextArea.TextLength); currentTextArea.Text = currentTextArea.Text.Replace(searchTextbox.Text, replacementTextbox.Text); }
public static void ShowOpenDialog(TabControl tabControl) { //create a new file dialog and choose file to open OpenFileDialog openDialog = new OpenFileDialog(); openDialog.Filter = "txt Files (*txt)|*txt|All Files (*.*)|*.*"; if (openDialog.ShowDialog() == DialogResult.OK) { //check to see if there is already this tab page TabPage currentTabPage = null; foreach (TabPage tabPage in tabControl.TabPages) { if (tabPage.Name == openDialog.FileName) { currentTabPage = tabPage; break; } } //Create a new tab page TabPage newTabPage = TabControlMethods.CreateNewTabPage(openDialog.SafeFileName); //a variable to hold text box contained in tab page TypingArea newTypingArea = TabControlMethods.CurrentTextArea; //Get the path of the File string filePath = openDialog.FileName; //Get the text of the file string fileText = File.ReadAllText(filePath); //Set the text of current text box by file Text newTypingArea.Text = fileText; //In the next time, if this tab page already has a name, just open it tabControl.SelectedTab.Name = openDialog.FileName; } //dispose for sure openDialog.Dispose(); }
// // Method: OpenFile // Description: Clears the text box and imports a new file to the TextBox // Parameters: void // Returns: void // private void OpenFile() { // Open file dialog OpenFileDialog OpenFile = new OpenFileDialog(); // Display the open file dialog OpenFile.ShowDialog(); // Safe guard in case the user wants to cancel instead of open the file if (OpenFile.FileName != "") { // Clear the TypingArea TypingArea.Document.Blocks.Clear(); // Read all the text to the typing area TypingArea.AppendText(File.ReadAllText(OpenFile.FileName)); // Change the status of the UnsavedWork UnsavedWorkStatus(false); } }
private void replaceButton_Click(object sender, EventArgs e) { TypingArea currentTextArea = TabControlMethods.CurrentTextArea; currentTextArea.Focus(); //Check if this string has been replaced or this string has nothing if (indexOfSearchText == -1 || searchTextbox.Text.Equals(replacementTextbox.Text) || currentTextArea.SelectionLength == 0) { return; } //We might changed or do something with the text so we need to get this again textsFound.Clear(); textsFound = currentTextArea.FindAll(searchTextbox.Text); currentTextArea.Select(textsFound[indexOfSearchText], searchTextbox.Text.Length); //Make it be replaced currentTextArea.SelectedText = replacementTextbox.Text; //just select for nothing much currentTextArea.Select(textsFound[indexOfSearchText], replacementTextbox.Text.Length); }
/// <summary> /// Set syntax highlight to the specified language. /// </summary> /// <param name="language"></param> private void SetHighlightRule(string language) { if (TabControlMethods.IsEmpty()) { return; } TypingArea currentTextArea = TabControlMethods.CurrentTextArea; Font fontToSet; /*Use for the specified keyword highlighting.*/ var typingFont = currentTextArea.Font; TabControlMethods.CurrentTextArea.EnableHighlight = true; TabControlMethods.CurrentTextArea.Clear(); switch (language) { case "C#": { //Number highlight currentTextArea.AddHighlightDescriptor(DescriptorRecognition.IsNumber, "", HighlightType.ToEOW, Color.IndianRed, typingFont, UsedForAutoComplete.No); //Keyword highlight var keywords = new List <string>() { "abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", "class", "const", "continue", "decimal", "default", "delegate", "do", "double", "else", "enum", "event", "explicit", "extern", "false", "finally", "fixed", "float", "for", "foreach", "goto", "if", "implicit", "in", "int", "interface", "internal", "is", "lock", "long", "namespace", "new", "null", "object", "operator", "out", "override", "params", "private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof", "stackalloc", "static", "string", "struct", "switch", "this", "throw", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe", "elif" }; fontToSet = new Font(typingFont, FontStyle.Bold); currentTextArea.AddHighlightKeywords(keywords, Color.CornflowerBlue, fontToSet); //Keyword highlight another color var keywords2 = new List <string>() { "define", "error", "import", "undef", "include", "using", "ifdef", "line", "endif", "ifndef", "pragma" }; currentTextArea.AddHighlightKeywords(keywords2, Color.LightSeaGreen, typingFont); //Comment highlight var commentSymbols = new List <string>() { "//", "///", "////" }; currentTextArea.AddListOfHighlightDescriptors(commentSymbols, DescriptorRecognition.StartsWith, HighlightType.ToEOL, Color.Green, typingFont, UsedForAutoComplete.No); currentTextArea.AddHighlightDescriptor(DescriptorRecognition.StartsWith, "/*", HighlightType.ToCloseToken, "*/", Color.Green, typingFont, UsedForAutoComplete.No); //Highlight text between begin and end token var listPair = new List <string>() { "\"", "\"", "\'", "\'", }; currentTextArea.AddHighlightBoundaries(listPair, Color.Red, typingFont); //Highlight string start with '#' currentTextArea.AddHighlightDescriptor(DescriptorRecognition.StartsWith, "#", HighlightType.ToEOW, Color.LightSeaGreen, typingFont, UsedForAutoComplete.No); } break; case "C++": { //Number highlight currentTextArea.AddHighlightDescriptor(DescriptorRecognition.IsNumber, "", HighlightType.ToEOW, Color.IndianRed, typingFont, UsedForAutoComplete.No); //Comment highlight var commentSymbols = new List <string>() { "//", "///", "////" }; currentTextArea.AddListOfHighlightDescriptors(commentSymbols, DescriptorRecognition.StartsWith, HighlightType.ToEOL, Color.Green, typingFont, UsedForAutoComplete.No); currentTextArea.AddHighlightDescriptor(DescriptorRecognition.StartsWith, "/*", HighlightType.ToCloseToken, "*/", Color.Green, typingFont, UsedForAutoComplete.No); //Keyword highlight var keywords = new List <string>() { "asm", "auto", "bool", "break", "case", "catch", "char", "class", "const_cast", "continue", "default", "delete", "double", "else", "enum", "dynamic_cast", "extern", "false", "float", "for", "union", "unsigned", "using", "friend", "goto", "if", "inline", "int", "long", "mutable", "virtual", "namespace", "new", "operator", "private", "protected", "public", "register", "void", "reinterpret_cast", "return", "short", "signed", "sizeof", "static", "static_cast", "volatile", "struct", "switch", "template", "this", "throw", "true", "try", "typedef", "typeid", "unsigned", "wchar_t", "while" }; fontToSet = new Font(typingFont, FontStyle.Bold); currentTextArea.AddHighlightKeywords(keywords, Color.CornflowerBlue, fontToSet); //Highlight text between begin and end token var listPair = new List <string>() { "\"", "\"", "\'", "\'", }; //currentTextArea.AddHighlightBoundaries(listPair, Color.Red, typingFont); currentTextArea.AddHighlightDescriptor(DescriptorRecognition.StartsWith, "\"", HighlightType.ToCloseToken, "\"", Color.Red, typingFont, UsedForAutoComplete.No); //Highlight string start with '#' currentTextArea.AddHighlightDescriptor(DescriptorRecognition.StartsWith, "#", HighlightType.ToEOW, Color.SlateGray, typingFont, UsedForAutoComplete.No); } break; case "C": { //Number highlight currentTextArea.AddHighlightDescriptor(DescriptorRecognition.IsNumber, "", HighlightType.ToEOW, Color.IndianRed, typingFont, UsedForAutoComplete.No); //Keyword highlight var keywords = new List <string>() { "abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", "class", "const", "continue", "decimal", "default", "delegate", "do", "double", "else", "enum", "event", "explicit", "extern", "false", "finally", "fixed", "float", "for", "foreach", "goto", "if", "implicit", "in", "int", "interface", "internal", "is", "lock", "long", "namespace", "new", "null", "object", "operator", "out", "override", "params", "private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof", "stackalloc", "static", "string", "struct", "switch", "this", "throw", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe" }; fontToSet = new Font(typingFont, FontStyle.Bold); currentTextArea.AddHighlightKeywords(keywords, Color.CornflowerBlue, fontToSet); //Comment highlight var commentSymbols = new List <string>() { "//" }; currentTextArea.AddListOfHighlightDescriptors(commentSymbols, DescriptorRecognition.StartsWith, HighlightType.ToEOL, Color.Green, typingFont, UsedForAutoComplete.No); currentTextArea.AddHighlightDescriptor(DescriptorRecognition.StartsWith, "/*", HighlightType.ToCloseToken, "*/", Color.Green, typingFont, UsedForAutoComplete.No); //Highlight text between begin and end token var listPair = new List <string>() { "\"", "\"", "\'", "\'", }; //currentTextArea.AddHighlightBoundaries(listPair, Color.Red, typingFont); currentTextArea.AddHighlightDescriptor(DescriptorRecognition.StartsWith, "\"", HighlightType.ToCloseToken, "\"", Color.Red, typingFont, UsedForAutoComplete.No); //Highlight string start with '#' currentTextArea.AddHighlightDescriptor(DescriptorRecognition.StartsWith, "#", HighlightType.ToEOW, Color.SlateGray, typingFont, UsedForAutoComplete.No); } break; case "VB": { //Number highlight currentTextArea.AddHighlightDescriptor(DescriptorRecognition.IsNumber, "", HighlightType.ToEOW, Color.IndianRed, typingFont, UsedForAutoComplete.No); //Keyword highlight var keywords = new List <string>() { "addhandler", "addressof", "alias", "and", "andalso", "as", "boolean", "byref", "byte", "byval", "call", "case ", "catch", "cbool", "cbyte", "cchar", "cdate", "cdbl", "cdec", "char", "cint", "class", "clng", "cobj", "const", "continue", "csbyte", "cshort", "csng", "cstr", "ctype", "cuint", "culng", "cushort", "date", "decimal", "declare", "default", "delegate", "dim", "directcast", "do", "double", "each", "else", "elseif", "end", "endif", "enum", "erase", "error", "event", "exit", "false", "finally", "for", "friend", "function", "get", "gettype", "getxmlnamespace", "global", "gosub", "goto", "handles", "if", "implements", "imports", "in", "inherits", "integer", "interface", "is", "isnot", "let", "lib", "like", "long", "loop", "me", "mod", "module", "mustinherit", "mustoverride", "mybase", "myclass", "namespace", "narrowing", "new", "not", "nothing", "notinheritable", "notoverridable", "object", "of", "on", "operator", "option", "optional", "or", "orelse", "out", "overloads", "overridable", "overrides", "paramarray", "partial", "private", "property", "protected", "public", "raiseevent", "readonly", "redim", "rem", "removehandler", "resume", "return", "sbyte", "select", "set", "shadows", "shared", "short", "single", "static", "step", "stop", "string", "structure", "sub", "synclock", "then", "throw", "to", "true", "try", "trycast", "typeof", "uinteger", "ulong", "ushort", "using", "variant", "wend", "when", "while", "widening", "with", "withevents", "writeonly", "xor" }; fontToSet = new Font(typingFont, FontStyle.Bold); currentTextArea.AddHighlightKeywords(keywords, Color.CornflowerBlue, fontToSet); //Comment highlight var commentSymbols = new List <string>() { "//", "///", "////" }; currentTextArea.AddListOfHighlightDescriptors(commentSymbols, DescriptorRecognition.StartsWith, HighlightType.ToEOL, Color.Green, typingFont, UsedForAutoComplete.No); currentTextArea.AddHighlightDescriptor(DescriptorRecognition.StartsWith, "/*", HighlightType.ToCloseToken, "*/", Color.Green, typingFont, UsedForAutoComplete.No); //Highlight text between begin and end token var listPair = new List <string>() { "\"", "\"", "\'", "\'", }; //currentTextArea.AddHighlightBoundaries(listPair, Color.Red, typingFont); currentTextArea.AddHighlightDescriptor(DescriptorRecognition.StartsWith, "\"", HighlightType.ToCloseToken, "\"", Color.Red, typingFont, UsedForAutoComplete.No); //Highlight string start with '#' currentTextArea.AddHighlightDescriptor(DescriptorRecognition.StartsWith, "#", HighlightType.ToEOW, Color.SlateGray, typingFont, UsedForAutoComplete.No); } break; case "Javascript": { //Number highlight currentTextArea.AddHighlightDescriptor(DescriptorRecognition.IsNumber, "", HighlightType.ToEOW, Color.IndianRed, typingFont, UsedForAutoComplete.No); //Keyword highlight var keywords = new List <string>() { "abstract", "arguments", "await", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "debugger", "default", "delete", "do", "double", "elseenum", "eval", "export", "extends", "false", "final", "finally", "float", "for", "function", "goto", "if", "implements", "import", "in", "instanceof", "int", "interface", "let", "long", "native", "newnull", "package", "private", "protected", "public", "return", "short", "static", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "truetry", "typeof", "var", "void", "volatile", "while", "with", "yield", "abstract", "boolean", "byte", "char", "double", "final", "float", "goto", "int", "long", "native", "short", "synchronized", "throws", "transient", "volatile" }; fontToSet = new Font(typingFont, FontStyle.Bold); currentTextArea.AddHighlightKeywords(keywords, Color.CornflowerBlue, fontToSet); //Keyword highlight another color var keywords2 = new List <string>() { "Array", "Date, eval", "function", "hasOwnProperty", "Infinity", "isFinite", "isNaN", "isPrototypeOf", "length", "Math", "NaN", "name", "Number", "Object", "prototype", "String", "toString", "undefined", "valueOf" }; currentTextArea.AddHighlightKeywords(keywords2, Color.LightSeaGreen, typingFont); //Comment highlight var commentSymbols = new List <string>() { "//" }; currentTextArea.AddListOfHighlightDescriptors(commentSymbols, DescriptorRecognition.StartsWith, HighlightType.ToEOL, Color.Green, typingFont, UsedForAutoComplete.No); currentTextArea.AddHighlightDescriptor(DescriptorRecognition.StartsWith, "/*", HighlightType.ToCloseToken, "*/", Color.Green, typingFont, UsedForAutoComplete.No); //Highlight text between begin and end token var listPair = new List <string>() { "\"", "\"", "\'", "\'", }; currentTextArea.AddHighlightBoundaries(listPair, Color.Red, typingFont); } break; case "SQL": { //Number highlight currentTextArea.AddHighlightDescriptor(DescriptorRecognition.IsNumber, "", HighlightType.ToEOW, Color.IndianRed, typingFont, UsedForAutoComplete.No); //Keyword highlight var keywords = new List <string>() { "add", "except", "percent", "all", "exec", "plan", "alter", "execute", "precision", "and", "exists", "primary", "any", "exit", "print", "as", "fetch", "proc", "asc", "file", "procedure", "authorizationfillfactor", "public", "backup", "for", "raiserror", "begin", "foreign", "read", "between", "freetext", "readtext", "break", "freetexttable", "reconfigure", "browse", "from", "references", "bulk", "full", "replication", "by", "function", "restore", "cascade", "goto", "restrict", "case", "grant", "return", "check", "group", "revoke", "checkpoint", "having", "right", "close", "holdlock", "rollback", "clustered", "identity", "rowcount", "coalesce", "identity_insert", "rowguidcol", "collate", "identitycol", "rule", "column", "if", "save", "commit", "in", "schema", "compute", "index", "select", "constraint", "inner", "session_user", "contains", "insert", "set", "containstable", "intersect", "setuser", "continue", "into", "shutdown", "convert", "is", "some", "create", "join", "statistics", "cross", "key", "system_user", "current", "kill", "table", "current_date", "left", "textsize", "current_time", "like", "then", "current_timestamp", "lineno", "to", "current_user", "load", "top", "cursor", "national", "tran", "database", "nocheck", "transaction", "dbcc", "nonclustered", "trigger", "deallocate", "not", "truncate", "declare", "null", "tsequal", "default", "nullif", "union", "delete", "of", "unique", "deny", "off", "update", "desc", "offsets", "updatetext", "disk", "on", "use", "distinct", "open", "user", "distributed", "opendatasource", "values", "double", "openquery", "varying", "drop", "openrowset", "view", "dummy", "openxml", "waitfor", "dump", "option", "when", "else", "or", "where", "end", "order", "while", "errlvl", "outer", "with", "escape", "over", "writetext" }; fontToSet = new Font(typingFont, FontStyle.Bold); currentTextArea.AddHighlightKeywords(keywords, Color.CornflowerBlue, fontToSet); //Comment highlight var commentSymbols = new List <string>() { "//" }; currentTextArea.AddListOfHighlightDescriptors(commentSymbols, DescriptorRecognition.StartsWith, HighlightType.ToEOL, Color.Green, typingFont, UsedForAutoComplete.No); currentTextArea.AddHighlightDescriptor(DescriptorRecognition.StartsWith, "/*", HighlightType.ToCloseToken, "*/", Color.Green, typingFont, UsedForAutoComplete.No); //Highlight text between begin and end token var listPair = new List <string>() { "\'", "\'", }; currentTextArea.AddHighlightBoundaries(listPair, Color.Red, typingFont); } break; case "Normal Text": { TabControlMethods.CurrentTextArea.EnableHighlight = false; } break; default: break; } UpdateStatusBar(); TabControlMethods.CurrentTextArea.Refresh(); TabControlMethods.CurrentTabPageInfo.Language = language; }