Question

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.

Was it helpful?

Solution

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)

OTHER TIPS

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.

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")

you mustn't use 32bit color cursors.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top