Results 1 to 4 of 4

Thread: Button within a Location to execute a code

  1. #1

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Angry Button within a Location to execute a code

    I am getting and error where 80, 380 is and the Comma is getting a error it blatantly says ") expected"

    Can someone tell me why it does this?



    Code:
    If e.KeyCode = Keys.Space And BtnSprite.Location = New Point(12, 330) <= (80, 380) Then
                MsgBox("Txt here")
            End If
    Additional info: I wan it if the button sprite is within the location of 12, 330 and 8-, 380 to execute the msgbox. I was confused on how I can implicate between methods but there is none. There is Greater then and less then signs that do the trick.

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

    Re: Button within a Location to execute a code

    Neither your code nor your explanation actually makes sense. Presumably you want to know whether the Location of the Button falls within the box described by those two Points as opposite corners. Is that correct? If so then you have two choices:

    1. Examine the X and Y components of the Location separately and determine whether they are each within the appropriate range:
    Code:
    If e.KeyCode = Keys.Space AndAlso
        BtnSprite.Left >= 12 AndAlso
        BtnSprite.Left <= 80 AndAlso
        BtnSprite.Top >= 330 AndAlso
        BtnSprite.Top <= 380 Then
        MessageBox.Show("Txt here")
    End If
    2. Construct the box and see if it contains the Location:
    Code:
    Dim box As New Rectangle(12, 330, 80 - 12, 380 - 330)
    
    If e.KeyCode = Keys.Space AndAlso box.Contains(BtnSprite.Location) Then
        MessageBox.Show("Txt here")
    End If

  3. #3

    Thread Starter
    Addicted Member Reapism's Avatar
    Join Date
    Sep 2012
    Posts
    170

    Resolved Re: Button within a Location to execute a code

    Quote Originally Posted by jmcilhinney View Post
    Neither your code nor your explanation actually makes sense. Presumably you want to know whether the Location of the Button falls within the box described by those two Points as opposite corners. Is that correct? If so then you have two choices:

    1. Examine the X and Y components of the Location separately and determine whether they are each within the appropriate range:
    Code:
    If e.KeyCode = Keys.Space AndAlso
        BtnSprite.Left >= 12 AndAlso
        BtnSprite.Left <= 80 AndAlso
        BtnSprite.Top >= 330 AndAlso
        BtnSprite.Top <= 380 Then
        MessageBox.Show("Txt here")
    End If
    2. Construct the box and see if it contains the Location:
    Code:
    Dim box As New Rectangle(12, 330, 80 - 12, 380 - 330)
    
    If e.KeyCode = Keys.Space AndAlso box.Contains(BtnSprite.Location) Then
        MessageBox.Show("Txt here")
    End If
    Thanks so much, you professional. The second one was exactly it! Now I want to make the button move faster while holding shift across the form.


    My Code:
    Code:
     If Keys.LShiftKey And e.KeyCode = Keys.W Then
                BtnSprite.Location += New Point(0, -5) 'Added Location value to W using shift
            End If
    Original without shift:
    Code:
    If e.KeyCode = Keys.W Then
                BtnSprite.Location += New Point(0, -1) 'Added Location value to W
            End If

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

    Re: Button within a Location to execute a code

    If you are interested in only horizontal or only vertical then use the Button's Left and Top properties rather than Location. Only use Location if you're interested in both horizontal and vertical. The same goes for Width, Height and Size.

    As for the question, it's a bit tricky if you want to differentiate between left and right Shift keys. You can easily differentiate between left and right when the user depresses that key but, when the user depresses another key while the Shift key is down, you can't easily differentiate between left and right then. If you don't care about left and right then use something like this:
    Code:
    Select Case e.KeyData
        Case Keys.W
            BtnSprite.Top -= 1
        Case Keys.Shift Or Keys.W 'Shift+W
            BtnSprite.Top -= 5
        Case Keys.A
            BtnSprite.Left -= 1
        Case Keys.Shift Or Keys.A 'Shift+A
            BtnSprite.Left -= 5
        Case Keys.S
            BtnSprite.Top += 1
        Case Keys.Shift Or Keys.S 'Shift+S
            BtnSprite.Top += 5
        Case Keys.D
            BtnSprite.Left += 1
        Case Keys.Shift Or Keys.D 'Shift+D
            BtnSprite.Left += 5
    End Select
    If you do care about left and right Shift then post back and we can have a closer look.

Tags for this Thread

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