Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

infinite loop implemenaion

$
0
0

Hi, my objective is to create a window form application that implements an in infinite-loop. The attached listing is a form with 2 buttons to simulates a counter that increments when the up button is clicked and decrements with the clicking of the down button. I placed the infinite loops in the button-clicking procedures but there was no display at the window. Further, when I suppressed the action of the infinite loops in the procedure, the window displays the fresh count only after I minimize the window then maximize again. It is appreciated if anyone could give me a lead about how to implement the infinite loop properly. Thanks.

using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class WinForm : Form
{
    private Button b1;
    private Button b2;
    uint counter = 0;// printing counter
    String CntStr;
    String msgStr;
    bool Up, Down;
    public WinForm()
    {
        InitializeComponent();
    }
    public bool body()
    {
        if (     Up   && (counter < 10000)) counter++;
        else if (Down && (counter >     0)) counter--;
        CntStr = counter.ToString();
        msgStr = "counter reading = " + CntStr;
        this.Paint += f1_paint;
        return true;
    }
    public void b1_click(object sender, EventArgs e)
    {
        Up = true;
        Down = false;
        //while (true){
            body();
        //}
    }
    public void b2_click(object sender, EventArgs e)
    {
        Down = true;
        Up = false;
        //while (true){
            body();
        //}
    }
    private void InitializeComponent()
    {
        this.b1 = new Button();
        this.b1.Click += new EventHandler(b1_click);
        b1.Location = new Point(0, 40);
        this.b1.Text = "UP";
        this.Controls.Add(b1);
        this.b2 = new Button();
        this.b2.Click += new EventHandler(b2_click);
        b2.Location = new Point(0, 80);
        this.b2.Text = "DOWN";
        this.Controls.Add(b2);
        // 
        // WinForm
        // 
        this.Paint += new PaintEventHandler(f1_paint);
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Name = "WinForm";
    }
    private void f1_paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.DrawString(msgStr, new Font("Helvetica", 10),
        new SolidBrush(Color.Blue), 40, 100);
    }
}
public class Program
{
    public static int Main()
    {
        Application.Run(new WinForm());
        return 0;
    }
}


Esmat


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>