Results 1 to 8 of 8

Thread: Using Protected Function

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    UK
    Posts
    489

    Using Protected Function

    Hi

    Well basically I want to do move a control via arrow keys, I did this however the keydown event doesnt really work with this type of scenario, anyways I found some code which does the job

    Code:
     Protected Overrides Function ProcessCmdKey(ByRef msg As Message, _
      ByVal keyData As Keys) As Boolean
    
    
    
    
    
     Select Case keyData
    
    Case Keys.Up
    
    'do whatever....up arrow pressed
    bHandled = True
    
    Case Keys.Down
    
    'do whatever....down arrwo pressed
    bHandled = True
    
    
    End Select
    
    Return bHandled
    
    End Function
    However I have no idea how to call it to be used
    Learning C♯

    Data Binding & Bound Controls - Objects and wizards will never be as intelligent as you, do it yourself! (Unless your Pro)

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Using Protected Function

    You make a new class. Then inherit the control object you want to use this in, then put that code in the class.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    UK
    Posts
    489

    Re: Using Protected Function

    Quote Originally Posted by Jenner
    You make a new class. Then inherit the control object you want to use this in, then put that code in the class.
    Could you post a quick example, never had to do this before.
    Learning C♯

    Data Binding & Bound Controls - Objects and wizards will never be as intelligent as you, do it yourself! (Unless your Pro)

  4. #4
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Using Protected Function

    Certainly, here's an example using a TextBox. I make a new class. I call it "SpecialTextBox"

    Code:
    Public Class SpecialTextBox
        Inherits TextBox
    
        Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
            Dim bHandled As Boolean
            Select Case keyData
                Case Keys.Up
                    Me.Top -= 4
                    bHandled = True
                Case Keys.Down
                    Me.Top += 4
                    bHandled = True
                Case Keys.Left
                    Me.Left -= 4
                    bHandled = True
                Case Keys.Right
                    Me.Left += 4
                    bHandled = True
                Case Else
                    bHandled = False
            End Select
    
            Return bHandled
    
        End Function
    End Class
    Now, I add my "SpecialTextBox" to my form. Now, when I press or hold down an arrow key while my textbox has focus, I can move it around the form.

    Inheritance is REALLY nice. My new class does EVERYTHING a Textbox does! BUT! I am overriding one of it's standard abilities with my own. Many objects in .NET have methods you can override like this to do whatever you want.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    UK
    Posts
    489

    Re: Using Protected Function

    Hi

    Thanks for the example, I dont have VB on this conputer but in sure it will work , okay another question which I really need help on the logic part.

    Ok, I have a graphic with one straight line and several other small lines like the example below

    Code:
    ______
          |
          | 
          |
          | 
          |______
          | 
          |
          | 
          |
          | 
    ______|
          | 
          |
          | 
          |
          | 
          |______
          |
    The movable control is by deafult placed on the line, however I only want th user to be able to follow the lines and not go be able to move off the line, I could probably a massive conditional statement e.g. if statement, but it probably would be several pages long and not efficient at all, therefore I am asking for other ideas.

    Cheers
    Learning C♯

    Data Binding & Bound Controls - Objects and wizards will never be as intelligent as you, do it yourself! (Unless your Pro)

  6. #6
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Using Protected Function

    Hmm... ok, this is where I take a step back and put the question "exactly why do you want to do this?" I say this because I have suspicions as to what you're doing and if I'm right, then I'm going to tell you your whole methodology for doing it this way is wrong and there is a much much easier way to do what you want to do.

    If I'm wrong, then I can probably help you out with some math-routines to create a proper boundary for you to follow.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    UK
    Posts
    489

    Re: Using Protected Function

    I think your suspision is right, I never really set out to do this, I just started getting ideas etc. Basically a graphic moves up and down the straight line continously, which I have already done, then this movevable control has to reach the bottom side line without colliding with the graphic (control) moving up and down continously, to avoid this it must get to the slides sticking out of the main line.

    I havent done the collision detection, but my timer fires so frequently Im not worried about doing it.
    Learning C♯

    Data Binding & Bound Controls - Objects and wizards will never be as intelligent as you, do it yourself! (Unless your Pro)

  8. #8
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Using Protected Function

    Gotcha, my suspicion was you are trying to make some form of game using moving controls, but even if you're not, it's complex enough that some game programming and methods would help you out a lot.

    Using moving controls to make a game is pure rubbish. The overhead on your components is horrible. Likewise, GDI+, which is the standard method Windows uses to draw controls is awful, but you can use it for most simple things or things that don't need too much motion.

    Without getting into the basics of DirectX let me point you to a site with examples that really helped me learn some of these basics. It has a section on "VB.NET for beginners", then goes into "Simple game programming" which includes using the OnPaint Event to put things onto a form, how to make a tile-based game, simple animation, boundary condition methods, timing, and input. After that, if you want to get daring, it'll show you the same concepts using DirectX.

    Anyways, try here: http://vbprogramming.8k.com/tutorials/main.htm

    If you're pretty decent with VB.NET, jump straight to tutorial 9: GDI+
    This things you're making sounds like it should be done using an OnPaint Override which is much easier than trying to push pictureboxes around.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

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