Results 1 to 10 of 10

Thread: [RESOLVED] Why doesn't my cursor show ?

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Resolved [RESOLVED] Why doesn't my cursor show ?

    Hi,

    I don't understand why my cursor isn't visible after this method:
    Code:
        Private Sub SystemEvents_PowerModeChanged(ByVal sender As Object, ByVal e As PowerModeChangedEventArgs)
            If e.Mode.ToString = "Resume" Then
                Me.Hide()
                Me.CenterToScreen()
                Me.TopMost = True
                'Cursor.Hide()
                Dim curX As Int32 = Me.Location.X + (Me.Width \ 2)
                Dim curY As Int32 = Me.Location.Y + (Me.Height - 28)
                Me.Show()
                Cursor.Position = New Point(curX, curY)
                'Cursor.Show()
            End If
        End Sub
    You can see I've tried the Show method, (Microsoft say Hide has to be balanced with Show, so I also tried adding Hide).

    When this code has run, the cursor is in the correct position, but it has to be moved before it's visible.
    I've juggled with the order of the commands but nothing I've done makes any difference.

    Poppa
    Last edited by Poppa Mintin; Aug 1st, 2020 at 05:24 AM. Reason: Typo (Again !)
    Along with the sunshine there has to be a little rain sometime.

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Why doesn't my cursor show ?

    What cursor? A mouse cursor or a cursor in a textbox? Also shouldn't there be a Handles clause or did you use AddHandler? And a few nitpicks: I believe ByVal is unnessary in this case and why aren't you using the Integer keyword instead of Int32?

  3. #3

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Why doesn't my cursor show ?

    Quote Originally Posted by Peter Swinkels View Post
    What cursor? A mouse cursor or a cursor in a textbox? Also shouldn't there be a Handles clause or did you use AddHandler? And a few nitpicks: I believe ByVal is unnessary in this case and why aren't you using the Integer keyword instead of Int32?
    Q: What cursor? A mouse cursor or a cursor in a textbox?
    A: The mouse cursor, isn't the 'cursor' in a textbox called a caret ?

    I've been coding with BASIC since 1978, with Visual Basic since 1990 (Version 3.0) and with Vb.NET since VS2005 and I've always used ByVal or ByRef where required and I've used Int32 since it was introduced, never has any issues with that.

    Also: In this instance, removing the ByVal 'e' produces an error:
    Severity Code Description Project File Line Suppression State
    Error BC30451 'e' is not declared. It may be inaccessible due to its protection level. Resume C:\Users\georg\Documents\~VB Snippets\Working Backup\Resume\Resume\Form1.vb 73 Active
    You can clearly see that 'e' is actually used in the method, removing just the ByVal 'Sender' produced the error:
    Severity Code Description Project File Line Suppression State
    Error BC31143 Method 'Private Sub SystemEvents_PowerModeChanged(e As PowerModeChangedEventArgs)' does not have a signature compatible with delegate 'Delegate Sub PowerModeChangedEventHandler(sender As Object, e As PowerModeChangedEventArgs)'. Resume C:\Users\georg\Documents\~VB Snippets\Working Backup\Resume\Resume\Form1.vb 9 Active
    So, far from being unnecessary they are both required.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  4. #4
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Why doesn't my cursor show ?

    We can take it for granted that Poppa Imported System.Win32 and added a handler, otherwise the Sub wouldn't fire.

    I tried tinkering with the Sub to see if I can get it to work. I changed the If statement to PowerModes.StatusChange, so I can get the Sub to run just by pulling and/or reinserting the power plug on my laptop. It then works for me as required, with the form centred and the cursor at the prescribed position.

    No need to mess around with ToString. Enumerations are meant to make things clearer, so there's no need to obscure them. What is more, there is no need to Hide and Show the form: it works equally well when they are commented out. (ByRef is also unnecessary, since it's the default. EDIT: just saw posts 2 and 3. Well, it works for me without ByRef.)

    I haven't tried PowerModes.Resume but presumably you have to set the mode to PowerModes.Suspend first.

    hope this helps, BB

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Why doesn't my cursor show ?

    Quote Originally Posted by Poppa Mintin View Post
    Q: What cursor? A mouse cursor or a cursor in a textbox?
    A: The mouse cursor, isn't the 'cursor' in a textbox called a caret ?

    I've been coding with BASIC since 1978, with Visual Basic since 1990 (Version 3.0) and with Vb.NET since VS2005 and I've always used ByVal or ByRef where required and I've used Int32 since it was introduced, never has any issues with that.

    Also: In this instance, removing the ByVal 'e' produces an error:
    You can clearly see that 'e' is actually used in the method, removing just the ByVal 'Sender' produced the error:

    So, far from being unnecessary they are both required.


    Poppa
    Okay, okay. What version of Visual Studio are you using? I asked about ByVal because I remember it used to be inserted automatically by Visual Studio. I just checked and ByVal doesn't appear in auto-generated form events. And okay, so you're an experienced programmer. Fine, so am I, and that can sometimes actually work against you because you get stuck in obsolete ways of doing things.

    btw:
    Thank you for answering my questions.

    Peter

    EDIT:
    And I checked, it appears both caret and cursor are valid terms when talking about textboxes.
    Last edited by Peter Swinkels; Aug 1st, 2020 at 11:38 AM.

  6. #6

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Why doesn't my cursor show ?

    Thanks guys,

    I have it showing the cursor now, it sits neatly between the two options buttons on the form. It always did, but it's visible now.
    Stepping through from a breakpoint I made a surprise (to me) discovery, when control gets to 'Me.Show()' control goes back to the start of the subroutine and goes through it all again.

    Also... Watch Windows tell me that 'e.Mode.ToString' = "Suspend(3)", not 'Resume', so the If statement was never true. So I don't know how it ever got as far as 'Me.Show()', unless the (3) equates to 'Resume'.

    Anyway, after a little more playing, I removed the If statement altogether and repositioned the 'Me.Show()'. When running, the form sits hidden in the background until the laptop lid is opened, hence the subroutine. Removing the If statement meant that I could also remove the ByVal arguments since Boops boops also says the arguments are not required.

    I worry a little because closing the laptop lid is also a Power Mode Change, but since that also puts the laptop either to sleep, or in hibernation I suppose that's nothing to worry about. Sadly, disconnecting or connecting the power lead also opens the form, I guess I'll have to put up with that, I don't do either of those very often when the laptop is running. I'll sign this thread off as resolved.

    Oh, I might just mention that the 'Centre to Screen' is because some programmes I run change the screen resolution, shifting the form's position in the process. I like the form to appear where I expect it, so that when I open the lid I can make the choice to run my Daily Diary application or not.

    Code:
        Private Sub SystemEvents_PowerModeChanged()
            Me.Show()
            Me.CenterToScreen()
            Me.TopMost = True
            Dim curX As Int32 = Me.Location.X + (Me.Width \ 2)
            Dim curY As Int32 = Me.Location.Y + (Me.Height - 28)
            Cursor.Position = New Point(curX, curY)
        End Sub

    Poppa
    Along with the sunshine there has to be a little rain sometime.

  7. #7

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Why doesn't my cursor show ?

    Hi,

    (Forgot to mark this as resolved, Just as well because it's not.)

    I went back to the original method, and added some test instructions...
    Code:
    
        Private Sub SystemEvents_PowerModeChanged(ByVal sender As Object, ByVal e As PowerModeChangedEventArgs)
    
            ' Temp investigation.
            Me.Show()
            Static x, y As String
            y = x
            If y = Nothing Then y = "Nothing"
            x = e.Mode.ToString
            Label1.Text = "Was: " & y & ". Is now: " & x
            ' End of temp.
    
            If e.Mode.ToString = "Resume" Then
                ' Me.Show()      'Temp comment-out
                Action()
                Me.CenterToScreen()
                Me.TopMost = True
                Dim curX As Int32 = Me.Location.X + (Me.Width \ 2)
                Dim curY As Int32 = Me.Location.Y + (Me.Height - 28)
                Cursor.Position = New Point(curX, curY)
            End If
        End Sub
    Ok, start the project, when up and running (Form not visible) remove the power connector:
    Up comes the form, Label1 text is 'Was: Nothing. Is now: PowerChange'. (Form still visible, not told to hide)...
    ...reconnect the power, Label1 text changes to 'Was: PowerChange. Is now: PowerChange'...

    Close and re-start the project (fresh start), project running, form hidden.
    Close the laptop lid, wait until computer is asleep, re-open the lid, computer wakes up, form shows...
    Label1 text now says 'Was: Resume. Is now: PowerChange'.

    So... Obviously 'Resume' is a valid argument.

    Now all I need is the answer to my original question... Why doesn't the cursor show?
    I shall try to investigate why the subroutine (obviously) runs twice, that may have a bearing on my problem.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Why doesn't my cursor show ?

    With regards to the use of ByVal, the point was to remove just the keyword, not the whole parameter. Your third quote even shows you that it's not required:
    Severity Code Description Project File Line Suppression State
    Error BC31143 Method 'Private Sub SystemEvents_PowerModeChanged(e As PowerModeChangedEventArgs)' does not have a signature compatible with delegate 'Delegate Sub PowerModeChangedEventHandler(sender As Object, e As PowerModeChangedEventArgs)'. Resume C:\Users\georg\Documents\~VB Snippets\Working Backup\Resume\Resume\Form1.vb 9 Active
    The ByVal keyword does not hurt but is no longer required as it is the default. You only need to specify how a parameter is passed if it's by reference, i.e. ByRef is required but ByVal is not.

    As suggested, this is wrong:
    vb.net Code:
    1. If e.Mode.ToString = "Resume" Then
    Even if it works, it's still wrong. If you're using an enumeration (you are) then use it:
    vb.net Code:
    1. If e.Mode = PowerModes.Resume Then
    As for the question, if I put my computer to sleep and then wake it up, the cursor doesn't display until I move the mouse. That appears to be standard Windows behaviour so there doesn't appear to be a problem to solve.

  9. #9

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Why doesn't my cursor show ?

    Quote Originally Posted by jmcilhinney View Post
    As for the question, if I put my computer to sleep and then wake it up, the cursor doesn't display until I move the mouse. That appears to be standard Windows behaviour so there doesn't appear to be a problem to solve.
    Ah !

    Oh bugger !

    That's a nuisance but I guess it answers the question.

    All other points noted. I was just about to add a small test project, I may do that anyway as a possible point of interest.
    (I'll make changes where appropriate to your recommendations first.)


    Thanks John


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  10. #10

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Why doesn't my cursor show ?

    A small test project of possible interest.

    This project is to investigate the results of Microsoft.Win32.SystemEvents.PowerModeChanged.
    Removing and replacing the external power from a laptop, for example, or waking the computer from sleep or hibernation.

    A general view of my lay-out: (Normally I'd set Labels texts at the design stage)

    Attachment 178176
    Code:
    Imports Microsoft.Win32
    
    Public Class Form1
        Private Sub Form1_Load() Handles MyBase.Load
            Button1.Text = "Clear"
            Label1.Text = "Current:"
            Label2.Text = "Previous:"
            Label3.Text = "2 Previous:"
            Label4.Text = "3 Previous:"
            Clear()
            AddHandler Microsoft.Win32.SystemEvents.PowerModeChanged, AddressOf PMC
        End Sub
    
        Private Sub Button1_Click() Handles Button1.Click
            Clear()
        End Sub
    
        Private Sub Button2_Click() Handles Button2.Click
            Me.Close()
        End Sub
    
        Private Sub Clear()
            Label5.Text = Nothing
            Label6.Text = Nothing
            Label7.Text = Nothing
            Label8.Text = Nothing
        End Sub
    
        Private Sub PMC(ByVal sender As Object, ByVal e As PowerModeChangedEventArgs)
            Label8.Text = Label7.Text
            Label7.Text = Label6.Text
            Label6.Text = Label5.Text
            If Label6.Text = Nothing Then Label6.Text = "Nothing"
            Label5.Text = e.Mode.ToString
        End Sub
    
    End Class

    Poppa
    Along with the sunshine there has to be a little rain sometime.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width