Not updating

porkshopp

Active member
Joined
Apr 13, 2019
Messages
26
Location
Sweden
Programming Experience
Beginner
I have this Windows forms application that should run a very basic probability simulation based on deathchance and replicationchance. But my TextBox3 is not updating in realtime but updates once, when the entire species dies out. How do I get it to update in realtime?
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
        }
        private void Label1_Click(object sender, EventArgs e)
        {
        }
        private void Label3_Click(object sender, EventArgs e)
        {
        }
        private void Button1_Click(object sender, EventArgs e)
{
int Count;
bool success3 = int.TryParse(textBox4.Text, out Count);
if (success3)
{
Count = Convert.ToInt16(textBox4.Text);

}
            else textBox3.Text = "Invalid Syntax!";
            double rChance;
double dChance;
bool success = double.TryParse(textBox1.Text, out dChance);
bool success2 = double.TryParse(textBox2.Text, out rChance);
if (success && success2 && rChance <= 100 && rChance >= 0 && dChance >= 0 && dChance <= 100 && success3)
{
bool simulation = false;

while (!simulation)
{
textBox3.Text = Count.ToString();
Random rng = new Random();
int num = rng.Next(0, 101);
int num2 = rng.Next(0, 101);
if (num < rChance && Count != 0) Count++;

if(num2 < dChance && Count != 0) Count--;

if (Count == 0) simulation = true;
Thread.Sleep(1);
                }
            }
            else textBox3.Text = "Invalid Syntax!";

        }
        private void TextBox4_TextChanged(object sender, EventArgs e)
        {
        }
}
}
 
When you debugged your code, i.e. set a breakpoint and stepped through it line by line, where EXACTLY did its behaviour deviate from your expectations and what data was in use at the time?
 
Unfortunately, your code is all in the constructor click handler. You are writing code as if you were running in a console. Remember that when writing for Windows GUI, Windows runs on a event driven message driven model. What that means is that from time to time, Windows will send a message to your program that the paint event has to be handled. You can also send a message to Windows indicating that your data has changed, and that it should send you a paint event.

For what you have above, you can try to take advantage of the Windows timer events as well. Every time you get a timer tick event, advance your simulation forward and update the control, and then mark your control as invalid. (Marking your control as invalid tells Windows that it should repaint the control.) Then the important part, yield control back to Windows so that it has the opportunity to send the paint message to the control and the control can paint.

Out of curiosity, what book or tutorial are you using to learn how to write code in C#, and in this case in WinForms? These concepts should have been taught to you before you got to this point.
 
Last edited:
I have no clue
Then you need to find out. Did you actually debug at all? That is something that you MUST do any time you have an issue like this. If you don't know what the code is actually doing then what chance do you have of working out what it's doing wrong? If you haven;t tried to work out what it's actually doing then you haven't tried to work out what it's doing wrong.

Set a breakpoint at the top of the code and then step through it line by line. Before each step, determine exactly what it is that you expect to happen, i.e. what values your variables should have and what path execution should follow. After you step, check whether what actually happened matched your expectations. If not, you have found an issue and you can investigate that issue specifically, based on the actual data being used at the time. Even if you still can't work out the solution, at least you can provide us with much more relevant information. The more time you spend debugging, the better you get at it and the more often you WILL be able to work out the solution for yourself.
 
I don't have any recommendations for any free online books or tutorials.

In general, I don't recommend learning WinForms since it is a deadend technology because it is at end-of-line. The path forward is to use WPF or Xamarin. If you do insist on going down the WinForms path, I recommend this book: Programming Windows Forms by Charles Petzold.
 
As far as books go; I assume you have a library in your city. They are generally free to go in and read. Or you could do as I've done and read MSDN documentation until you are blue in the face, and know it all backwards. ;)

If you do fork out for books, you can pick up some good ones on Amazon for around $15 - With an abundance of information online, there is no excuse for lack of education on the C# subject in my opinion.

C# 5.0 in a Nutshell: The Definitive Reference Fifth Edition

Head First C# - Although C# in Depth is a better read.

As for debugging, Jmc is correct. If you don't know what your code is doing, how do you expect to know what it's not doing? There are many tutorials online which teach you how to debug.
 
Back
Top Bottom