Timer acting weird - vb.net

Hi guys I have another problem which is all about the timer..the form that I would want to show for 3 seconds is only showing for about half a second. Please help me thanks in advance
Main form code:
Private Sub submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles submit.Click
'question 1
If Label1.Text = "Who invented the airplane?" And TextBox1.Text = "third" Then
Label2.Text = (Label2.Text) + 1
correctmsg.Show()
correctmsg.Hide()
Label1.Text = "Who invented the telephone?"
Return 'Don't do any more checks this time around
ElseIf Label1.Text = "Who invented the airplane?" Then
'Reason ElseIf (In case the question was 'who invented the telephone' then the first errormessage should not not be shown)
wrongmsg.Show()
Return
End If
Splash form code:
Public Class correctmsg
Private Sub correctmsg_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim mysplash As New correctmsg
mysplash.Timer1.Interval = 3000
End Sub
End Class

Something like this:
Public Class correctmsg
' correctmsg == mysplash ??? so this is a form??
Private Sub correctmsg_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 3000 ' could be a timer here
End Sub
Public Sub ShowMsg
Timer1.Enabled = true
me.Visible = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
Me.Visible = False
End Sub
End Class
I am just showing the form by making it visible. No need to make a new form. When the timer expires, hide the form and disable the timer. To use it:
correctmsg.ShowMsg
' this was hiding the form as soon as it shows:
'correctmsg.Hide()

Related

Mouse Over picture on VB and make it stay when clicked

So I make a form like this
Form1
The picture box will show neutral.png when form is loaded, so
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.Image = Image.FromFile("Images\neutral.png")
End Sub
I make the image on picturebox1 change to x.png while mouse over and disappear when mouse leave, so
Private Sub PictureBox1_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseEnter
PictureBox1.Image = Image.FromFile("Images\x.png")
End Sub
Private Sub PictureBox1_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
PictureBox1.Image = Image.FromFile("Images\neutral.png")
End Sub
My question is how I make the x.png image stay on the picturebox1 when I click the picturebox1. Doing this
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
PictureBox1.Image = Image.FromFile("Images\x.png")
End Sub
Doesn't seem work since the mouseleave event still have an effect.
There is example, from my comment
Public Class Form1
Private isClicked As Boolean = False
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
PictureBox1.Image = Image.FromFile("images\neutral.jpg")
End Sub
Private Sub PictureBox1_Click(sender As Object, e As System.EventArgs) Handles PictureBox1.Click
If isClicked = False Then isClicked = True Else isClicked = False 'if You click again, everything back
'isClicked = True 'in this case, when You click "x.jpg" will stay always
PictureBox1.Image = Image.FromFile("images\x.jpg")
End Sub
Private Sub PictureBox1_MouseEnter(sender As Object, e As System.EventArgs) Handles PictureBox1.MouseEnter
If isClicked = False Then 'picturebox isn't clicked, so show x.jpg ... otherwise x.jpg will be showed always
PictureBox1.Image = Image.FromFile("images\x.jpg")
End If
End Sub
Private Sub PictureBox1_MouseLeave(sender As Object, e As System.EventArgs) Handles PictureBox1.MouseLeave
If isClicked = False Then 'picturebox isn't clicked, so show neutral image ... otherwise x.jpg will be showed always
PictureBox1.Image = Image.FromFile("images\neutral.jpg")
End If
End Sub
End Class
Use some variable, in this example is isClicked.
Always make checking under MouseEnter and MouseLeave is picturebox clicked or not.
By this example, You can click again on picturebox to, let's say, reset that isClicked, so You can have again neutral and x images.

Do Process while Progress Bar is Running

I know that knowledge is expensive, but is there who want to help me
i want to run process when progress bar is running,
i try with this code
Public Class Form2
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Call Prcss()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Progress.Value < 100 Then
Progress.Value += 2
ElseIf Progress.Value = 100 Then
Timer1.Stop()
Form1.Show()
Me.Hide()
End If
End Sub
Private Sub Prcss()
With Progress
.Value = 0
Threading.Thread.Sleep(450)
Label1.Text = "Renewing Custom Content"
.Value = 20
Threading.Thread.Sleep(450)
Label1.Text = "Getting Information"
.Value = 50
Threading.Thread.Sleep(450)
Label1.Text = "Downloading Udpdate"
.Value = 70
Threading.Thread.Sleep(450)
Label1.Text = "Ready to Start"
.Value = 100
End With
End Sub
i don't know where is my mistake, i read this on my book.
Try using a BackgroundWorker or a Thread:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
For i As Integer = 1 To 1000
BackgroundWorker1.ReportProgress(CInt(i / 10))
Threading.Thread.Sleep(500)
Next
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
MessageBox.Show("Finished!")
End Sub
This is the basic example and require you to add a BackgroundWorker to the form. You can see help here: https://msdn.microsoft.com/es-es/library/cc221403(v=vs.95).aspx

How to make a picturebox hide when dragged over another picturebox?

So I nailed the ability to drag a picturebox around the Windows form. I need it to hide itself when it's dragged and dropped over another picture. I've tried a few methods but none seem to work and I am now back at square one, the only code I have is so that I can move the picturebox around the form.
I Solved Your Problem
Here Is How The Form Should Look Like
And Here Is The Code I Made:
Private Sub CheckTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckTimer.Tick
CheckTimer.Stop()
CheckTimer.Interval = 1
If PictureBox2.Location = New System.Drawing.Point(TextBox1.Text, TextBox2.Text) Then
PictureBox2.Visible = False
End If
CheckTimer.Start()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = PictureBox1.Location.X
TextBox2.Text = PictureBox1.Location.Y
CheckTimer.Start()
End Sub
Hope This Code Was Helpful To You.
I made an better code than the old one, try it.
Here Is How The Form Should Look Like:
and here is the code :
Public Class Form1
Dim Point As New Point
Dim X, Y As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
Timer1.Interval = 1
Point = Cursor.Position
PictureBox2.Location = New System.Drawing.Point(Point.X - X, Point.Y - Y)
Timer1.Start()
End Sub
Private Sub PictureBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseDown
X = Cursor.Position.X - PictureBox2.Location.X
Y = Cursor.Position.Y - PictureBox2.Location.Y
Timer1.Start()
End Sub
Private Sub PictureBox2_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseUp
Timer1.Stop()
If PictureBox2.Location.X < PictureBox1.Size.Width Then
PictureBox2.Visible = False
End If
End Sub
End Class
I hope this code was useful to you.

Button Click Event Within Another

I have two buttons Start button and Stop button .I run my program by clicking on start button. I want to stop the program during the start button . But the program will not responde until the start buttun finish its job. How i can do check the stop buttun during the start. i heard about threading but i do not know how i can do it.
Private Sub Button_Start (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
//my code
//check always if the user push stop if no continue if yes go to this sub
//my code
end sub
Private Sub Button_Stop (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
stopClick = True
Dim Response As Integer
Response = MessageBox.Show("Do you really want to exit?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If Response = vbYes Then
Me.Close()
End If
End Sub
you can use threading put button1 code in a function and use the thread .
you can refer to this example
'Thread created to handle the Background process for start_function
Dim t As System.Threading.Thread
Private Sub start_function()
While True
'your code here for Eg:
Dim i As Integer
i = i + 1
End While
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
t = New System.Threading.Thread(AddressOf Me.start_function)
t.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
t.Abort()
End Sub
Drag a backgroundworker component onto your form.
Imports System.ComponentModel
Public Class Form1
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
Me.Text = "Busy Doing Work"
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
Me.Text = "Asking to Cancel"
BackgroundWorker1.CancelAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
While Not BackgroundWorker1.CancellationPending
System.Threading.Thread.Sleep(1000)
End While
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Me.Text = "Cancelled"
End Sub
End Class

Close form after set time

The code below allows me to fade in and out when it opens and closes, which is what I want. However, I would like my form to remain open for 10 seconds before the fading starts. I am struggling to get that part done.
Here is what I have so far:
Public Class frmDefinitions
Private Sub Button1_Click(sender As Object, e As EventArgs) _
Handles Button1.Click
tmr_out.Enabled = True
End Sub
Private Sub frmDefinitions_Load(sender As Object, e As EventArgs) _
Handles MyBase.Load
Me.Opacity = 100
tmr_in.Enabled = True
End Sub
Private Sub tmr_in_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles tmr_in.Tick
Me.Opacity += 0.05
If Me.Opacity = 1 Then
tmr_in.Enabled = False
End If
End Sub
Private Sub tmr_out_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles tmr_out.Tick
Me.Opacity -= 0.05
If Me.Opacity = 0 Then
tmr_out.Enabled = False
Me.Close()
End If
End Sub
End Class
You will need to setup a third Timer to delay the start of your tmr_out Timer. I would trigger the delay as soon as your tmr_in is disabled. You should then get your 10 second delay before you start your fade out. You could also try to use the Form's Shown event to start the Delay but you would need to adjust the 10 seconds to accommodate the fade in delay.
Public Class Form1
Dim tmrDelay As New Timer()
Public Sub New()
InitializeComponent()
tmrDelay.Interval = 10000
AddHandler tmrDelay.Tick, AddressOf tmrDelay_Tick
End Sub
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Me.Opacity = 0
tmr_in.Enabled = True
End Sub
Private Sub tmrDelay_Tick(sender As System.Object, e As System.EventArgs)
tmrDelay.Stop()
tmr_out.Start()
End Sub
Private Sub tmr_in_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_in.Tick
Me.Opacity += 0.05
If Me.Opacity = 1 Then
tmr_in.Enabled = False
tmrDelay.Start() 'Start Your 10 second delay here.
End If
End Sub
Private Sub tmr_out_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_out.Tick
Me.Opacity -= 0.05
If Me.Opacity = 0 Then
tmr_out.Enabled = False
Me.Close()
End If
End Sub
End Class