How to enable copy paste on disabled TextBox

Nil Pun

I've a two way data binding set up for Title Text box on win form as below:

txtTitle.DataBindings.Add("Enabled", Person, "Editable", true, DataSourceUpdateMode.OnPropertyChanged);

Unfortunately, when text is Disabled users can't copy the text. Is there any workaround to enabling the COPY/PASTE preserving two way data bind?

Measuring

A textbox control has a ReadOnly property which you can set to true. Binding should still be updated.

Option 1: Create a class with NotifyPropertyChanged

Create a class holding the data that will be bound:

public class Book : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string name;
    [Bindable(true)]
    public string Name
    {
        get { return name; }
        set
        {
            name = value; 
            if (PropertyChanged != null) 
                PropertyChanged(this, 
                                new PropertyChangedEventArgs("Name"));
        }
    }

    public Book(string name)
    {
        Name = name;
    }
}

Create an instance of this class and bind it:

public Book TheBook { get; set; }

public Form1()
{
    InitializeComponent();

    TheBook = new Book("");
    textBox1.DataBindings.Add(new Binding("Text", 
                                          TheBook,
                                          "Name",
                                          true,
                                          DataSourceUpdateMode.OnPropertyChanged, 
                                          ""));
}

Change the property and it will update:

private void button1_Click(object sender, EventArgs e)
{
    TheBook.Name = "Different";
}

Option 2: Create a custom control to mask Enabled as ReadOnly

Create the following custom control and use that for binding to Enabled:

public partial class DBTextBox : TextBox
{
    private bool enabled;
    public new bool Enabled
    {
        get { return enabled; }
        set
        {
            enabled = value;
            base.Enabled = true;
            ReadOnly = !enabled;
        }
    }

    public DBTextBox()
    {
        InitializeComponent();
    }
}

Whenever that DBTextBox is set to Enabled = false it will make it ReadOnly instead.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to enable copy paste on disabled TextBox

From Dev

How to enable cut or copy-paste in xterm

From Dev

How to enable cut or copy-paste in xterm

From Dev

How to enable a disabled textbox from mouse click vb.net

From Dev

How to replace (copy paste) non alpha characters in the textbox

From Dev

copy to clipboard from disabled Textbox

From Dev

copy to clipboard from disabled Textbox

From Dev

How can enable the "Copy-paste" menu in EditText?

From Dev

How to enable shortcuts (copy-paste) inside chrooted java app?

From Dev

How can enable the "Copy-paste" menu in EditText?

From Dev

Enable copy and paste in to HTML fields

From Dev

How to enable copy when textbox.enabled is false?

From Dev

javascript enable a disabled textbox after a while

From Dev

javascript enable a disabled textbox after a while

From Dev

Exclude input from disabled copy and paste jQuery

From Dev

Disable Copy/Paste/Right Click in mvc TextBox

From Dev

Enable copy/paste on bare Linux inside VMware

From Dev

Enable copy/paste in vim on bare Linux

From Dev

How to copy and paste a file?

From Dev

How to copy and paste a file?

From Dev

How to enable copy paste from between host machine and virtual machine in vmware, virtual machine is ubuntu

From Dev

How to enable a disabled checkbox dynamically?

From Dev

Disabled Cortana, how to enable it now?

From Dev

Disabled copy and paste after adding wordcount plugin of Ckeditor

From Dev

How to enable textbox in a particular row?

From Dev

How to copy and paste with a mouse with tmux

From Dev

How to disallow copy/paste in a form?

From Dev

How to copy, paste, cut in FastColoredTextBox?

From Dev

How to hide copy/paste popup

Related Related

HotTag

Archive