4

This is the image of the design window:

enter image description here

Here is the MainForm.Designer.cs file:

namespace SamsCSharp24
{
    partial class ImeObrasca
    {
        // irrelavent code is omitted, only event subscriptions are left
        private void InitializeComponent()
        {
            // irrelavent code is omitted for brewity
            // 
            // SelectPicture
            // 
            this.SelectPicture.Paint += new System.Windows.Forms.PaintEventHandler(this.SelectPicture_Paint);
            this.SelectPicture.Click += new System.EventHandler(this.SelectPicture_Click);
            // 
            // Quit
            // 
            this.Quit.Click += new System.EventHandler(this.Quit_Click);
            // 
            // PictureBox
            //
            this.PictureBox.MouseLeave += new System.EventHandler(this.PictureBox_MouseLeave);
            this.PictureBox.MouseEnter += new System.EventHandler(this.PictureBox_MouseEnter);
            // 
            // btnOptions
            //
            this.btnOptions.Click += new System.EventHandler(this.btnOptions_Click);
            // 
            // timerClock
            // 
            this.timerClock.Tick += new System.EventHandler(this.timerClock_Tick);
        }

        #endregion
    }
}

Here is the MainForm.cs file:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SamsCSharp24
{
    public partial class ImeObrasca : Form
    {
        public ImeObrasca()
        {
            InitializeComponent();
            // when uncommenting below line, window is not seen in taskbar
            // this.ShowInTaskbar = false; 
        }

        // other code is omitted for brewity 
        private void SelectPicture_Paint(object sender, PaintEventArgs e)
        {
            // just for fun, change color of a button to light blue
            SelectPicture.BackColor = Color.Azure;
        }

        private void timerClock_Tick(object sender, EventArgs e)
        {
            // when timer ticks, change label's text into current time of day
            staticClock.Text = "Current time of day: " +
                DateTime.Now.Hour.ToString() + " : "  +
                DateTime.Now.Minute.ToString() + " : " +
                DateTime.Now.Second.ToString();
        }
    }
}

Timer control has following properties set via designer:

Enabled = true;
Interval = 1000
Name = timerClick
Tick (event) = timerClock_Tick

As for label, here are the properties also set with designer:

BorderStyle = FixedSingle
Name = staticClock
Autosize = false
Text = 
Other properties are default or irrelevant ( like Location or Size )

PROBLEM:

When I run the application ( in Debug mode ), window appears with properly placed controls and with proper look. Every other part of the code works successfully ( picture opening / drawing etc ) but the label remains empty, as initially set in the designer.

After I minimize / maximize the window, the label text is set correct. I have tried to move the part of the window with label "out" of the screen and get it back to see what happens. The text in the label was changed sometimes -> it didn't update correct.

MY EFFORTS TO SOLVE THE PROBLEM:

This is my first time trying out C# and WinForms so I have tried to find some online documentation on timers.

After examining .Designer.cs file I have found out that the timer from toolbox belongs to System.Windows.Forms.Timer class. I found nothing there to help me, since in Remarks section is stated that setting property Enabled to true starts the timer, and setting it to false stops it.

I have tried to put simple message box, and it started popping properly when the window was minimized. When window is in normal state nothing showed, but other parts of the program worked well ( picture opening / drawing / etc ).

After trying to Google for solution / searching here on SO, I have found no concrete solution ( although some suggestions were made, but as I said, they weren't helpful to me ).

QUESTION:

How to modify timer's tick handler, so label's text can be modified every second?

What am I doing wrong?

nevets
  • 4,631
  • 24
  • 40
AlwaysLearningNewStuff
  • 2,939
  • 3
  • 31
  • 84
  • While your question seems well researched and well written, there really was nothing important about that first paragraph so I removed it. We don't require an introduction to you; it's the problem we care about. – Jeroen Vannevel Aug 28 '14 at 19:27
  • @JeroenVannevel: I have seen the edit, there is no problem. This way the question is shorter. Since this is my very first time trying C# I didn't know which info was relevant and which wasn't so I have decided to give as much info as possible. I wonder if I could edit my post with cutting the irrelevant parts of the code... – AlwaysLearningNewStuff Aug 28 '14 at 19:29
  • There does seem to be a lot of code that's not likely relevant. A lot of your code is setting up the controls, maybe you could trim it so you have at max just a few lines of possibly relevant code for each control? It doesn't really matter what the color is of some other label. – Jeroen Vannevel Aug 28 '14 at 19:32
  • I don't see a `timerClock.Start()` anywhere in your code. Try adding it after the `InitializeComponent()` in your form's constructor. You may also want to put a `this.Refresh()` at the end of your `timerClock_Tick` handler. Edit: I see what you mean about setting `Enabled` to true. I would advise against doing that in the designer especially before you set your tick interval. – helrich Aug 28 '14 at 19:39
  • @JeroenVannevel: I have left only timer's tick handler, in my posted code. I am thinking of removing the posted `Designer.cs` altogether as I don't think it will be helpful, but I need an opinion of someone more experienced. What do you think? – AlwaysLearningNewStuff Aug 28 '14 at 19:40
  • @helrich: I have made a fresh project, added timer and label and everything works fine. The problem must be something with the other controls that I have added, so I will add one by one control in this new project in hope to try and find the origin of the problem. – AlwaysLearningNewStuff Aug 28 '14 at 19:43
  • 6
    Wrong kind of fun. Paint event handlers should only paint, they should **never** change properties that cause the Paint event to be fired again. Such shenanigans cause the UI thread to burn 100% core, never getting to dispatch the low-priority synthesized messages. Like WM_TIMER. Minimizing the window stops that, temporarily. – Hans Passant Aug 28 '14 at 19:45
  • 2
    @HansPassant: Please post this as an answer, so other beginners can also learn from my mistake. After removing the paint handler everything worked well, I guess I should go step by step instead of trying to jump impatiently forward. Thank you, best regards. – AlwaysLearningNewStuff Aug 28 '14 at 19:53
  • Enabled = true; should be the last line – Zakos Sep 01 '14 at 11:04

1 Answers1

3

As Hans Passant stated in one of the comments, "Paint event handlers should only paint, they should never change properties that cause the Paint event to be fired again. Such shenanigans cause the UI thread to burn 100% core, never getting to dispatch the low-priority synthesized messages. Like WM_TIMER. Minimizing the window stops that, temporarily."

Scott
  • 4,458
  • 1
  • 19
  • 27