Scroll panel auto hide inside components - vb.net

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

Related

Moving PictureBox with Timer

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

Timer not stopping VB.NET

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

Run timer in background in visual basic form

first I'm new in this, and I have this code that shows a prompt to restart or postpone the restart for a while, the issue is that i want to hide the message and bring it back after the time specified by the user.
I'm using a "visual basic form" and the time that restart will be postponed it's selected from a "ComboBox"
My code is as follows.
Imports System.Management
Imports System.Security.Permissions
Imports System
Imports System.IO
Imports System.Collections
Imports System.SerializableAttribute
Public Class Form2
Dim PostponeReboot As Integer = 50
Private Const CP_NOCLOSE_BUTTON As Integer = &H200
Protected Overloads Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim myCp As CreateParams = MyBase.CreateParams
myCp.ClassStyle = myCp.ClassStyle Or CP_NOCLOSE_BUTTON
Return myCp
End Get
End Property
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form1.Hide()
Label4.Text = SystemInformation.UserName
Button1.Enabled = False
ComboBox1.Enabled = False
Timer1.Interval = 1000
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
CheckBox2.Enabled = False
Button1.Enabled = True
ComboBox1.Enabled = False
ElseIf CheckBox1.Checked = 0 Then
CheckBox2.Enabled = True
Button1.Enabled = False
ComboBox1.Enabled = False
End If
End Sub
Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
If CheckBox2.Checked Then
CheckBox1.Enabled = False
ComboBox1.Enabled = True
Button1.Enabled = True
ElseIf CheckBox2.Checked = 0 Then
CheckBox1.Enabled = True
ComboBox1.Enabled = False
Button1.Enabled = False
End If
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.Text = "1 Hora" Then
PostponeReboot = 10
ElseIf ComboBox1.Text = "2 Horas" Then
PostponeReboot = 20
ElseIf ComboBox1.Text = "4 Horas" Then
PostponeReboot = 40
ElseIf ComboBox1.Text = "Seleccione" Then
Button1.Enabled = False
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If CheckBox1.Checked Then
MessageBox.Show("Rebooting")
'Shell("shutdown -r -f -t 60")
Form1.Close()
End
ElseIf CheckBox2.Checked Then
MessageBox.Show(PostponeReboot)
Timer1.Start()
Me.Hide()
End If
If PostponeReboot = 0 Then
Me.Show()
Else
Me.Hide()
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
PostponeReboot = PostponeReboot - 1
'Label5.Text = PostponeReboot
End Sub
End Class
In the first "If" sentence of below I want to start the timer and hide the form, and in the second "If" i want to bring it back the form, but the form remains hidden.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If CheckBox1.Checked Then
MessageBox.Show("Rebooting")
'Shell("shutdown -r -f -t 60")
Form1.Close()
End
ElseIf CheckBox2.Checked Then
MessageBox.Show(PostponeReboot)
Timer1.Start()
Me.Hide()
End If
If PostponeReboot = 0 Then
Me.Show()
Else
Me.Hide()
End If
End Sub
I've tried putting the second "If" sentence in another place but don't work, what I'm doing wrong.
I assume here that your Timer1 class raises the Timer1.Tick event every x time after Timer1.Start() is called. The fact that the form can hide tells me Timer1.Start() isn't a blocking method. As such, your second if statement will be verified right after you hide the form, without waiting for the PostponeReboot variable to reach zero. This particular button handler would then exit and your form would remain hidden. What I see is that you already have an event handler for each tick of your timer. Why not use this handler to verify the state of your PostponeReboot variable?
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
PostponeReboot = PostponeReboot - 1
If PostponeReboot = 0 Then
Timer1.Stop() 'I would assume
Me.Show()
End If
End Sub
Although, I would recommend you to try other solutions, like having your timer raise an event only when it reaches the elapsed time (so you don't have to handle each ticks unnecessarily). I would also recommend looking into an Universal Windows App with Toast Notifications as you could set a Notification to appear at a set time (handled by Windows) so that you don't have to have a thread running in the background for this.

how to move an object to form corner then stop?

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

Slider panel that hides components inside it

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