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:
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.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()
[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:
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: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()
[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:
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:Code:My.Computer.Clipboard.ContainsAudio() My.Computer.Clipboard.ContainsData() My.Computer.Clipboard.ContainsFileDropList() My.Computer.Clipboard.ContainsImage() My.Computer.Clipboard.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![]()
Well done! +rep
Nice +rep
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks