+ Reply to Thread
Results 1 to 3 of 3

Thread: The Clipboard - VB.NET

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    The Clipboard - VB.NET

    The clipboard contains info what you have copied(or cut) and is what will be added when you paste something. In VB.NET we can both alter and read this info. We'll found all clipboard functions in My.Computer.Clipboard.






    Set

    My.Computer.Clipboard.Set... is used when changing the data in the clipboard, this will work as you would have copied/cut the desired data. You have to define with datatype the data you want to apply is, the one that exists is:

    Code:
            My.Computer.Clipboard.SetAudio()
            My.Computer.Clipboard.SetData()
            My.Computer.Clipboard.SetDataObject()
            My.Computer.Clipboard.SetFileDropList()
            My.Computer.Clipboard.SetImage()
            My.Computer.Clipboard.SetText()
    Note that SetData could be any data type, when using it you need to set which data type you want to add. This means SetData will have two parameters instead of one which is the number for the other ones. The parameter they use is the data you want to add(SetAudio has two different ways of doing this). Here below comes an example on how to use SetText to store a string in the Clipboard.


    [highlight=VB.NET] My.Computer.Clipboard.SetText("This will be showed if you use Ctr-V")[/highlight]

    And another example using SetImage:

    [highlight=VB.NET] My.Computer.Clipboard.SetImage(Me.BackgroundImage)[/highlight]








    Get

    My.Computer.Clipboard.Set... is used to read the info from the clipboard, this will work a bit like using paste. When using it you have to choose which data type to use, you can choose one of these:


    Code:
           My.Computer.Clipboard.GetAudioStream()
            My.Computer.Clipboard.GetData()
            My.Computer.Clipboard.GetDataObject()
            My.Computer.Clipboard.GetFileDropList()
            My.Computer.Clipboard.GetImage()
            My.Computer.Clipboard.GetText()
    Note that GetData uses one parameter to set which data type you want here too. The different is that this is the only one with a parameter at all since these ones will just give you the values. An example of doing this with GetText is showed below:


    [highlight=VB.NET]MessageBox.Show("The value in your clipboard is: " & My.Computer.Clipboard.GetText)[/highlight]


    And another one with GetImage:


    [highlight=VB.NET] Me.BackgroundImage = My.Computer.Clipboard.GetImage[/highlight]








    Contains

    A problem with reading the clipboard is that we don't know what data type the clipboard contains of. To check this we could use My.Computer.Clipboard.Contains... for the data types we want to check, the return value will be a boolean value which will tell us if the clipboard contains the special data type or not. The data types that we can check is:



    Code:
            My.Computer.Clipboard.ContainsAudio()
            My.Computer.Clipboard.ContainsData()
            My.Computer.Clipboard.ContainsFileDropList()
            My.Computer.Clipboard.ContainsImage()
            My.Computer.Clipboard.ContainsText()
    Note that here again the ContainsData is used together with a parameter telling it which format we want to use, also note that there's nothing called ContainsDataObject. A simple example showing how to use ContainsText:

    [highlight=VB.NET] If My.Computer.Clipboard.ContainsText() = True Then
    MessageBox.Show("The Clipboard contains a String value")
    Else
    MessageBox.Show("The Clipboard doesn't contains a String value")
    End If[/highlight]

    And a little more advanced example using GetText, GetImage, ContainsText and ContainsImage:


    [highlight=VB.NET] If My.Computer.Clipboard.ContainsText() = True Then
    Me.Text = My.Computer.Clipboard.GetText
    ElseIf My.Computer.Clipboard.ContainsImage Then
    Me.BackgroundImage = My.Computer.Clipboard.GetImage
    Else
    MessageBox.Show("The Clipboard contains an invalid data type")
    End If[/highlight]







    Clear

    You can also use My.Computer.Clipboard.Clear to clear all data(regardless of the data type) from the clipboard.






    That was everything you can do with the clipboard in VB.NET. Hope you learned something new and have a happy time coding

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: The Clipboard - VB.NET

    Well done! +rep

  4. #3
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: The Clipboard - VB.NET

    Nice +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Clipboard Game
    By Jordan in forum Games
    Replies: 3626
    Last Post: 01-01-2012, 03:50 AM
  2. Intercepting Clipboard events
    By mnirahd in forum C and C++
    Replies: 1
    Last Post: 02-01-2011, 12:49 PM
  3. Copy To Clipboard Help - Probably easy fix...
    By Souluna in forum JavaScript and CSS
    Replies: 1
    Last Post: 07-07-2009, 05:16 AM
  4. Clipboard
    By dirkfirst in forum C# Programming
    Replies: 3
    Last Post: 07-14-2006, 10:06 AM

Tags for this Thread

Bookmarks

Posting Permissions

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