• 13
• 18
• 19
• 27
• 10

# Windows Forms Idle Hell

This topic is 3683 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've got a very basic form but I'm having trouble understanding the basics... How do I keep my program in sync with a class of variables that are displayed in the form for instance. i.e.
namespace BlackJackForm{

static class Program
{
static public class Globals
{
public static int timer;
}
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Globals.timer = new Timer();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Idle += App_Idle;
Application.Run(new Form1());
}

static void App_Idle(object sender, EventArgs e)
{
static int last_time;
int new_time = getticks()//example
BlackJackForm.Form1.Update_Idle(last_time-new_time);
}
}
}


Neither of the two lines below work from within the form class and I can access my globals from within the form class itself but I dont know how to trigger the form to redraw itself. I understand I cant update the label as it may not exist yet or thats what the compiler seems to implying.
namespace BlackJackForm
{
partial class Form1
{
public static void Update_Idle(int t)
{
BlackJackForm.Form1.lbl_time.Text = T;
BlackJackForm.Form1.lbl_time.Text = BlackJackForm.Program.Globals.Program.timer;
}
}
}


How come the classes are layed out so different to just normal c++ why does the main function/globals need to be encapsulated within a static class ? How would I trigger the form to refresh so if the time changes for instance I could say form1.refresh() then that would take into account all changes. Closest I can find to a refresh is a paint event but how would I trigger that based on a variable change like a constantly updating clock ? I understand the application has an idle event is it possible to also declare a form idle event which could update the form itself based on a custom class of ints etc ? Thanks for any help, fishy [Edited by - fishleg003 on February 17, 2008 3:33:14 AM]

##### Share on other sites
Generally, you don't need to tell your form to redraw your form since .NET handles it all for you. If you're just changing the text of a label for example, just change the Text property and it'll automagically appear on your form.

Quote:
 How come the classes are layed out so different to just normal c++

Because C# is not the same as C++. They're similar, but not exactly the same.

Quote:
 why does the main function/globals need to be encapsulated within a static class ?

They don't. You can have regular old functions, and you can have regular old globals in a managed application. I don't know much C#, but I'd assume that it'd be the same.

Hope this helps.

##### Share on other sites
My forms keep flashing but I have figured out how to keep the form updated by actually having a reference to the form created.

Below is the basis of the two classes I have,
Main File
namespace BlackJackForm{        static class Program    {        public static class Globals        {            public static string text;             public Form1 Main_Form();        }        [STAThread]        static void Main()        {            Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);            Application.Idle += App_Idle;            Globals.Main_Form = new Form1;            Application.Run(Globals.Main_Form);        }        static void App_Idle(object sender, EventArgs e)         {            Globals.Main_Form.Refresh_From_Idle("something");        }    }}

Form File
namespace BlackJackForm{    partial class Form1    {       private void InitializeComponent()        {            this.Label = new System.Windows.Forms.Label();        }        public void Refresh_From_Idle(string T)        {            this.Label.Text = T;            this.Refresh();        }     public System.Windows.Forms.Label Label    }}

All I wanted was to be able to trigger a change on the forms so when the program idle function is called and it decides the player needs a new card the form would change or the time label would keep ticking.

With the above the form is updating but will only update if the mouse moves or if I drag the window it will only then start counting. The idle function should keep firing constantly I'd of thought when no other actions are being performed but it seems to be the oposite only when actions are being performed on the form.

How would I correctly setup the idle function so it keeps firing when the program is idle ? and how do I stop the form from flashing ?

[Edited by - fishleg003 on February 17, 2008 3:19:15 PM]

Bump

##### Share on other sites
What about doing some update work during paint?

##### Share on other sites
Fishleg003... You need to rethink your approach to your problem - not least to eradicate your global 'form' variable and the slightly backward notion of 'pushing' data into views.

Quote:
 All I wanted was to be able to trigger a change on the forms so when the program idle function is called and it decides the player needs a new card the form would change or the time label would keep ticking.

So trying to use OnIdle here is bit of a contradiction - you're trying to do something specific while the application does nothing specific. If you really want reliable events in an event driven system you need to make the events yourself.

The quickest solution is to drop a timer on your form and do the updating and decision making from a handler inside the form. If it happens that you need to watch some user input as well, then handle the mouse/keyboard input specifically and set some flags for it. If you need to reference time in a common frame across your program, use real time and a bit of maths, rather than trying to accumulate it in random chunks.

Jans.

##### Share on other sites
Wow information overload, I had no idea c# was seperate from c and that was why I was just going round in circles. Thanks alot for your help I'll keep reading up on it I think.

From what I understand you cant make use of the standard libaries from within c# project or use c++ classes ?

Ill make a new post just so its more clear I think as I can see myself why its got so twisted.

[Edited by - fishleg003 on February 18, 2008 6:00:56 AM]