8

I have a TextBox control and want to be able to copy content of TextBox.

Properties of TextBox look like this;

textBox1.Enabled = false;
textBox1.ReadOnly = false;

I cannot copy content of textBox1 even though ReadOnly property is false.

Is there any suggestion?

Mehmet Ince
  • 4,059
  • 12
  • 45
  • 65

4 Answers4

13

Ýou may try this if you want the user to allow copy paste:

textBox1.ReadOnly = true;

From MSDN forum

In the context of a TextBox, readonly allows the user to set focus to and select and copy the text but not modify it. A disabled TextBox does not allow any interaction whatsoever.

Use ReadOnly when you have data that you want the user to see and copy, but not modify. Use a disabled textbox, when the data you are displaying is not applicable in for the current state of a dialog or window.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • 2
    @MehmetInce:- Also to add:- When you make a control as **disabled** it cannot be edited and its content is excluded when the form is submitted. When you make as control is **readonly** it cannot be edited, but its content (if any) is still included with the submission. – Rahul Tripathi Mar 04 '14 at 09:55
  • Couple tips on readonly textbox+copy to clipboard: 1: You can also override the background color to make it white again/etc. 2: I also recommend select all on a 'onclick' event. So data copied is highlighted. Check out https://stackoverflow.com/a/2151429/503621 – B. Shea Feb 23 '18 at 18:18
5

You should set your textboxes to ReadOnly = true instead of Enabled = false if you want to support copy/paste.

Fedor Hajdu
  • 4,657
  • 3
  • 32
  • 50
4
textBox1.ReadOnly = true;

you can even use a copy button and code as follows:

System.Windows.Forms.Clipboard.SetText(textBox1.Text);
Mark Fenech
  • 1,358
  • 6
  • 26
  • 36
1
 <input type="text" id="txtMobileNo" runat="server" onkeypress="return onlyNos(event,this);" class="form-control input-sm m-bot15" readonly="readonly" maxlength="10" style="font-weight: bold; background-color: #ecf9ec" tabindex="0" />

use readonly="readonly" in textbox code

Code
  • 679
  • 5
  • 9