techmanbd Posted September 10, 2003 Posted September 10, 2003 I am new to VB .NET and been awhile since I programmed with VB6. So bare with me. I have searching all over for the answer but will post now. Using a command button, I would like to use it as the folowing: When button is pressed down and keep it pressed, it will repeat the code I have in the sub for the button and once I have released the button it stops the code. Is .net capable of doing this? and if so how? please :confused: Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi
aewarnick Posted September 10, 2003 Posted September 10, 2003 Handle the MouseDown event of the button and use a timer to repeat the code. Also handle the MouseUp event and stop the timer there. Quote C#
AlexCode Posted September 15, 2003 Posted September 15, 2003 The timer idea works, and I've another idea for you! This will work just fine and, as you're new to VB.net, you'll learn some cool things like multithreading programming :) Try it and mess around with it! Any questions post them :p On a form put a button and a labelm don't change their names, add this code and see it running (it's some counter thar resets every time it's presses. If you don't want it to reset declare the i var outside of the method...) Enjoy! Private Down As Boolean = False Private DownThread As Threading.Thread Private Sub myMethod() Dim i As Integer = 0 Do While Down Me.Label1.Text = i.ToString i += 1 Loop End Sub Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown Down = True DownThread = New Threading.Thread(AddressOf myMethod) DownThread.Start() End Sub Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp DownThread.Suspend() Down = False DownThread = Nothing End Sub Quote Software bugs are impossible to detect by anybody except the end user.
aewarnick Posted September 15, 2003 Posted September 15, 2003 Glad you got it working. I am not new to programming in .net, I use C# which is almost the same as vb.net except syntax. Thanks for trying to help though, you'll be a benefit to the site. Maybe someone else will be inspired by your idea. :) Quote C#
AlexCode Posted September 15, 2003 Posted September 15, 2003 I just like to teach what I know! :) I'm glad you've found it useful... Quote Software bugs are impossible to detect by anybody except the end user.
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.