Articles in this section
Category / Section

How to implement Visual Studio like read-only file in WinForms Editor?

3 mins read

Keydown event

VS.NET unlike most of other editors allows editing of read-only files. It only prompts the user during the save operation that the file is read-only and the read-only attribute needs to be overridden in order to save the changes. Based on the user input, the changes are either lost or the read-only attribute is overridden, and the changes are saved. By default, the EditControl does not allow editing of read-only files. All keystrokes are simply ignored when a read-only file is loaded into the EditControl.

The VS.NET like behavior can be implemented in the EditControl by handling its KeyDown event as shown in the code snippets below.


private void editControl1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    if ((e.Control == true) || (e.Alt == true))
        // Do nothing
        // Check if its a ReadOnly file
        if ((File.GetAttributes(filePath) & FileAttributes.ReadOnly) != 0)
            string message = "Do you wish to override the read-only attribute and perform modifications ?";
            string caption = "ReadOnly file opened - unable to make modifications";
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult result = MessageBox.Show(this, message, caption, buttons);
            if (result == DialogResult.Yes)
                FileInfo fileInfo = new FileInfo(filePath);
                fileInfo.Attributes -= FileAttributes.ReadOnly;
            else if (result == DialogResult.No)
                // Do nothing



Private Sub editControl1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles editControl1.KeyDown
    If e.Control = True OrElse e.Alt = True Then
       ' Do nothing
       ' Check if its a ReadOnly file
       If(File.GetAttributes(filePath) And FileAttributes.ReadOnly) <> 0 Then
           Dim message As String = "Do you wish to override the read-only attribute and perform modifications ?"
           Dim caption As String = "ReadOnly file opened - unable to make modifications"
           Dim buttons As MessageBoxButtons = MessageBoxButtons.YesNo
           Dim result As DialogResult = MessageBox.Show(Me, message, caption, buttons)
           If result = DialogResult.Yes Then
              Dim fileInfo As New FileInfo(filePath)
              fileInfo.Attributes -= FileAttributes.ReadOnly
           ElseIf result = DialogResult.No Then
              ' Do nothing
           End If
       End If
    End If
End Sub 'editControl1_KeyDown




I hope you enjoyed learning about how to implement Visual Studio like read-only file handling behavior in WinForms SyntaxEditor (EditControl).

You can refer to our WinForms Editor feature tour
page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms Editor example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied