Copy to Clipboard-VBForums
Results 1 to 13 of 13

Thread: Copy to Clipboard

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    55

    Copy to Clipboard

    Hello guys.

    I have an issue when I try to copy data to clipboard.
    I have 4 textboxes and I want all data from these 4 textboxes to be copied to clipboard.
    I wrote these few line of code, but I manage to copy the data from the first textbox only.
    Obviously, I am missing something...


    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    Dim J as control
    
     For Each Me.J In Me.Controls
                If TypeOf J Is TextBox Then
                    If J.Text <> "" Then
                        Clipboard.SetText(J.Text)
                    End If
                End If
            Next J
    
    End Sub

  2. #2
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    404

    Re: Copy to Clipboard

    you need to combine all of the values first before setting the value of the clipboard like below

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    Dim J as control
    Dim sb As Text.StringBuilder = New Text.StringBuilder()
     For Each Me.J In Me.Controls
                If TypeOf J Is TextBox Then
                    If J.Text <> "" Then
                        CombinedValue.Append(J.Text & Environment.NewLine)
                    End If
                End If
            Next J
    Clipboard.SetText(CombinedValue.ToString)
    End Sub

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,199

    Re: Copy to Clipboard

    Alternatively you could also use String.Concat:
    Code:
    Clipboard.SetText(String.Concat((From txt As TextBox In Me.Controls.OfType(Of TextBox)() Select txt.Text)))

  4. #4
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    404

    Re: Copy to Clipboard

    LINQ - what horrible stuff!

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,199

    Re: Copy to Clipboard

    Quote Originally Posted by Leary222 View Post
    LINQ - what horrible stuff!
    Sorry that it's not backwards compatible and requires forward thinking, but considering that LINQ just cut down your 10 lines down to 1... what makes this particular example horrible?

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    55

    Re: Copy to Clipboard

    @Leary222 & dday9
    Doesn't seem to work.
    I get these errors

    Error 2 'CombinedValue' is not declared. It may be inaccessible due to its protection level
    Error 1 'Text' is ambiguous, imported from the namespaces or types 'System, System.Drawing'.

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,199

    Re: Copy to Clipboard

    Quote Originally Posted by stratos View Post
    @Leary222 & dday9
    Doesn't seem to work.
    I get these errors

    Error 2 'CombinedValue' is not declared. It may be inaccessible due to its protection level
    Error 1 'Text' is ambiguous, imported from the namespaces or types 'System, System.Drawing'.
    With my example, are you accidentally using:
    Code:
    Clipboard.SetText(String.Concat((From text As TextBox In Me.Controls.OfType(Of TextBox)() Select text.Text)))
    If so then I think that could cause the error. I'm not using a variable named CombinedValue in my example so I'm assuming that is Leary222's error which it appears to be a typo, replace CombinedValue with sb.

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    55

    Re: Copy to Clipboard

    Seems to work now but there is still an issue.

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    Dim J as control
    
     For Each Me.J In Me.Controls
    
                If TypeOf J Is TextBox Then
                    If J.Text <> "" Then
                        Clipboard.SetText(String.Concat((From txt As TextBox In Me.Controls.OfType(Of TextBox)() Select txt.Text)))                
                    End If
                End If
            Next J
    
    End Sub
    All data are copied but they are all stacked in one line with no Spaces and backwards

    (i.e. Let's say that I input these data in 4 textboxes: 23.5 12 90.6 56.1.
    The code export that "56.190.61223.5")
    Last edited by stratos; Apr 26th, 2016 at 12:26 PM.

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,199

    Re: Copy to Clipboard

    You do not need to include all the looping mess, that is why I used LINQ in the first place. If it is backwards then you can reverse the array. If you want to include a space between each TextBox then use String.Join instead:
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Clipboard.SetText(String.Join(" ", (From txt As TextBox In Me.Controls.OfType(Of TextBox)() Select txt.Text).Reverse()))
    End Sub

  10. #10

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    55

    Re: Copy to Clipboard

    Thanks! It's fine now.

  11. #11
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    404

    Re: Copy to Clipboard

    Quote Originally Posted by dday9 View Post
    Sorry that it's not backwards compatible and requires forward thinking, but considering that LINQ just cut down your 10 lines down to 1... what makes this particular example horrible?
    Sorry dday, now realize that my comment is unproductive.

    personally i dont like the idea of 'unmannged code ' in the sense that it looks a bit like sql and visual studio wont highlight any syntax errors with it and its probably converted back to something similar to my code after the overhead of parsing your string.

  12. #12
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,199

    Re: Copy to Clipboard

    personally i dont like the idea of 'unmannged code ' in the sense that it looks a bit like sql
    You do not have to use a SQL style syntax, you can use this in its place:
    Code:
    Clipboard.SetText(String.Join(" ", Me.Controls.OfType(Of TextBox)().Select(Function(txt) txt.Text).Reverse()))
    However, I simply prefer the SQL style syntax for queries such as this.

    visual studio wont highlight any syntax errors with it
    What version of Visual Studios are you using?! Since its release of LINQ, I have never had an instance where Visual Studios did not pick up a syntax error. Now as of Visual Studios 2015 they've added more extensive support for LINQ with IntelliSense as well as the ability for the debugger to debug expressions where as in in previous versions that was not available.

    its probably converted back to something similar to my code after the overhead of parsing your string.
    There is no doubt that you will take a performance hit when using LINQ. What you need to do is balance the relationship between the need to update/maintain code and readability vs. straight performance and this is exactly a case where I would justify using a one line LINQ statement that queries all TextBox controls located on a Form and returning their Text over having to iterate through each control, checking if the Type is a TextBox, checking if the Text is not empty, and then appending the Text to a StringBuilder.

    Just for Schlitz-and-Giggles I've decided to do a performance test. When you ran both a LINQ and StringBuilder examples as provided by both of us the amount of time that it took to iterate through 10 arbitrary types it took 0 milliseconds. When I did the same test only repeating the examples 10,000 times LINQ took 20 milliseconds and StringBuilder took 10 milliseconds. My point is that the user will only be doing this one at a time, not repeatedly so the performance hit doesn't matter.

    Here is a fiddle of the test that I did: https://dotnetfiddle.net/auuuJs

  13. #13
    Hyperactive Member
    Join Date
    Sep 2014
    Posts
    404

    Re: Copy to Clipboard

    Ha - unsupprisingly i stand corrected.....

    i thought thats linq was passed like "do linq" - not sure why i thought that probably on the basis id never used it because i didn't like the look of it.

    i now see VS does offer syntax support - because its not a string like i thought it was.

    i much prefer the alternative approach you posted - thanks for broadening my mind lol

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.