none
Assigning a custom cursor to a control RRS feed

  • Question

  • Hello to all,

    I'm trying to assign a custom cursor to a picture box. I have already created the custom cursor, "Underline.cur", using the VS 2017 utility. It appears in the Solution Explorer, and the file exists in the projects' folder, where VS2017 decided it should be. I have set the cursor's BuildAction property to EmbeddedResource.

    I've tried a variety of code found in other related postings, but apparently still missing something. The code I am trying to use is:

    Pic_BitEditor.Cursor = New Cursor(Pic_BitEditor.GetType(), "Underline.cur")
    

    The error is it cannot find the file. It gives the path where it expects to find the file, but it's not where VS2017 put it when I created it. I would think VS2017 knows where it wants certain files to be. Do I simply move or copy the file to the other folder, and will that create problems later when I build and distribute the application?

    Also tried to load it using My.Resources, but also had problems.

    Seems the 'simple' things cause the most trouble. Any assistance is appreciated.

    DW

    Thursday, July 27, 2017 6:02 PM

Answers

  • Here are three ways to use the .cur file. It has to be a real .cur file.

    'for disc file Dim MyCursor As New Cursor("c:\bitmaps\curshand.cur") PictureBox1.Cursor = MyCursor 'for app startup folder Dim MyCursor2 As New Cursor(Application.StartupPath & "\curshand.cur") PictureBox1.Cursor = MyCursor2 'for .cur file in resources Dim ms As New IO.MemoryStream(My.Resources.CURSHAND) PictureBox1.Cursor = New Cursor(ms)


    PS When you import the existing .cur file to your resources, Visual Studio makes a copy of the file and puts it in your ProjectName\ProjectName\Resources folder. When you just open the file using the path in code you need to put the file at the location your code specs.

    Thursday, July 27, 2017 9:32 PM

All replies

  • Hello to all,

    I'm trying to assign a custom cursor to a picture box. I have already created the custom cursor, "Underline.cur", using the VS 2017 utility. It appears in the Solution Explorer, and the file exists in the projects' folder, where VS2017 decided it should be. I have set the cursor's BuildAction property to EmbeddedResource.

    I've tried a variety of code found in other related postings, but apparently still missing something. The code I am trying to use is:

    Pic_BitEditor.Cursor = New Cursor(Pic_BitEditor.GetType(), "Underline.cur")

    The error is it cannot find the file. It gives the path where it expects to find the file, but it's not where VS2017 put it when I created it. I would think VS2017 knows where it wants certain files to be. Do I simply move or copy the file to the other folder, and will that create problems later when I build and distribute the application?

    Also tried to load it using My.Resources, but also had problems.

    Seems the 'simple' things cause the most trouble. Any assistance is appreciated.

    DW

    Hi

    I have an Icon in Resources, (Embedded so stays with Project) named 'MyCursor' - even though it is an Icon of a FootBall :)

    I use it with a My.Settings Boolean variable called 'Football' :

            If My.Settings.UseFootball Then
                Me.Cursor = New Cursor(My.Resources.MyCursor.Handle)
            Else
                Me.Cursor = Cursors.WaitCursor
            End If
    
    so, I would suppose you could have your own cursor used via (say) a Mouse Enter event handler for a control etc.


    Regards Les, Livingston, Scotland

    Thursday, July 27, 2017 6:26 PM
  • " I would think VS2017 knows where it wants certain files to be. "

    It is not clear how you put the cursor in your project but I don't think VS knows about your .cur file location (unless its in resources).

    The string "Underline.cur" is not a complete path to the file. The full path would be something like "c:\mycursorfolder\Underline.cur".

    If the .cur file is to be in your application folder you need to supply that path which might be like C:\Program Files (x86)\Sandia Software\Mine Field\. If using the app path you can get the folder path your app.exe starts up within using Application.StartupPath.

    Finally, the app.exe folder is different when you are running in VisualStudio. So the .cur needs to be in the proper folder there as well. Something like project name \bin\Debug. You can specify this location using VS.

    Thursday, July 27, 2017 7:14 PM
  • Thanks Tommytwotrain & leshay

    I tried specifying the full path in the original code line I posted. It finds the file, but the error is now with the use of "New".

    Trying a different approach, I managed to create a resource file and have added the cursor file to it.

    Pic_BitEditor.Cursor = New Cursor(My.Resources.Underline)

    Again, the error is with using "New".  What am I missing here?
    Thursday, July 27, 2017 8:10 PM
  • Hi dw80916

    Here is some code using 2 x Icon files and 1 x Cursor file.

    2 of the controls on the form use the 2 Icon files and 1 of them uses the Cursor file.

    ' Form1 with Panel1, Label1 and
    ' Label2
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Panel1_MouseEnter(sender As Object, e As EventArgs) Handles Panel1.MouseEnter
            ' icon in My.Resources
            Cursor = New Cursor(My.Resources.Football.Handle)
        End Sub
        Private Sub Panel1_MouseLeave(sender As Object, e As EventArgs) Handles Panel1.MouseLeave
            Cursor = Cursors.Default
        End Sub
    
        Private Sub Label1_MouseEnter(sender As Object, e As EventArgs) Handles Label1.MouseEnter
            ' icon in My.Resources
            Cursor = New Cursor(My.Resources.FromMars.Handle)
        End Sub
        Private Sub Label1_MouseLeave(sender As Object, e As EventArgs) Handles Label1.MouseLeave
            Cursor = Cursors.Default
        End Sub
    
        Private Sub Label2_MouseEnter(sender As Object, e As EventArgs) Handles Label2.MouseEnter
            ' Cursor file in My.Resources
            Dim ms As New IO.MemoryStream(My.Resources.MyCursorFile)
            Label2.Cursor = New Cursor(ms)
        End Sub
        Private Sub Label2_MouseLeave(sender As Object, e As EventArgs) Handles Label2.MouseLeave
            Cursor = Cursors.Default
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    Thursday, July 27, 2017 9:25 PM
  • Here are three ways to use the .cur file. It has to be a real .cur file.

    'for disc file Dim MyCursor As New Cursor("c:\bitmaps\curshand.cur") PictureBox1.Cursor = MyCursor 'for app startup folder Dim MyCursor2 As New Cursor(Application.StartupPath & "\curshand.cur") PictureBox1.Cursor = MyCursor2 'for .cur file in resources Dim ms As New IO.MemoryStream(My.Resources.CURSHAND) PictureBox1.Cursor = New Cursor(ms)


    PS When you import the existing .cur file to your resources, Visual Studio makes a copy of the file and puts it in your ProjectName\ProjectName\Resources folder. When you just open the file using the path in code you need to put the file at the location your code specs.

    Thursday, July 27, 2017 9:32 PM