tags:

views:

5666

answers:

8

In my VB.net project I created a custom cursor (Window.cur). How can I assign that to the cursor without having to use the full file path to that file?

VB.Net has My.Resources but it does not show the cursors that are embedded in the project.

I found an example that used code like this: New Cursor(Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream("Window.cur") but that does not work.

A: 

You are missing the namespace. You probably want to use:

MyNamespace.MySubfolder.Window.cur

EDIT: Also, make sure your Build Action for the item is "Embedded Resource", otherwise it will not be include in your dll/exe.

Otávio Décio
ocdecio: Can you be more specific please? I tried all the variations I could think of with what you mentioned. VB.Net does not have MyNamespace from what I can see (course I could be wrong).
KerryF
A: 

Suppose you are assigning "Cursor1.cur" to be the cursor for the control "Button1."

In your Form.Load event you would do something like -

Button1.Cursor = New Cursor(Me.GetType(), "Cursor1.cur")
+3  A: 

Guessing the resource name can be difficult. To find out, run Ildasm.exe on your program. Double-click "Manifest" and look for the .mresource.

Another way to do it that avoids guessing: Project + Properties, Resource tab. Click the arrow on the "Add Resource" button, Add Existing File and select your .cur file. Make your code look like this:

Dim ms As New System.IO.MemoryStream(My.Resources.Cursor1)
Button1.Cursor = New Cursor(ms)
Hans Passant
+1  A: 

Thanks for the help! I assumed that if I created the resource in the Visual Studio IDE it would add it to my project. Silly me!

I had to go to the Project tab to add the Window.Cur file using Add Resource (thanks nobugz!) and then use the code he mentioned:

Dim ms As New System.IO.MemoryStream(My.Resources.Window)

Button.Cursor = New Cursor(ms)

I would vote up on the answer if I could but I can't as I only have a value of 13 currently.

KerryF
A: 

hello,

I am trying to use a custom cursor (downloaded from internet target.cur) I added it to the project resources and added the following code

Dim ms As New System.IO.MemoryStream(My.Resources.target) Me.PictureBox1.Cursor = New Cursor(ms)

It throws the following exception: System.ArgumentException was unhandled Message="Image format is not valid. The image file may be corrupted. Parameter name: stream" ParamName="stream" Source="System.Windows.Forms"

but Strangly, this code works for any system cursors from c:\window\cursors. I am not able to figure out why. I downloaded a couple of other cursors too, its the same result.

Any thoughts??? I really need to fix this by the end of the week! Any ideas will be greatly appreciated. You can also email me at [email protected]

Thanks,

A: 

hi,

you mustn't use 32bit color cursors.

A: 

Even if the thread is over a year old...

Thanks nobugz! I was on the brink of going ballistic after trying to assign my custom cursor to my program! So for everyone who found that thread through google like me:

Dim ms As New System.IO.MemoryStream(My.Resources.Cursor1)

Button1.Cursor = New Cursor(ms)

That worked for me! :D

aline
A: 

@Naga,

They may need tobe 8 bit only as xdStyla indicated above.

Dib