We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to implement Visual Studio like read-only file handling behavior in WinForms SyntaxEditor (EditControl)?

Platform: WinForms |
Control: EditControl

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




2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile