Page 1 of 1
Timers
Posted: Tue Aug 07, 2007 8:39 am
by Patrickh
Ok, so I have an internal timer that displays a messagebox in a certain amount of time. How might I display the remaining time in a labelbox (i want it to count down)? Thanks.
Oh, and here's a seperate problem:
Okay, so I loaded all the possible fonts on windows into a combobox, using the following code
Code: Select all
Dim t As New System.Drawing.Text.InstalledFontCollection
For Each f As FontFamily In t.Families
ComboBox2.Items.Add(f.Name)
Next
now how do I make it so that when you select one, it changes the font property of TextBox1.Text to the selected font?
thanks for any help
Posted: Tue Aug 07, 2007 5:27 pm
by LuxuriousMeat
Heres a example:
And you need to do this:
-Add a timer.
-Make your timers interval to 1000 (it's in milliseconds (1000 milliseconds = 1 second))
Code: Select all
Public Class Form1
Dim count As Integer = 30
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Not count = 0 Then
Label1.Text = count
count -= 1
Else
Timer1.Stop()
MsgBox("Times up!")
End If
End Sub
End Class
Posted: Tue Aug 07, 2007 8:50 pm
by Patrickh
That works, thanks, but I need it to work in minutes, for example:
5:00
4:59
4:58
etc.
Is there a trick for that?
Posted: Wed Aug 08, 2007 6:11 am
by [cc]z@nd!
maybe use two integers? one from 59 to 0, then when it gets negative, it's re-initialized to 59. then the other, for minutes, maybe on a different timer, from whatever number under 60 to 0, like the minutes.
Posted: Wed Aug 08, 2007 9:04 am
by Patrickh
[cc]z@nd! wrote:maybe use two integers? one from 59 to 0, then when it gets negative, it's re-initialized to 59. then the other, for minutes, maybe on a different timer, from whatever number under 60 to 0, like the minutes.
That sounds way too complicated, I made it so you type 5 in the textbox and it will count down for 5 five minutes. The only problem is that it counts down from 300. There's probably a simple conversion trick, but I'm not familiar with it. [cc]z@nd!, I'm sure that would work, but I'm going to search for a simpler method before I resort to that.
Posted: Fri Aug 10, 2007 9:31 pm
by OwnZ joO
I'll post some timer code from a C# program that I made, you should be able to convert it to VB.net, but if you need to I'll help.
Code: Select all
private void timer1_Tick(object sender, EventArgs e)
{
_seconds--;
Time.Text = GetTime(_seconds); 'Time.Text is the labels text
' GetTime returns a string
if (seconds < 1) // 0 or less
{
'do what you want, I assume stop the timer and reset
}
}
private string GetTime(int seconds) // a function that returns a string
{
int s = seconds % 60; 'use mod instead of % in vb(get's the remainder of the division)
int m = seconds / 60;
string sec = "";
string min = "";
if( s < 10 )
{
sec = "0";
}
if( m < 10 )
{
min = "0";
}
sec += s.ToString();
min += m.ToString();
return min & ":" & sec
}
Posted: Sat Aug 11, 2007 4:19 am
by Prey
Or just
Code: Select all
// Hour: 3600, Minute: 60, Second: 1
int time = 7200;
private void timer1_Tick(object sender, EventArgs e)
{
if (--time <= 0)
{
time = 0;
timer1.Enabled = false;
}
label1.Text = string.Format("{0}:{1}:{2}",
(time / 3600).ToString("00"),
((time % 3600) / 60).ToString("00"),
((time % 3600) % 60).ToString("00"));
}
Posted: Sat Aug 11, 2007 8:25 am
by OwnZ joO
I used string.format in my code, but i didn't know if vb.net had that, so i changed some of my code around, notice the &'s in there.