0

I know that there is no update function in VB, But I was wondering if there was a particular piece of code that could run the code inside it every frame/tick. Like a timer, but I don't know how to use a timer.

Here is some sample code that I would want to check every second.

if aBoolean then
    textBox1.Text = "Aboolean is true"
else
    textBox1.Text = "Aboolean is false"
end if

And for instance a button would change aBoolean to be true. I know this could be done around a button press, but its just an example. If there is nothing to do this, then I will just re-write my code in C# and use the void update() for it. Thanks.

Blair Cross
  • 13
  • 1
  • 2

3 Answers3

1

C# code

   Timer timer = new Timer();

    public FormWithTimer()
    {
        InitializeComponent();

        timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
        timer.Interval = (1000) * (10);             // Timer will tick evert 10 seconds
        timer.Enabled = true;                       // Enable the timer
        timer.Start();                              // Start the timer
    }

    void timer_Tick(object sender, EventArgs e)
    {
        if (aBoolean)
        {textBox1.Text = "Aboolean is true";}
        else { textBox1.Text = "Aboolean is false"; }
    }

Example from : https://social.msdn.microsoft.com/Forums/windows/en-US/43daf8b2-67ad-4938-98f7-cae3eaa5e63f/how-to-use-timer-control-in-c

Dieter B
  • 1,142
  • 11
  • 20
0

By the looks of things, you are writing your code in VB, so I'll do that here. Also I'm not intending to be condescending, I just want to write the answer so that anyone else looking at this can use it and learn a bit about timers.

First off, in the toolbox, you need to double click on the Timer control. This will add a timer underneath your form in the Design window. The timer doesn't appear on your form as it doesn't have any sort of graphical interface.

Underneath the window you should have a timer control called Timer1. Double click this and your code window will open and you'll find a new sub that looks something like this -

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

End Sub

inside the Sub add the code that you want to happen every second. So now you should have this -

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    if aBoolean then
        textBox1.Text = "Aboolean is true"
    else
        textBox1.Text = "Aboolean is false"
    end if
End Sub

OK? So far so good. This bit of code doesn't do anything yet. When Timer1 is enabled, every time the timer ticks, it raises an event called Timer1.Tick. The code above runs when this Tick event happens.

The next bit depends when you want the timer to start ticking and how long you want the interval between ticks to be. The interval is stored as milliseconds, so to set the interval for Timer1 to 1 second, all you need to do is this

Timer1.Interval=1000

This needs to go somewhere in your code that you know will run before you want the timer to start ticking. I sometimes put it in the Form's Load event, but it doesn't have to be there.

At the point in your program where you want the timer to start ticking, it's as easy as adding this to a Sub in your code

Timer1.Start

Inevitably there will be a point where you want the timer to stop firing out these ticks, so at that point, just add

Timer1.Stop

That pretty much covers the basics of timers.

Hope it helps

David Wilson
  • 4,369
  • 3
  • 18
  • 31
-2

In Winforms u have the

Control.OnPaint

Event, which is mostly like an Update call. This is Language independent (C#, VB, C++)

In WPF u have the

UIElement.OnRender

Event, which is also mostly like an Update call. Depending on ur window framework, u can use one of each as an update exchange.

Yosh Synergi
  • 314
  • 3
  • 12
  • This approach doesn't update the UI when the boolean variable `aBoolean` changes. The UI will call `OnPaint`/`OnRender` when `textBox1.Text` changes, so that's what the OP needs to know how to update using a timer. – Enigmativity Oct 28 '15 at 11:04
  • This doesnt answer what the OP is asking. – David Wilson Oct 29 '15 at 00:54