I want to set a time like 2:00:00 PM and run it by minutes. But I'm not getting the current time on my computer I just want to set a time when I entered a time in textbox and show it to the label then it will run and also the interval of my timer is equal to 1.
I tried this code but nothing happened. can anyone help me thank you
Private Sub Form_Load()
timer1.enabled = true
End Sub
Private Sub Timer1_Timer()
Label1.Caption = "2:00:00 PM"
End Sub
It doesn't look like you have set the interval property for the timer, unless you have set it in the properties.
If you add timer1.interval = 60000 above the timer1.enabled it should fire the timer event after 1 minute.
You can find some more information on it at VB6 Timer interval property
Set the initial value of the Label1 caption in Form_Load(). Make sure the interval of the timer is set properly. In the Timer1_Timer() event, update the caption of Label1. To make sure you're showing the absolutely right value, you should not assume the timer fires perfectly at every interval. Instead, in Form_Load(), set a module level variable with current time. When the timer fires, get the difference between the current time (when timer fires) and the module level variable. Increment the initial value of the label ("2:00:00 PM" in the example you gave) by this difference and update the label to show that new value.
Related
I have programmed a traffic light on visual basic 6.0 but I cant seem to program the countdown(10-0) to be on infinite loop at every light i.e it should countdown from 10 to 0 before each light shows.
Here is my code
Private Sub Timer2_Timer()
If Label1. Caption = 0 Then
Timer2.Enabled = False
MsgBox ("go")
Else
Label1. Caption = Label1. Caption - 1
End If
End Sub
the countdown continues to 1,-2,-3,-4 and so on but I want it to start from 10 again after counting from 10 to 0. How do I put it on endless loop on visual basic 6.0
Instead of:
Timer2.Enabled = False
do something like:
Label1.Caption = 10
That way the timer will keep running forever, and will reset to 10 every time it reaches 0.
Also, to make the code a little more robust, you might take this approach:
Private Const maxCounter As Integer = 10
Private counter As Integer
Private Sub form_Load()
counter = maxCounter
End Sub
Private Sub Timer2_Timer()
If counter = 0 Then
counter = maxCounter
MsgBox ("go")
Else
counter = counter - 1
End If
Label1.Caption = counter
End Sub
One advantage is that your code does not depend on how you set the Caption property at design time. And secondly it is a little more modular and understandable in that the functional logic (how the counter is managed) and the display logic (how the caption is set) are mostly distinct.
Also, using a constant maxCounter just means you don't have to repeat the value in different places, and it can be changed easily without introducing discrepencies.
Obviously this is a small program but these are good programming principles to think about which benefit larger programs tremendously.
I have a local sub that allows the user to move a row of a datagridview, triggered by a button click. The sub works fine in debugger but when it exits control is transfered to the calling form, i.e. the current form is closed. This also happens when no row is moved, i.e. when one of the abort conditions on entrance are met. Simply: exiting this sub will close the form!?!
Private Sub btnMove_Click(sender As Object, e As EventArgs) Handles btnMove.Click
Dim rowToGo As DataGridViewRow
Dim rtgIndex As Integer = 0
If (dgvAuftrag.RowCount <= 1) or (dgvAuftrag.CurrentRow Is Nothing) Then
Beep()
Exit Sub
End If
rowToGo = dgvAuftrag.CurrentRow
rtgIndex = rowToGo.Index + 1
If (rtgIndex >= dgvAuftrag.RowCount) Then rtgIndex = 0
Try
dgvAuftrag.Rows.Remove(rowToGo)
dgvAuftrag.Rows.Insert(rtgIndex, rowToGo)
Catch ex As Exception
IssueErrorMessage(ex)
End Try
End Sub
All other local subs and functions work normal, just this one behaves strange. Any ideas how to fix/avoid this bug?
This is not a solution of the problem but a functioning workaround based on the sugestion of Hans. I have introduced a global boolean var named OKtoExit which is intialized to false.
private OKtoExit as boolean = false
Then I have a new FormClose event handler that checks that var. If OKtoExit is false then e.Cancel = true and the handler exits. The regular Exit functions (Save and Quit) set OKtoExit to true, any other code leaves the values unchanged.
Private Sub Current_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If Not exitOK Then
e.Cancel = True
Exit Sub
End If
End Sub
As I said, this is just a workaround that has the same effect as a normal functioning VB-Code. I would appreciate if somebody could present a real solution!
After many months I discovered the true reason for the problem and I must give all credits to Hans Passant: I had a button on one of the first forms that had the Dialog Result property set to Cancel. This was a really beautyful button so many other buttons in the appliaction were a copy of this first button where I just modified the label. Thus they all led to the unwanted behavior that a form was closed as soon as a user clicked one of them no matter what the label said... After months I discovered that just by chance. Thanks to Hans again, I obviously overlooked his last hint "And look at the button's DialogResult property."!
I'm using:
var alarm = NSUserNotification()
var currentTime = NSDate()
alarmTime = currentTime.dateByAddingTimeInterval(60)
alarm.deliveryDate = alarmTime
NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification(alarm)
To get a notification that will fire an hour from the current time, the problem is I want the app to automatically set up another alarm after the first one finishes. NSTimer doesn't seem like it will work because once the app goes to the background it kills the timer. What can I do to achieve this? Can I piggy back another method onto a NSUserNotification
Edit: it also needs to be dynamic, I can't just set the repeat variable of the notification because I need to be able to switch how far out the alarm will be each time it resets.
You just have to set your deliveryRepeatInterval and deliveryTimeZone, as follow:
// set your deliveryTimeZone to localTimeZone
alarm.deliveryTimeZone = NSTimeZone.localTimeZone()
// The date components that specify how a notification is to be repeated.
// This value may be nil if the notification should not repeat.
// alarm.deliveryRepeatInterval = nil
// The date component values are relative to the date the notification was delivered.
// If the calendar value of the deliveryRepeatInterval is nil
// the current calendar will be used to calculate the repeat interval.
// alarm.deliveryRepeatInterval?.calendar = NSCalendar.currentCalendar()
// to repeat every day, set .deliveryRepeatInterval.day to 1.
alarm.deliveryRepeatInterval?.day = 1
// to repeat every hour, set .deliveryRepeatInterval.hour to 1.
alarm.deliveryRepeatInterval?.hour = 1
alarm.deliveryDate = NSDate().dateByAddingTimeInterval(60)
NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification(alarm)
Is there a way when I click on a ToolStripButton, it shows the WaitCursor and updates the StatusStrip for about 10 seconds, then returns back to normal. I just don't know how to type in the coding.
If someone could guide me through the process. (or even give me the code)
Thank You
J Mahone
Using a timer:
1:Add a timer from your component toolbox to your form.
2:Set the inteval to 10,000 (this is in milliseconds, 1000 = 1 second)
3:In the timers' "Tick" event, write this code:
Timer1.stop 'This assumes your timer it named Timer1
Me.Cursor = Cursors.Default
4: When you want to make it show the cursor, either have a method to do both these lines and call the method or just write these 2 lines all over the place:
Me.Cursor = Cursors.WaitCursor
Timer1.Start
I'd suggest to use async/await to keep the "normal" program flow.
Me.Cursor = Cursors.WaitCursor
Await Task.Delay(10000)
Me.Cursor = Cursors.Default
I have an application written in VBA for Excel that takes in a live data feed. Whenever there is a change in the data, various events are triggered within VBA.
I also have some UserForms with ComboBoxes on them. My problem is that when I click the down arrow on the ComboBox and try to make a selection, the moment I get an update from the data feed, the ComboBox resets. What I would like to do is pause the events while I am making my selection in the ComboBox and then unpause it when I am done. How do I generate this functionality?
Try this to turn off:
application.enableevents = false
And this to turn back on:
application.enableevents = true
To pause and show a message and to keep working on something during the pause. finally press button
Public Ready As Boolean
Private Sub Command1_Click()
Ready = True
End Sub
Private Sub Form_Load()
Me.Show
Ready = False
Call Wait
Label1.Visible = True
End Sub
Public Function Wait()
Do While Ready = False
DoEvents
Loop
End Function
Maybe you can put a flag to bypass the update event on your combobox until a selection is made.
Private bLock as boolean ' declare at module level
' When a user clicks on the combobox
Private Sub DropDownArrow_Click() ' or cboComboBox_Click()
bLocked = True
End Sub
' This procedure is the one that does the updating from the data source.
' If the flag is set, do not touch the comboboxes.
Private Sub subUpdateComboBoxes()
If Not bLocked then
' Update the comboboxes
End If
End Sub
' When the selection is made, or the focus changes from the combobox.
' check if a selection is made and reset the flag.
Private Sub cboComboBox_AfterUpdate() ' Or LostFucus or something else
if Format(cboComboBox.Value) <> vbNullString Then
bLocked = False
End If
End Sub
Hope that helps.