Clipboard runtime error. Please help

blumonde

Well-known member
Joined
Jul 26, 2005
Messages
68
Programming Experience
Beginner
I ran into an error below when vb executing the dim statement below:
Dim iData As IDataObject = Clipboard.GetDataObject()


An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in system.windows.forms.dll

Additional information: The requested clipboard operation failed.


Could someone please tell me how to fix it.

Thanks.

blumonde
 
Have you copied the contents of the textbox (or any source you have there) to the clipboard?

VB.NET:
[color=#008080]Clipboard[/color].SetDataObject(TextBox1.Text) 'i'm assuming that textbox1 is current source

Now you can go for getDataObject ... for instance

VB.NET:
[/color][/color]
[color=#0000ff][color=#008000]'Declare an IDataObject to hold the data returned from the [/color][color=#008000]clipboard.[/color]
[color=#0000ff]Dim[/color][color=#000000] iData [/color][color=#0000ff]As[/color][/color][color=#000000] IDataObject
[/color]
[color=#0000ff]With[/color] [color=black]objTemp[/color]
[color=black].[/color][color=black]Content[/color][color=black].[/color][color=black]Paste[/color][color=black]()[/color]
[color=black].[/color][color=black]Activate[/color][color=black]()[/color]
[color=#0000ff]{...}[/color]
[color=black]iData[/color] = [color=blue]Clipboard[/color][color=black].[/color][color=black]GetDataObject[/color]
[color=#0000ff]If[/color] [color=black]iData[/color][color=black].[/color][color=black]GetDataPresent[/color][color=black]([/color][color=blue]DataFormats[/color][color=black].[/color][color=black]Text[/color][color=black]) [/color][color=#0000ff]Then[/color]
[color=black]TextBox1[/color][color=black].[/color][color=black]Text[/color] [color=#804040]=[/color] [color=#000080]CType[/color][color=black]([/color][color=black]iData[/color][color=black].[/color][color=black]GetData[/color][color=black]([/color][color=blue]DataFormats[/color][color=black].[/color][color=black]Text[/color][color=black]), [/color][color=blue]String[/color][color=black])[/color]
[color=#0000ff]End[/color] [color=#0000ff]If[/color]
[color=black].[/color][color=black]Saved[/color] = [color=#0000ff]True[/color]
[color=black].[/color][color=#000080]Close[/color][color=black]()[/color]
[color=#0000ff]End[/color] [color=#0000ff]With[/color][color=#000000]



Cheers ;)



edit: note that (the point is) you have to check with e.Data.GetDataPresent(...) or checking the returned object from GetData(...) against *nothing* before using the data.
 
Last edited:
kulrom said:
Have you copied the contents of the textbox (or any source you have there) to the clipboard?

VB.NET:
[color=#008080]Clipboard[/color].SetDataObject(TextBox1.Text) 'i'm assuming that textbox1 is current source

Now you can go for getDataObject ... for instance

VB.NET:
[/color][/color]
[color=#0000ff][color=#008000]'Declare an IDataObject to hold the data returned from the [/color][color=#008000]clipboard.[/color]
[color=#0000ff]Dim[/color][color=#000000] iData [/color][color=#0000ff]As[/color][/color][color=#000000] IDataObject
[/color]
[color=#0000ff]With[/color] [color=black]objTemp[/color]
[color=black].[/color][color=black]Content[/color][color=black].[/color][color=black]Paste[/color][color=black]()[/color]
[color=black].[/color][color=black]Activate[/color][color=black]()[/color]
[color=#0000ff]{...}[/color]
[color=black]iData[/color] = [color=blue]Clipboard[/color][color=black].[/color][color=black]GetDataObject[/color]
[color=#0000ff]If[/color] [color=black]iData[/color][color=black].[/color][color=black]GetDataPresent[/color][color=black]([/color][color=blue]DataFormats[/color][color=black].[/color][color=black]Text[/color][color=black]) [/color][color=#0000ff]Then[/color]
[color=black]TextBox1[/color][color=black].[/color][color=black]Text[/color] [color=#804040]=[/color] [color=#000080]CType[/color][color=black]([/color][color=black]iData[/color][color=black].[/color][color=black]GetData[/color][color=black]([/color][color=blue]DataFormats[/color][color=black].[/color][color=black]Text[/color][color=black]), [/color][color=blue]String[/color][color=black])[/color]
[color=#0000ff]End[/color] [color=#0000ff]If[/color]
[color=black].[/color][color=black]Saved[/color] = [color=#0000ff]True[/color]
[color=black].[/color][color=#000080]Close[/color][color=black]()[/color]
[color=#0000ff]End[/color] [color=#0000ff]With[/color][color=#000000]



Cheers ;)



edit: note that (the point is) you have to check with e.Data.GetDataPresent(...) or checking the returned object from GetData(...) against *nothing* before using the data.



Hi kulrom,

I don't know why you declare an object(objtemp) for. What is its purpose ?

I added try and catch below and the error came up is below:

Try

Dim iData As IDataObject = Clipboard.GetDataObject()

' Determines whether the data is in a format you can use.

If iData.GetDataPresent(DataFormats.Text) Then

' Yes it is, so display it in a text box.

myModifier.sFDealerNew = CType(iData.GetData(DataFormats.Text), String)



End If

Catch

Dim err As Exception

MsgBox(err.Message)

End Try


An unhandled exception of type 'System.NullReferenceException' occurred in myapp.exe

Additional information: Object reference not set to an instance of an object.


I thought we did set it to an instance hence below:

Dim iData As IDataObject = Clipboard.GetDataObject()

I am at lost. Please help.

Note: sometimes it runs fine. Most of the time it gives the error.

blumonde
 
Ok, i'm not sure what are you trying to acomplish there but however this is the simplest way to set and get clipboard content:

VB.NET:
[size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] SetClipboardButton_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] SetClipboardButton.Click
 
[/size][size=2][color=#008000]'SetData
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] aDataObject [/size][size=2][color=#0000ff]As[/color][/size][size=2] [color=blue]New[/color] DataObject
 
aDataObject.SetData(DataFormats.Text, [/size][size=2][color=#0000ff]Me[/color][/size][size=2].ToClipboardTextBox.Text)
 
ClipBoard.SetDataObject(aDataObject)
 
[/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Sub
 
 
 
[/color][/size][size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] GetClipBoardButton_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] GetClipBoardButton.Click
 
[/size][size=2][color=#008000]'GetData / content from [color=#000000]ToClipboardTextBox control and pass the value to FromClipBoardTextBox[/color]
 
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2] Clipboard.GetDataObject.GetDataPresent(DataFormats.Text) [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].FromClipBoardTextBox.Text = System.Windows.Forms.Clipboard.GetDataObject.GetData(DataFormats.Text)
 
[/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Sub[/color][/size]


Cheers ;)
 
kulrom said:
Ok, i'm not sure what are you trying to acomplish there but however this is the simplest way to set and get clipboard content:

VB.NET:
[size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] SetClipboardButton_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] SetClipboardButton.Click
 
[/size][size=2][color=#008000]'SetData
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] aDataObject [/size][size=2][color=#0000ff]As[/color][/size][size=2] [color=blue]New[/color] DataObject
 
aDataObject.SetData(DataFormats.Text, [/size][size=2][color=#0000ff]Me[/color][/size][size=2].ToClipboardTextBox.Text)
 
ClipBoard.SetDataObject(aDataObject)
 
[/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Sub
 
 
 
[/color][/size][size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] GetClipBoardButton_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] GetClipBoardButton.Click
 
[/size][size=2][color=#008000]'GetData / content from [color=#000000]ToClipboardTextBox control and pass the value to FromClipBoardTextBox[/color]
 
[/color][/size][size=2][color=#0000ff]If[/color][/size][size=2] Clipboard.GetDataObject.GetDataPresent(DataFormats.Text) [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].FromClipBoardTextBox.Text = System.Windows.Forms.Clipboard.GetDataObject.GetData(DataFormats.Text)
 
[/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Sub[/color][/size]

Cheers ;)


hi kulrom,

I tried your exact format but still getting the error above. However, when I run it on windows 2000 there is no error. I am using xp pro. and I get the error. Am I missing something in my win xp pro ?

cheers,

blumonde
 
Back
Top