I have little dummy software to test the precision of the stopwatch in visual basic. I've been told that visual basic has a real good timing, but I'm experiencing some strange behaviors.
This is ALL my code:
Imports System.IO
Public Class Form1
Public tmrTime As New Stopwatch
Dim currentDate As Date
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
tmrTime.Start()
End Sub
Private Sub Form1_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
TextBox1.Text = (tmrTime.ElapsedTicks / Stopwatch.Frequency) * 1000
End Sub
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
TextBox2.Text = (tmrTime.ElapsedTicks / Stopwatch.Frequency) * 1000
End Sub
End Class
The problem si that if I take the non-decimal part of the two textbox (which is the absolute time of pressing and the abs. time of releasing) they are almost ALWAYS coupled, that is BOTH ODD or BOTH EVEN.
Do you know what's happening?
I have the same result using tmrTime.ElapsedMilliseconds or tmrTime.Elapsed.Ticks :-\
There is a good article about StopWatch/Timer precision in .Net here on a MSDN blog. If I correctly understand what you want to do, this should solve your problem:
Public Class Form1
Private _sw As New Stopwatch
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
_sw.Start()
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
Dim elapsed As Long
elapsed = _sw.ElapsedTicks
_sw.Stop()
tbTicks.Text = (elapsed / Stopwatch.Frequency) * 1000
_sw.Reset()
End Sub
End Class
The code posted from Georg need a upgrade:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
Related
I've noticed something when I use Mousehover event, that it is slower in some milliseconds.
I mean that when I point my cursor on it, it is few milliseconds slower. please help me How to fix this
Here's my code:
Public Class Form1
Private Sub Button1_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.MouseHover
Button1.ForeColor = Color.White
End Sub
Private Sub Button1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave
Button1.ForeColor = Color.Black
End Sub
End Class
Please help me.
use MouseMove event instead of MouseHover, that will do it for you. It gives you an immediate response.
Code:
Private Sub Button1_MouseMove(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.MouseMove
Button1.ForeColor = Color.White
End Sub
Best wishes!
-BHARATHI Tutorials
I'm trying to make a little prank program where a form keeps opening until it hits a certain number like 50 or something, I've tried this:
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Show()
End Sub
End Class
But that doesn't work, anyone willing to help? Thanks
Ditto what litelite said. If you want to show a new instance of the form, Show() won't cut it.
In fact, once you close that form, the timer you're using will be lost. So you'll need to handle the Form.Closing event as well. Since we're just having fun here, I'd suggest that you make that work like cutting off the head of a Hydra, two more replace it.
Try something like this:
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim _newForm as Form2 = new Form2()
_newForm.Show()
End Sub
Private Sub Me_Closing(sender As Object, e As EventArgs) Handles Form2.Closing
Dim _newForm as Form2 = new Form2()
_newForm.Show()
Dim _newForm2 as Form2 = new Form2()
_newForm2.Show()
End Sub
End Class
Hello Stack overflow community, I am here to get some help for a tool that I am programming in VB.net, I am using Visual Studio 2015 and what I am working on right now is a tool that basically creates a backup of a folder, it basically copies all files from a path that it's given in TextBox1 to the path that it's given in TextBox2, when the user presses the button "Create", it will execute this code:
My.Computer.FileSystem.CopyDirectory(TextBox1.Text, TextBox2.Text, True)
It works perfectly but what I want to do is, the progress bar that I have added to be updated during the copy paste process so the user can know that it's actually working.
The whole code is here:
Public Class Form4
Private Sub FolderBrowserDialog1_HelpRequest(sender As Object, e As EventArgs)
End Sub
Private Sub FolderBrowserDialog2_HelpRequest(sender As Object, e As EventArgs)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FolderBrowserDialog1.ShowDialog()
End Sub
Private Sub FolderBrowserDialog1_Disposed(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = FolderBrowserDialog1.SelectedPath.ToArray
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
FolderBrowserDialog2.ShowDialog()
End Sub
Private Sub FolderBrowserDialog2_Disposed(sender As Object, e As EventArgs) Handles Button2.Click
TextBox2.Text = FolderBrowserDialog2.SelectedPath.ToArray
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
My.Computer.FileSystem.CopyDirectory(TextBox1.Text, TextBox2.Text, True)
TextBox1.Clear()
TextBox2.Clear()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Me.Hide()
Form3.Show()
End Sub
End Class
If there's anything else that I haven't explained, let me know, thank you.
I want to block the key board input when the form starts, but it is not working, please check my code:
Public Class Form2
Public Declare Function BlockInput Lib "User32" (ByVal fBlock As Integer) As Integer
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim name As String
name = "12345"
If (name = TextBox1.Text) Then
Me.Close()
End If
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Visible = True
TextBox1.Focus()
End Sub
Private Sub TextBox2_Click_1(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox2.MouseClick
BlockInput(1)
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
End Class`
I used "groupbox.enabled = False" in order to stop program users from editing anything within the groupbox on my form. You can then enable the groupbox again providing certain conditions are met. Another method I've used in my programming for VB.net was setting the read-only property (I used to to auto-populate the system date to prevent users from entering false dates) I don't know if either of those methods might help you?
i am getting really annoyed by this piece of code for visual basic
please can you help. i have looked on YouTube and everywhere else even the vb website!!! i would really appreciate it if someone could help me out
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show("Hi!!")
Timer1.Start()
End Sub
Private Sub ProgressBar1_Click(sender As Object, e As EventArgs) Handles ProgressBar1.Click
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(1)
If ProgressBar1.Value = 100 Then
Timer1.Stop()
MsgBox("Jeff")
End If
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs)
End Sub
Private Function GetNumericUpDown1(v As Integer) As Boolean
End Function
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
If ( : ( ) Then
MessageBox.Show("Well Done!")
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
MessageBox.Show("!!Stopped!!")
Timer2.Stop()
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
ProgressBar2.Increment(0.6)
If ProgressBar2.Value = 100 Then
Timer2.Stop()
MsgBox("Jeff")
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
MessageBox.Show("!!Started!!")
Timer2.Start()
End Sub
Private Sub SplitContainer1_Panel1_Paint(sender As Object, e As PaintEventArgs) Handles SplitContainer1.Panel1.Paint
End Sub
Private Sub NotifyIcon1_MouseDoubleClick(sender As Object, e As MouseEventArgs)
End Sub
Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs)
End Sub
Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
ProgressBar3.Increment(1)
If ProgressBar3.Value = 100 Then
Timer3.Stop()
End If
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Timer3.Start()
End Sub
Private Sub ProgressBar3_Click(sender As Object, e As EventArgs) Handles ProgressBar3.Click
End Sub
End Class
BTW:this is my first post so it is probably rubbish!!
I don't have enough points to comment.
First off, as others have said we can't recreate this since we have no idea what your Form looks like or what you are expecting it to do.
Secondly, you should turn on Option Strict in the Compile section of Project properties to avoid technical errors like ProgressBar2.Increment(0.6) since 0.6 isn't a valid integer.
I threw together a TabControl (which you never mentioned in the OP) with 3 tabs and the various buttons and progress bars you list in what seemed like a logical fashion to me and it ran just fine for what code you have. Clicked the button on each tab and each progress bar eventually got to 100%. I have no idea what else you were expecting.
If you remove all the empty subs and action listeners, it would be easier to detect the problem