Tek Eye Logo

Tek Eye

Text File Edit Component for .NET WinForms Applications

Here we show you how to add a lightweight .NET C# text editor component to a Windows Forms application (WinForms). The control supports color coding when editing source files (based on the file extension). This C# text editor control comes with example code in a Visual Studio project. Itching to try it? A compiled DLL, source code and demo program is available in the TFEdit.zip file.

A WinForms .NET Control to Add Text and Source Editing to Windows Applications

This component is compatible with version 2.0 of the .NET framework and higher, so it is suitable for use on older versions of Windows, such as Windows XP (update: Microsoft no longer supports Windows XP). The control is called Text File Edit (TFEdit for .NET) and comes as a single DLL (Dynamic Link Library), named TextFileEdit.dll. It has several useful properties including turning the display of tabs and spaces on and off, EOL (end-of-line) marker support, showing line numbers and vertical and horizontal rulers. The component is from an Open Source project so all the code is available to change and improve. It comes with a small demonstration project to test it out, here is the component in action in the demo project:

Text File Editing in C#

Background

A while back (many years ago) a C# Windows project required a simple text editor to allow users of a program to edit text based files and log files without needing to switch to another application (such as Notepad or Word). The editing capability was a small part of the requirements. At the time the use of the .NET text box did not provide enough functionality. The Rich Text Format control was not suitable. All commercial editors were two complex and added costs to the project. The answer was to extract the text editor portion of a Open Source IDE (Integrated Development Environment) project called SharpDevelop. The SharpDevelop component was called ICSharpCode.TextEditor.dll. This component was refactored to simplify the namespace to TextFileEdit, and a simple test project was written. This was then published on a previous web site where it sat for a few years before the web site was repurposed. With the release of a new version of Visual Studio Express (verison 2012) it was decided to see how well it handled older controls. So the Text File Edit DLL was dusted down and given a go, it still worked.

Update: The free version of Visual Studio is now Visual Studio Community.

Download and Run the Example C# Text Editor

You can download the Text File Edit DLL, source code and a demo test project using the TFEdit.zip file. Extract the contents to a folder on your computer. If you are using Visual Studio Community (or the older Express) create a folder, e.g. call it TFEdit, in the Visual Studio Projects directory. Projects is normally in the Visual Studio directory in the Documents folder. Open the solution (T_F_E_Solution.sln), the solution was created in Visual Studio 2008 so you will see a conversion report for later editions of Visual Studio. Set the TFETest project as the start-up. I.e. select Set as StartUp Project from the context menu (usually right-click) in the Solution Explorer. The project should compile and run.

Update: Although the source code and solution file for this code is nearly ten years old it was recently tried in Visual Studio Community 2015. The project was opened and converted and executed successfully by Visual Studio Community 2015.

Use the Text File Edit Control in Your Projects

Text Edit Component Added to Visual Studio

The TextFileEdit.dll is in TextFileEdit\Project\bin\Release. With Visual Studio running start (or open) a Windows Forms Application project. Open the Toolbox (use the View menu if neccessary) and drag and drop the TextFileEdit.dll onto a tab, e.g. the General tab or create a new tab to hold the control. With a WinForm open in design mode drag the TextEditorControl from the Toolbox onto the form. It can then be positioned and properities set.

dotNet Edit Control

The ZIP file that contains the source code has more detailed instructions on adding TFEdit to a project, although it was written sometime ago for Visual Studio 2008.

Text File Edit .NET Control Properties

The TFEdit control supports the following properties:

  • AllowCaretBeyondEOL – When set to "true" the caret (typing cursor) can be moved beyond the end of the actual line. When it goes beyond the last character it is in a screen position and not in any text. Pressing the Return (Enter) key fills the line with spaces up to the point the caret has been moved and creates a new line.
  • BracketMatchingStyle – When set to "After" then pairs of brackets, () or [] or {}, are highlighted when the caret moves past them. When set to "Before" the brackets are highlighted as the caret approaches them.
  • ConvertTabsToSpaces – When set to "true" and a tab is typed, the tab character is not placed into the text. Instead spaces are placed into the text, the number of spaces being equal to the "TabIndent" property.
  • HideMouseCursor – When set to "true" the mouse cursor is hidden during typing.
  • IndentStyle – When set to "Auto" the text on the next line will start at the same postion as the current text. The same number of tabs and spaces are added to the new line that preceed the current line. When set to "None" no indentation occurs.
  • IsIconBarVisible – Shows a vertical bar on the left hand side on to which icons can be programmatically placed on a line by line basis.
  • LineViewerStyle – When set to "FullRow" the line that has the focus for typing is highlighted. When set to "None" no highlight is present on the current line.
  • ShowEOLMarkers – When set to "true" a light marker (pilcrow character) is shown to indicate where the end of the line (carriage-return and line-feed character pair) will occur in the text file.
  • ShowHRuler – When set to "true" a "ruler" is drawn at the top of the text, within the control bounds, to give a visual indication of the columns.
  • ShowInvalidLines – When set to "true" the space below the last line will have a marker to show the additional lines that can fit into the area. This markers just indicate the position of additonal lines and are not lines present in the file to be saved.
  • ShowLineNumbers – When set to "true" line numbers appear in a margin at the left of the control within the control boundaries.
  • ShowSpaces – When set to "true" a light dot will indicate a "space" character.
  • ShowTabs – When set to "true" a tab character in the text is indicated by a light right pointing double angle character.
  • ShowVRuler – When set to "true" a vertical line is drawn down the control. It will be positioned at the "text column" indicated by the property "VRulerRow".The vertical line can be used as a visual guide to determine the length of lines. Sometimes it is necessary to limit the length of lines in a text file for some systems, usually to 80 or 120 characters.
  • TabIndent – This property determines the number of character columns to indent when a tab is pressed or a tab is present in the text.
  • VRulerRow – This property determines the character column in which the vertical ruler line will be displayed.

Issues

  • When editing source code files the highlighting of paired brackets does not always occur.

Future Releases

There are no plans to release future versions of this control. The source code is available under the LGPL licence for you to make any required changes. The code was taken from an early version of the SharpDevelop IDE. Whilst the text editor in SharpDevelop has changed several times over various SharpDevelop releases TFEdit has not. The current SharpDevelop text editor is a WPF (Windows Presentation Foundation) component called AvalonEdit, it can be loaded into a WPF solution using the Manage NuGet Packages for Solution option from the NuGet Package Manager off the Tools menu. Search for AvalonEdit. (To install NuGet select Extensions and Updates from the Tools menu in Visual Studio Community or Express. Select Online in the left hand panel and then NuGet Package Manager, download and install.)

Summary

TFEdit for .NET is a text editor component for WinForms applications. Although many years old it runs happily on the latest versions of Visual Studio and .NET Framework. Orignally TFEdit was compiled against version 2.0 of the .NET framework so will run on older Windows platforms. In theory it will run across several Operating Systems using Mono but that has not been tried. A compiled DLL, source code and demo program is available in the TFEdit.zip file. It is licensed under LGPL.

Archived Comments

Rich Stephens on April 14, 2015 at 10:36 pm said:

Hi, I saw your control. I was wondering if you had any hints as to how best to implement a "Find" function using your control? You know, the standard "Ctrl-F Find, F3 Find Next" kind of thing.

It’s really more about how to access the inner document and highlight the found text, and keep track of where I am in the document for the "Find Next" feature.

Any help would be appreciated.

John Pool on June 22, 2017 at 5:58 am said:

Good morning Tek Eye.

Thank you for this component, it was exactly what I was looking for.

Kind regards,

John Pool, Amersfoort, Netherlands

See Also

  • View the Tek Eye full Index for other articles.

Author:  Published:  Updated:  

ShareSubmit to TwitterSubmit to FacebookSubmit to Google+Submit to LinkedInSubmit to redditPrint Page

markdown CMS Small Logo Icon ↓markdown↓ CMS is fast and simple. Build websites quickly and publish easily. For beginner to expert.



Articles on:

Android, HTML, VPS, Computing, IT, Computer History, ↓markdown↓ CMS



Free Android Projects and Samples:

Android Examples, Android List Examples, Android UI Examples