|
-
May 15th, 2008, 09:56 AM
#1
Thread Starter
Hyperactive Member
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)
-
May 15th, 2008, 10:11 AM
#2
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.
-
May 15th, 2008, 10:46 AM
#3
Thread Starter
Hyperactive Member
Re: Using Protected Function
 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)
-
May 15th, 2008, 10:57 AM
#4
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.
-
May 16th, 2008, 09:17 AM
#5
Thread Starter
Hyperactive Member
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)
-
May 16th, 2008, 09:51 AM
#6
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.
-
May 16th, 2008, 10:52 AM
#7
Thread Starter
Hyperactive Member
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)
-
May 16th, 2008, 11:18 AM
#8
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|