Results 1 to 6 of 6

Thread: [RESOLVED] Lock PictureBox Button Upon Click Till Timer Ends or Else

  1. #1

    Thread Starter
    New Member Namorence's Avatar
    Join Date
    Oct 2013
    Location
    Home
    Posts
    10

    Resolved [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.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Lock PictureBox Button Upon Click Till Timer Ends or Else

    can you show us your picturebox click event?

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Public Class Form1
    2.  
    3.     ''' <summary>
    4.     ''' Indicates whether or not each PictureBox is locked.
    5.     ''' </summary>
    6.     Private lockedStateByPictureBox As Dictionary(Of PictureBox, Boolean)
    7.  
    8.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    9.         'All PictureBoxes are initially unlocked.
    10.         Me.lockedStateByPictureBox = New Dictionary(Of PictureBox, Boolean) From {{Me.PictureBox1, False},
    11.                                                                                   {Me.PictureBox2, False},
    12.                                                                                   {Me.PictureBox3, False}}
    13.     End Sub
    14.  
    15.     Private Sub PictureBoxes_MouseEnter(sender As Object, e As EventArgs) Handles PictureBox3.MouseEnter,
    16.                                                                                   PictureBox2.MouseEnter,
    17.                                                                                   PictureBox1.MouseEnter
    18.         Dim pb = DirectCast(sender, PictureBox)
    19.  
    20.         If Not Me.lockedStateByPictureBox(pb) Then
    21.             'This PictureBox is not locked so indicate that the cursor is over it.
    22.             pb.BackColor = Color.Red
    23.         End If
    24.     End Sub
    25.  
    26.     Private Sub PictureBoxes_MouseLeave(sender As Object, e As EventArgs) Handles PictureBox3.MouseLeave,
    27.                                                                                   PictureBox2.MouseLeave,
    28.                                                                                   PictureBox1.MouseLeave
    29.         Dim pb = DirectCast(sender, PictureBox)
    30.  
    31.         If Not Me.lockedStateByPictureBox(pb) Then
    32.             'This PictureBox is not locked so indicate that the cursor is not over it.
    33.             pb.BackColor = Color.Green
    34.         End If
    35.     End Sub
    36.  
    37.     Private Sub PictureBoxes_Click(sender As Object, e As EventArgs) Handles PictureBox3.Click,
    38.                                                                              PictureBox2.Click,
    39.                                                                              PictureBox1.Click
    40.         Dim pb = DirectCast(sender, PictureBox)
    41.  
    42.         If Not Me.lockedStateByPictureBox(pb) Then
    43.             'Unlock any currently locked PictureBox(es).
    44.             Unlock()
    45.  
    46.             'Lock the PictureBox that was just clicked.
    47.             Lock(pb)
    48.         End If
    49.     End Sub
    50.  
    51.     Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    52.         'Unlock any currently locked PictureBox.
    53.         Unlock()
    54.     End Sub
    55.  
    56.     ''' <summary>
    57.     ''' Locks a PictureBox.
    58.     ''' </summary>
    59.     ''' <param name="pb">
    60.     ''' The PictureBox to lock.
    61.     ''' </param>
    62.     Private Sub Lock(pb As PictureBox)
    63.         'Indicate that the PictureBox is locked.
    64.         pb.BackColor = Color.Blue
    65.         Me.lockedStateByPictureBox(pb) = True
    66.  
    67.         'Start the unlock Timer.
    68.         Me.Timer1.Start()
    69.     End Sub
    70.  
    71.     ''' <summary>
    72.     ''' Unlocks any locked PictureBox.
    73.     ''' </summary>
    74.     Private Sub Unlock()
    75.         'Stop the unlock Timer.
    76.         Me.Timer1.Stop()
    77.  
    78.         For Each pb In Me.lockedStateByPictureBox.Keys.ToArray()
    79.             If Me.lockedStateByPictureBox(pb) Then
    80.                 'Unlock the locked PictureBox.
    81.                 Me.lockedStateByPictureBox(pb) = False
    82.  
    83.                 If Me.RectangleToScreen(pb.Bounds).Contains(Cursor.Position) Then
    84.                     'Indicate that the cursor is over the PictureBox.
    85.                     pb.BackColor = Color.Red
    86.                 Else
    87.                     'Indicate that the cursor is not over the PictureBox.
    88.                     pb.BackColor = Color.Green
    89.                 End If
    90.  
    91.                 'Only one PictureBox can ever be locked so look no further.
    92.                 Exit For
    93.             End If
    94.         Next
    95.     End Sub
    96.  
    97. End Class
    Last edited by jmcilhinney; Mar 11th, 2015 at 12:18 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member Namorence's Avatar
    Join Date
    Oct 2013
    Location
    Home
    Posts
    10

    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Lock PictureBox Button Upon Click Till Timer Ends or Else

    Quote Originally Posted by Namorence View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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
  •  



Click Here to Expand Forum to Full Width