2X faster development
The ultimate WinForms UI toolkit to boost your development speed.
Keydown eventVS.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. C# private void editControl1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if ((e.Control == true) || (e.Alt == true)) { // Do nothing } else { // 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; this.editControl1.Close(); this.editControl1.LoadFile(filePath); } else if (result == DialogResult.No) { // Do nothing } } } }
VB 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 Else ' 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 Me.editControl1.Close() Me.editControl1.LoadFile(filePath) 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.
This page will automatically be redirected to the sign-in page in 10 seconds.