|
-
Mar 10th, 2015, 06:08 PM
#1
Thread Starter
New Member
[RESOLVED] Lock PictureBox Button Upon Click Till Timer Ends or Else
Hello,
I am using PictureBox as Button, which starts a timer.
I have 3 different images for the button:
1st for normal button (MouseLeave)
2nd for hover (MouseEnter)
3rd for clicked (Click)
How can I lock the PictureBox Button to "Clicked" state (specific img) untill the timer ends or a different button is clicked to unlock it.
I usually use "Enabled = False", but this completely disables the PictureBox
Regards!
We make a living by what we get, but we make a life by what we give.
-
Mar 10th, 2015, 06:30 PM
#2
Re: Lock PictureBox Button Upon Click Till Timer Ends or Else
can you show us your picturebox click event?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 10th, 2015, 06:37 PM
#3
Re: Lock PictureBox Button Upon Click Till Timer Ends or Else
ok. here's how I'd write it:
Code:
Public Class Form1
Private WithEvents tmr As New Timer
Dim ignore As Boolean = False
Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
tmr.Stop()
PictureBox1.BackColor = Color.Red
ignore = False
End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
If ignore Then Return
ignore = True
PictureBox1.BackColor = Color.Blue
tmr.Interval = 5000
tmr.Start()
End Sub
Private Sub PictureBox1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
If ignore Then Return
PictureBox1.BackColor = Color.Green
End Sub
Private Sub PictureBox1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
If ignore Then Return
PictureBox1.BackColor = Color.Red
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 11th, 2015, 12:09 AM
#4
Re: Lock PictureBox Button Upon Click Till Timer Ends or Else
I would pretty much agree with .paul. but there are a couple of adjustments you'd need. Firstly, when you "unlock" a control, you'd want to test whether the cursor is contained within it to decide what colour/image to display. Secondly, you need a way to "unlock" a currently locked control when another control is clicked. E.g.
vb.net Code:
Public Class Form1
''' <summary>
''' Indicates whether or not each PictureBox is locked.
''' </summary>
Private lockedStateByPictureBox As Dictionary(Of PictureBox, Boolean)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'All PictureBoxes are initially unlocked.
Me.lockedStateByPictureBox = New Dictionary(Of PictureBox, Boolean) From {{Me.PictureBox1, False},
{Me.PictureBox2, False},
{Me.PictureBox3, False}}
End Sub
Private Sub PictureBoxes_MouseEnter(sender As Object, e As EventArgs) Handles PictureBox3.MouseEnter,
PictureBox2.MouseEnter,
PictureBox1.MouseEnter
Dim pb = DirectCast(sender, PictureBox)
If Not Me.lockedStateByPictureBox(pb) Then
'This PictureBox is not locked so indicate that the cursor is over it.
pb.BackColor = Color.Red
End If
End Sub
Private Sub PictureBoxes_MouseLeave(sender As Object, e As EventArgs) Handles PictureBox3.MouseLeave,
PictureBox2.MouseLeave,
PictureBox1.MouseLeave
Dim pb = DirectCast(sender, PictureBox)
If Not Me.lockedStateByPictureBox(pb) Then
'This PictureBox is not locked so indicate that the cursor is not over it.
pb.BackColor = Color.Green
End If
End Sub
Private Sub PictureBoxes_Click(sender As Object, e As EventArgs) Handles PictureBox3.Click,
PictureBox2.Click,
PictureBox1.Click
Dim pb = DirectCast(sender, PictureBox)
If Not Me.lockedStateByPictureBox(pb) Then
'Unlock any currently locked PictureBox(es).
Unlock()
'Lock the PictureBox that was just clicked.
Lock(pb)
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'Unlock any currently locked PictureBox.
Unlock()
End Sub
''' <summary>
''' Locks a PictureBox.
''' </summary>
''' <param name="pb">
''' The PictureBox to lock.
''' </param>
Private Sub Lock(pb As PictureBox)
'Indicate that the PictureBox is locked.
pb.BackColor = Color.Blue
Me.lockedStateByPictureBox(pb) = True
'Start the unlock Timer.
Me.Timer1.Start()
End Sub
''' <summary>
''' Unlocks any locked PictureBox.
''' </summary>
Private Sub Unlock()
'Stop the unlock Timer.
Me.Timer1.Stop()
For Each pb In Me.lockedStateByPictureBox.Keys.ToArray()
If Me.lockedStateByPictureBox(pb) Then
'Unlock the locked PictureBox.
Me.lockedStateByPictureBox(pb) = False
If Me.RectangleToScreen(pb.Bounds).Contains(Cursor.Position) Then
'Indicate that the cursor is over the PictureBox.
pb.BackColor = Color.Red
Else
'Indicate that the cursor is not over the PictureBox.
pb.BackColor = Color.Green
End If
'Only one PictureBox can ever be locked so look no further.
Exit For
End If
Next
End Sub
End Class
Last edited by jmcilhinney; Mar 11th, 2015 at 12:18 AM.
-
Mar 11th, 2015, 08:49 AM
#5
Thread Starter
New Member
Re: Lock PictureBox Button Upon Click Till Timer Ends or Else
Thank you for the quick responses!
Both of you suggested a lot of code tho (some of which I am not familiar with) and I am lookin for a simpler and shorter solution.
Since I just wanted to "lock" the PictureBox to a "clicked" state image only till the timer is running i.e. the ProgressBar. And If I use the "Enabled = False" it completely removes the PictureBox.
I came up with this solution, it may not be the best one, nor the right one, but it works like I wanted to:
(Atention, you may lol)
I've just created another PictureBox2 (with the "clicked" image) on top of the PictureBox1 Button and set it to "Visible = False"
Code:
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
Timer1.Start()
ProgressBar1.Value = ProgressBar1.Minimum
PictureBox1.Visible = False
PictureBox2.Visible = True
End Sub
Private Sub p(sender As Object, e As EventArgs) Handles Timer1.Tick
If ProgressBar1.Value = 100 Then
PictureBox1.Visible = True
PictureBox2.Visible = False
End If
End Sub
Regards!
We make a living by what we get, but we make a life by what we give.
-
Mar 11th, 2015, 09:04 AM
#6
Re: Lock PictureBox Button Upon Click Till Timer Ends or Else
 Originally Posted by Namorence
Thank you for the quick responses!
Both of you suggested a lot of code tho (some of which I am not familiar with) and I am lookin for a simpler and shorter solution.
Since I just wanted to "lock" the PictureBox to a "clicked" state image only till the timer is running i.e. the ProgressBar. And If I use the "Enabled = False" it completely removes the PictureBox.
I came up with this solution, it may not be the best one, nor the right one, but it works like I wanted to:
(Atention, you may lol)
I've just created another PictureBox2 (with the "clicked" image) on top of the PictureBox1 Button and set it to "Visible = False"
Code:
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
Timer1.Start()
ProgressBar1.Value = ProgressBar1.Minimum
PictureBox1.Visible = False
PictureBox2.Visible = True
End Sub
Private Sub p(sender As Object, e As EventArgs) Handles Timer1.Tick
If ProgressBar1.Value = 100 Then
PictureBox1.Visible = True
PictureBox2.Visible = False
End If
End Sub
Regards!
What you do is up to you but I would recommend that you make an effort to understand the good code that you were provided with rather than using what is, I'm sorry to say, quite poor code. We all write poor code to begin with, so there's no shame in that. Sticking with that poor code when you have been provided a superior alternative that can help you learn is shameful though.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|