Answer the question
In order to leave comments, you need to log in
How to organize Update in window forms?
Unity3d has monobehavior components, they have update. Every frame, all objects are traversed and all their updates are performed. And how do vs windows forms work? Where is update? In the events I saw Load, let's say this is an analogue of Start.
I did it like this (does not work (the form title does not change):
private void Form1_Load(object sender, EventArgs e)
{
System.Random rnd = new Random();
Task task = new Task(()=>
{
while(true)
{
this.Text = rnd.Next(0, 1000).ToString();
}
});
task.Start();
}
Answer the question
In order to leave comments, you need to log in
using System;
using System.Windows.Forms;
namespace WinFormsApp
{
public partial class MainForm : Form
{
private readonly Random _random;
private readonly Timer _timer;
public MainForm()
{
InitializeComponent();
_random = new Random();
_timer = new Timer();
_timer.Interval = 500;
_timer.Tick += OnTimerTick;
}
private void OnFormLoad(object sender, EventArgs e)
{
_timer.Start();
}
private void OnTimerTick(object sender, EventArgs e)
{
label.Text = _random.Next(0, 1000).ToString();
}
}
}
In windows forms, the analogue of unity update is the OnPaint method (override it):
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red, 2);
e.Graphics.DrawLine(pen, 0, 0, 150, 150);
pen.Dispose();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question