Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy Text from text box with button onclick event

I like to copy the text of the Textbox when user click button1, so that it can be paste anywhere.

I searched on google for some solutions, but didn't get any positive response.

Anybody advice me to how perform this action?

like image 915
Amrit Sharma Avatar asked Jun 14 '13 08:06

Amrit Sharma


3 Answers

You can use like this one:

private void btnCopy_Click(object sender, EventArgs e)
{
    Clipboard.SetText(txtClipboard.Text);
}
private void btnPaste_Click(object sender, EventArgs e)
{
    txtResult.Text = Clipboard.GetText();
}
like image 114
Tiny Hacker Avatar answered Oct 11 '22 14:10

Tiny Hacker


You wish to copy text to the clipboard. The basic syntax is:

Clipboard.SetText("The text you want to copy");

But in order for it to work there is more work to put into it, use the links I provided. You can find further information here and here for c# and here for ASP.net which is more relevant for you.

This code was taken from CodeProject link aformentioned, and should work by using different thread.

private static string _Val;
public static string Val
{
    get { return _Val; }
    set { _Val = value; }
}
protected void LinkButton1_Click(object sender, EventArgs e)
{            
    Val = label.Text;
    Thread staThread = new Thread(new ThreadStart (myMethod));
    staThread.ApartmentState = ApartmentState.STA;
    staThread.Start();
}
public static void myMethod()
{
    Clipboard.SetText(Val);
}
like image 36
avi.tavdi Avatar answered Oct 11 '22 16:10

avi.tavdi


Clipboard.SetText(textBox1.Text.ToString()); Everyone forgot to tell you about .ToString() method. That works 100%

like image 42
Sasha Xen Promychev Avatar answered Oct 11 '22 14:10

Sasha Xen Promychev