IsReadOnly

Topics: User Forum
May 28, 2010 at 10:24 AM

The IsReadOnly property appears to apply to setting values via code as well as the user being able to modify via the GUI. Is this intentional or am I missing something?

Cheers,

Dave

Coordinator
Jun 1, 2010 at 4:35 AM

You are correct.

I assume what you find odd is why the IsReadOnly value affects programmatic access as well as user access. I honestly don't get that either. I would much prefer that it would behave like a TextBox or RichTextBox ReadOnly property (programmatic access is still allowed)... but alas it does not.

 

Thanks
Jacob

 

Coordinator
Jun 1, 2010 at 7:58 AM

Agreed, it seems to me like it's another artifact of Scintilla's poor document/view separation. Most people would think the view would be readonly but in this case it's the document. I played with this for a bit and couldn't really come up with a good way of faking the desired behavior by modifying ScintillaNet. You can either try your hand at editing Scintilla to make the SCI_SETREADONLY message work the way you want or do something like this:

 

public class ReadOnlyBypass : IDisposable
{
    private Scintilla scintilla;
    private bool originalIsReadOnly;

    public ReadOnlyBypass(Scintilla scintilla)
    {
        this.scintilla = scintilla;
        this.originalIsReadOnly = scintilla.IsReadOnly;
        scintilla.IsReadOnly = false;
    }

    void IDisposable.Dispose()
    {
        scintilla.IsReadOnly = originalIsReadOnly;
    }
}

/// <summary>
/// I oh so love abusing extension methods...
/// </summary>
public static class ReadOnlyBypassScintillaExtension
{
    public static IDisposable ReadOnlyBypass(this Scintilla scintilla)
    {
        return new ReadOnlyBypass(scintilla);
    }
}

 

And its usage:

private void button1_Click(object sender, EventArgs e)
{
    using (var readOnlyBypass = scintilla.ReadOnlyBypass())
    {
        scintilla.Text = "I can make this change";
        scintilla.AppendText("\r\nAnd this too");
    }
}

This solution isn't perfect, there may be other event handlers that may set/unset the IsReadOnly property that you aren't aware of. But this should work fine in relatively simple situations and is only a relatively small PITA.

Jun 1, 2010 at 8:04 AM

Many thanks for the replies. I am using the control as a custom cell editor on a grid control and, luckily, there are "before enter edit" and "after enter edit" events on the control so I managed to clear/set IsReadOnly in those events. I'll certainly bear the ReadOnlyBypass method in mind.

Cheers,

Dave