[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

clearing a label/textbox after a specific time

Hi, I would like to ask for a simple way to do the following task:

By clicking a command button, a message will be shown in a label, and after a specific time, say 5 sec, the label has to be cleared.

Can I do that without using timer control?  I'd like to see if there's any statement in VB like:

label1.caption = 'my message'
wait 5 sec
label1.caption = ''

Thanks a lot!!
0
chuyan
Asked:
chuyan
1 Solution
 
Ryan ChongCommented:
Hi, you can use the GetTickCount API to do this:

put a StartTime on Form_Load()

Then in your Form_Activate(), Do a loop to compare the CurrentTime with the StartTime, if (CurrentTime - StartTime)/ 1000 > 5 then Clear the label1.caption.

'Hope will help.
'Code provided if necessary.

0
 
chuyanAuthor Commented:
Thanks for help.  I would like to see the code too.

But I want to ask, will it use up much CPU time as it continuously doing looping?
0
 
sharmonCommented:
You can do this as well...., I disabled the command button, you can handle it however you want though.

Private Sub Command1_Click()
  Command1.Enabled = False
 
  Label1.Caption = "My Message"
 
  Dim dteEnd As Date
 
  dteEnd = DateAdd("s", 5, Now())
 
  Do While Now() <= dteEnd
    DoEvents
  Loop
 
  Label1.Caption = ""
 
  Command1.Enabled = True
End Sub
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
sharmonCommented:
If you want to use the GetTickCount you could do this...


Option Explicit

Private Declare Function GetTickCount Lib "kernel32" () As Long


Private Sub Command1_Click()
  Command1.Enabled = False
 
  Label1.Caption = "My Message"
 
  Dim lngTickCount As Long
  lngTickCount = GetTickCount() + 5000
 
  Do While GetTickCount() <= lngTickCount
    DoEvents
  Loop
 
  Label1.Caption = ""
 
  Command1.Enabled = True
End Sub
0
 
chuyanAuthor Commented:
Thanks sharmon, your solution sounds simpler.

0
 
vim_asteyaCommented:
why dont u try the timer control
0
 
chuyanAuthor Commented:
vim_asteya, it's because it don't know how to fire the timer event after a specific time.
0
 
chuyanAuthor Commented:
oh... I just found I always had the wrong concept!  I just thought the timer event will fire whenever I enable the timer control!!  Thanks vim_asteya.  Using a timer control should be more simple.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now