What event(s) would I use to handle hiding a button/component that is inside this panel? This is a sliding panel that when the user hovers over, it expands and when the mouse exits, it collapses.
The problem is I don't know how to keep the components from showing until it is expanded.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Panel1.Dock = DockStyle.Left
Timer3.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If Panel1.Width < 150 Then
Panel1.Width = Panel1.Width + 100
ElseIf Panel1.Width = 150 Then
Timer1.Enabled = False
End If
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
If Panel1.Width >= 100 Then
Panel1.Width = Panel1.Width - 50
If Panel1.Width < 100 And Panel1.Width > 25 Then
Panel1.Width = Panel1.Width - 1
End If
ElseIf Panel1.Width = 25 Then
Timer2.Enabled = False
End If
End Sub
Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
If Panel1.ClientRectangle.Contains(Panel1.PointToClient(MousePosition)) Then
If Not Timer1.Enabled AndAlso Panel1.Width < 150 Then
Timer1.Enabled = True
Timer2.Enabled = False
End If
Else
If Not Timer2.Enabled AndAlso Panel1.Width > 25 Then
Timer1.Enabled = False
Timer2.Enabled = True
End If
End If
End Sub
End Class
You should probably rename your timers to keep them straight, something like SlideOpenTimer and SlideCloseTimer, etc. Would make it easier to understand what the timers are for.
I re-worked your timer events to hide the ListBox when the closing timer starts and to show the ListBox when the panel reaches its full width:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If Panel1.Width < 100 Then
Panel1.Width += 50
ElseIf Panel1.Width < 150 Then
Panel1.Width += 25
ElseIf Panel1.Width >= 150 Then
Timer1.Enabled = False
ListBox1.Visible = True
End If
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
If ListBox1.Visible Then
ListBox1.Visible = False
End If
If Panel1.Width > 100 Then
Panel1.Width -= 50
ElseIf Panel1.Width > 25 Then
Panel1.Width -= 25
ElseIf Panel1.Width <= 25 Then
Timer2.Enabled = False
End If
End Sub
Related
I want to create a program wherein when I click a Button, the PictureBox that is of the same width and height as the form moves down but I want the Timer to stop right after the PictureBox leaves the frame/form. And when I click another Button, the PictureBox will move back up but it will stop when it's at the center of the form, basically at the same location it was before moving down. The form's size is 700, 1000 if that helps. This is my code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + 9)
If (PictureBox1.Location = New Point(700, 1100)) Then
Timer1.Enabled = False
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Timer2.Enabled = True
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - 9)
If (PictureBox1.Location = New Point((Me.Width / 700) - (PictureBox1.Width / 700), (Me.Height / 1000) - (PictureBox1.Height / 1000))) Then
Timer2.Enabled = False
End If
End Sub
Let's assume your PictureBox starts in the top, left corner of the containing control (i.e. the Form, or a Panel, or whatever). This is Point(0,0).
In this event handler...
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + 9)
If (PictureBox1.Location = New Point(700, 1100)) Then
Timer1.Enabled = False
End If
End Sub
...you are checking if the top left corner of PictureBox1 is at position 700,1100 instead of checking if it is at 0,1100. Also, since you're adding + 9 each timer tick, it'll never be at a Y position of exactly 1100.
And then in this event...
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - 9)
If (PictureBox1.Location = New Point((Me.Width / 700) - (PictureBox1.Width / 700), (Me.Height / 1000) - (PictureBox1.Height / 1000))) Then
Timer2.Enabled = False
End If
End Sub
You want to check if PictureBox1.Location is now 0,0 (the starting position) instead of all of that position math you are doing.
Here is a cleaned-up version of your code. Note that it first checks the position of the PictureBox and only moves it if necessary.
Private Const INCREMENT As Integer = 9
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If PictureBox1.Location.Y >= 1100 Then
Timer1.Enabled = False
Else
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + INCREMENT)
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Timer2.Enabled = True
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
If PictureBox1.Location.Y <= 0 Then
Timer2.Enabled = False
Else
PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - INCREMENT)
End If
End Sub
I'm making a simple Windows Forms App with Visual Studio 2019. I want to use a timer and a progress bar but i can't make the timer stop.
I've tried Timer1.Stop() and Timer1.Enabled = False but neither of them have worked. The timer waits 1 second.
Here's the full code:
(it has changed a lot but I still have the problem)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Button1.Text = "Stop" Then
ProgressBar1.Value = 0
Timer1.Enabled = False
End If
Button1.Text = "Stop"
Timer1.Enabled = True
Cursor = Cursors.AppStarting
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If Timer1.Enabled = True Then
ProgressBar1.Increment(1)
Else
ProgressBar1.Value = 0
End If
If ProgressBar1.Value = 100 Then
Timer1.Stop()
Button1.Text = "Done!"
End If
End Sub
I strongly suggest that you use a CheckBox rather than a Button. If you set the Appearance to Button then it will look just like a regular Button but you can use the Checked property to represent state. The control will appear depressed when Checked is True. You can then use code like this:
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
Timer1.Enabled = CheckBox1.Checked
CheckBox1.Text = If(CheckBox1.Checked, "Stop", "Start")
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.PerformStep()
If ProgressBar1.Value = ProgressBar1.Maximum Then
CheckBox1.Checked = False
End If
End Sub
What this code should do is move an object to a chosen corner of the form, by clicking it, then it should stop when destination is reached, without using the (. location) property.
but in my code here, it keeps moving beyond the form border.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If Button1.Left = (Me.Width - Button1.Width) Then
Timer1.Enabled = False
Else
Button1.Left += 10
Button1.Top -= 10
End If
End Sub
You're incrementing by 10, not 1, so most likely you are passing the value Me.Width - Button1.Width without ever exactly equaling it.
Check to see if you're greater than your target, and use Me.ClientRectangle.Width instead of Me.Width:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim RightReached As Boolean = False
If Button1.Left >= (Me.ClientRectangle.Width - Button1.Width) Then
Button1.Left = Me.ClientRectangle.Width - Button1.Width
RightReached = True
Else
Button1.Left += 10
End If
Dim TopReached As Boolean = False
If Button1.Top <= 0 Then
Button1.Top = 0
TopReached = True
Else
Button1.Top -= 10
End If
If RightReached AndAlso TopReached Then
Timer1.Stop()
End If
End Sub
My code works, but The problem is I put a listbox in it. I want to make sure that even when the user hovers over the list box, the pane will close.
I am having 2 issues:
MAIN ISSUE: The panel's timers are not working because the listbox component is sort of blocking it out. I am unsure how to counteract this.
I want to hide or collapse the listbox for visual appeal.
Here is my code:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Panel1.Dock = DockStyle.Left
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If Panel1.Width < 150 Then
Panel1.Width = Panel1.Width + 100
ElseIf Panel1.Width = 150 Then
Timer1.Enabled = False
End If
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
If Panel1.Width >= 100 Then
Panel1.Width = Panel1.Width - 50
If Panel1.Width < 100 And Panel1.Width > 25 Then
Panel1.Width = Panel1.Width - 15
End If
ElseIf Panel1.Width <= 25 Then
Timer2.Enabled = False
End If
End Sub
Private Sub Panel1_MouseEnter(sender As Object, e As EventArgs) Handles Panel1.MouseEnter
Timer1.Enabled = True
Timer2.Enabled = False
End Sub
Private Sub Panel1_MouseLeave(sender As Object, e As EventArgs) Handles Panel1.MouseLeave
Timer1.Enabled = False
Timer2.Enabled = True
End Sub
End Class
The ListBox will cause the panel's MouseEnter and MouseLeave events to fire.
So comment out those events:
'Private Sub Panel1_MouseEnter(sender As Object, e As EventArgs) Handles Panel1.MouseEnter
' Timer1.Enabled = True
' Timer2.Enabled = False
'End Sub
'Private Sub Panel1_MouseLeave(sender As Object, e As EventArgs) Handles Panel1.MouseLeave
' Timer1.Enabled = False
' Timer2.Enabled = True
'End Sub
Then try adding a third timer to track the mouse over the panel:
Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
If Panel1.ClientRectangle.Contains(Panel1.PointToClient(MousePosition)) Then
If Not Timer1.Enabled AndAlso Panel1.Width < 150 Then
Timer1.Enabled = True
Timer2.Enabled = False
End If
Else
If Not Timer2.Enabled AndAlso Panel1.Width > 25 Then
Timer1.Enabled = False
Timer2.Enabled = True
End If
End If
End Sub
I want a timer to run on a form whose value is derived from a database. for example when i store the value for the timer in a variable, i multiply it by 60000 to convert milliseconds to minutes. Here is the code:
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim x as integer
details = ds.Tables(0).Rows(0).Item("Time")
Timer1.Interval =details * 60000
Timer1.Enabled = True
End Sub()
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
seconds.Text = x.ToString
If x= 0 Then
Timer1.Enabled = False
MessageBox.Show("You didn't finish in time.", "Sorry")
Me.Close()
Else
x-= 1
End If
End Sub
When i run this code, the timer runs in the background and when time's up, the form closes. But it doesn't show in the label seconds when timer is ticking. I hope you get what I mean :)
If you want the label to tick the seconds down to 0, you need the timer interval to be 1000, no matter how long you want this to run for.
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
details = ds.Tables(0).Rows(0).Item("Time")
Timer1.Interval =1000
endTime = DateTime.Now.AddMinutes(details)
Timer1.Enabled = True
End Sub()
Dim endTime As DateTime
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If DateTime.Now > endTime Then
Timer1.Enabled = False
MessageBox.Show("You didn't finish in time.", "Sorry")
Me.Close()
Else
Seconds.Text = (DateTime.Now - endTime).ToString("hh\:mm\:ss")
End If
End Sub
This happens because your timer ticks at Minutes interval. If you want to show seconds in the label then your timer should tick within 1 second interval (maybe a few milliseconds etc.)
So instead of setting Timer1.Interval =details * 60000, set x = details * 60 and Timer1.Interval to 1000.
There is also variable scoping problem with your code. You have declared x as local variable inside Form1_Load, which means that it won't be available inside the Timer1_Tick. Move it outside the procedure.
Dim x As Integer
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Details = ds.Tables(0).Rows(0).Item("Time")
Timer1.Interval = 1000
x = Details * 60
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
seconds.Text = x.ToString
If x = 0 Then
Timer1.Enabled = False
MessageBox.Show("You didn't finish in time.", "Sorry")
Me.Close()
Else
x-= 1
End If
End Sub
UPDATED
This is assuming that your database saved value is in Milliseconds. Otherwise modify appropriately.
Dim ticker As TimeSpan
Public Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Details = ds.Tables(0).Rows(0).Item("Time")
Timer1.Interval = 1000
ticker = TimeSpan.FromMilliseconds(Details)
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
seconds.Text = String.Format("{0:00}:{1:00}:{2:00}", ticker.Hours, ticker.Minutes, ticker.Seconds)
If ticker.Ticks <= 0 Then
Timer1.Enabled = False
MessageBox.Show("You didn't finish in time.", "Sorry")
Me.Close()
Else
ticker = ticker.Subtract(TimeSpan.FromSeconds(1))
End If
End Sub
This may not be completely what you may want, as I didn't convert the time or anything I just made it as basic as possiable and that way you should be able to work from it :)
Public Class Form1
Dim Time As Integer = 100 'Your time in seconds goes here!
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lblTime.Text = Time
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Time = Time - 1
lblTime.Text = Time
If Time = 0 Then
Timer1.Enabled = False
MsgBox("You didn't finish in time.", "Sorry")
Me.Close()
End If
End Sub
End Class
Any problems just message me