-3

I am using a Clipboard.SetText(textbox1.Text); in order to copy the contents of a text box to my clipboard, but I want to be able to copy the entire form text as well.

I tried using Clipboard.SetText(FrmMain.Text);

but it is not working, anyone have any thoughts or am i going about this the wrong way?

Jrobbie
  • 9
  • 6
  • 6
    What is "entire form text"? – Dmitrii Dovgopolyi Sep 13 '13 at 12:06
  • Do you want to copy from multiple text boxes? – huMpty duMpty Sep 13 '13 at 12:12
  • I guess what you need is manually concatenating the contents of your form? – olydis Sep 13 '13 at 12:14
  • in other words I want to copy all text on the form, all labels, text boxes and combo boxes to paste formatted the same way as the form...this is for ticket notation when i document changes and repairs made. – Jrobbie Sep 13 '13 at 12:16
  • 1
    Sounds to me like you want to take a screenshot of the active window: http://stackoverflow.com/questions/1163761/c-sharp-capture-screenshot-of-active-window and copy the resulting image to the clipbard: https://www.google.nl/search?q=c%23+copy+picture+to+clipboard&oq=c%23+copy+picture+to+clipboard&aqs=chrome..69i57j69i58.4486j0&sourceid=chrome&ie=UTF-8 – Maarten Sep 13 '13 at 12:32

3 Answers3

1

Add the following logic to a private method and call this method within button click handler.

StringBuilder text = new StringBuilder();

foreach (Control item in Controls)
 {
      if (item is TextBox)
        {
           text.Append(item.Text);
           text.Append(',');
        }
 }

Clipboard.SetText(text.ToString());

EDIT

Without checking if the control type is TextBox you can get only textbox as follows,

foreach (Control item in Controls.OfType<TextBox>())
{    
   text.Append(item.Text);
   text.Append(',');                
}
Kurubaran
  • 8,696
  • 5
  • 43
  • 65
0

if the text from the form is static, you could always add it as a string to your statement

Clipboard.SetText("Your String" + Textbox1.Text);
DJ Olker
  • 60
  • 7
0

You should create your own method for getting the string from all controls.

private string GetFormText()
{
    StringBuilder sb = new StringBuilder();
    foreach (Control control in this.Controls)
    {
        if (control.GetType() == typeof (TextBox) ||
            control.GetType() == typeof (ComboBox) ||
            control.GetType() == typeof (Label))
        {
            String controlText = String.Format("{0}:{1}", control.Name, control.Text);
            sb.AppendLine(controlText);
        }
    }
    return sb.ToString();
}

then you can save your form text like this:

Clipboard.SetText(this.GetFormText());
Dmitrii Dovgopolyi
  • 6,231
  • 2
  • 27
  • 44