JoshK Posted May 1, 2006 Posted May 1, 2006 ok I have a time that ticks every 10ms to update one lable with the current mouse position, and also set a panel to the color unser the mouse Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Dim point As Point point.X = Windows.Forms.Cursor.Position.X point.Y = Windows.Forms.Cursor.Position.Y lblMouse.Text = point.X & "," & point.Y Dim color As Color color = GetPixelColor(point.X, point.Y) lblColor.Text = ColorTranslator.ToWin32(Drawing.Color.FromArgb(color.A, color.R, color.G, color.B)).ToString pnlColor.BackColor = color System.Windows.Forms.Application.DoEvents() End Sub while thats happening I want to be able to save the current mouse position and the color to a listbox when the user hits the 'w' key Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress If e.Equals(Keys.W) Then lstCommands.Items.Add(lblmouse.tostring & " :: " & lblcolor.tostring) End Sub As you can see I put DoEvents() but when I hit the w key nothing happens, I also tried removing the IF in form1_keypress and the keypress just doesnt activate. Please help Quote
Leaders snarfblam Posted May 1, 2006 Leaders Posted May 1, 2006 By default the KeyPress event only fires for the control that has focus, which will never be the form if the form has child controls. You should set the form's KeyPreview property to True. This causes the form to intercept all keyboard input and process it before it is sent to child controls, which should be all you need to do to make your program work. Quote [sIGPIC]e[/sIGPIC]
JoshK Posted May 1, 2006 Author Posted May 1, 2006 Great thanks, I'll give it a try. Then, for future reference, what if I have a textbox for some kind of information, what if the user hit one of the keys that I have in KeyPress event. Is there a way to turn KeyPreview on and off in code? Quote
Leaders snarfblam Posted May 2, 2006 Leaders Posted May 2, 2006 One possiblility would be to examine the ActiveControl property and act based on that... Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown If ActiveControl Is TextBox1 Then 'You probably want to do nothing here Else If e.KeyCode = Keys.W Then 'Do things here End If End If End Sub You could also set the KeyPreview property when a TextBox gets or loses focus... Private Sub TextBox1_Enter(ByVal sender As System.Object, ByVal e As em.EventArgs) Handles TextBox1.Enter KeyPreview = False End Sub Private Sub TextBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave KeyPreview = True End Sub Quote [sIGPIC]e[/sIGPIC]
Cags Posted May 2, 2006 Posted May 2, 2006 (edited) Just so nobody gets confused its not strictly neccessary to alter behaviour for a textbox unless you don't want the form to perform the action when it is focused. Text typed will still appear in the textbox it was just also call the forms keypress. As such if the forms key press only handles shortcut keys and such, you will not need to code special cases for your textboxes. Edited May 2, 2006 by Cags Quote Anybody looking for a graduate programmer (Midlands, England)?
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.