How to show Clock with system time using with Timer control in windows forms C#.net || Clock Example with System Time in window forms dot net.


The following  article is shows about display clock in windows forms using timer control in C#.
For that I am taking one User control and One form to develop this. First add One User Control.
Select  add new item from solution add one User Control with the name ClockTime and take one timer control in ClockTime user control then write the following code in .cs file:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClockTime
{
    public partial class ClockTime : UserControl
    {
        const float PI = 3.141592654F;

        DateTime dateTime;

        float fRadius, fCenterX, fCenterY, fCenterCircleRadius, fHourLength;
        float fMinLength,fSecLength, fHourThickness,fMinThickness,fSecThickness;
         bool bDraw5MinuteTicks = true;
        bool bDraw1MinuteTicks = true;
        float fTicksThickness = 2;

        Color hrColor = Color.Black;
        Color minColor = Color.Black;
        Color secColor = Color.Black;
        Color circleColor = Color.Black;
        Color ticksColor = Color.Black;

    //    private System.Windows.Forms.Timer timer1;
        private System.ComponentModel.IContainer components;
        public ClockTime()
        {
            InitializeComponent();
        }
       

        private void ClockTime_Load(object sender, EventArgs e)
        {
            dateTime = DateTime.Now;
            this.ClockTime_Resize(sender, e);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.dateTime = DateTime.Now;
            this.Refresh();
        }
     
        private void DrawLine(float fThickness, float fLength, Color color, float fRadians, System.Windows.Forms.PaintEventArgs e)
        {
            e.Graphics.DrawLine(new Pen(color, fThickness),
                fCenterX - (float)(fLength / 9 * System.Math.Sin(fRadians)),
                fCenterY + (float)(fLength / 9 * System.Math.Cos(fRadians)),
                fCenterX + (float)(fLength * System.Math.Sin(fRadians)),
                fCenterY - (float)(fLength * System.Math.Cos(fRadians)));
        }

        private void DrawPolygon(float fThickness, float fLength, Color color, float fRadians, System.Windows.Forms.PaintEventArgs e)
        {

            PointF A = new PointF((float)(fCenterX + fThickness * 2 * System.Math.Sin(fRadians + PI / 2)),
                (float)(fCenterY - fThickness * 2 * System.Math.Cos(fRadians + PI / 2)));
            PointF B = new PointF((float)(fCenterX + fThickness * 2 * System.Math.Sin(fRadians - PI / 2)),
                (float)(fCenterY - fThickness * 2 * System.Math.Cos(fRadians - PI / 2)));
            PointF C = new PointF((float)(fCenterX + fLength * System.Math.Sin(fRadians)),
                (float)(fCenterY - fLength * System.Math.Cos(fRadians)));
            PointF D = new PointF((float)(fCenterX - fThickness * 4 * System.Math.Sin(fRadians)),
                (float)(fCenterY + fThickness * 4 * System.Math.Cos(fRadians)));
            PointF[] points = { A, D, B, C };
            e.Graphics.FillPolygon(new SolidBrush(color), points);
        }

        private void ClockTime_Paint(object sender, PaintEventArgs e)
        {
            float fRadHr = (dateTime.Hour % 12 + dateTime.Minute / 60F) * 30 * PI / 180;
            float fRadMin = (dateTime.Minute) * 6 * PI / 180;
            float fRadSec = (dateTime.Second) * 6 * PI / 180;

            DrawPolygon(this.fHourThickness, this.fHourLength, hrColor, fRadHr, e);
            DrawPolygon(this.fMinThickness, this.fMinLength, minColor, fRadMin, e);
            DrawLine(this.fSecThickness, this.fSecLength, secColor, fRadSec, e);


            for (int i = 0; i < 60; i++)
            {
                if (this.bDraw5MinuteTicks == true && i % 5 == 0) // Draw 5 minute ticks
                {
                    e.Graphics.DrawLine(new Pen(ticksColor, fTicksThickness),
                        fCenterX + (float)(this.fRadius / 1.50F * System.Math.Sin(i * 6 * PI / 180)),
                        fCenterY - (float)(this.fRadius / 1.50F * System.Math.Cos(i * 6 * PI / 180)),
                        fCenterX + (float)(this.fRadius / 1.65F * System.Math.Sin(i * 6 * PI / 180)),
                        fCenterY - (float)(this.fRadius / 1.65F * System.Math.Cos(i * 6 * PI / 180)));
                }
                else if (this.bDraw1MinuteTicks == true) // draw 1 minute ticks
                {
                   
                    e.Graphics.DrawLine(new Pen(ticksColor, fTicksThickness),
                        fCenterX + (float)(this.fRadius / 1.50F * System.Math.Sin(i * 6 * PI / 180)),
                        fCenterY - (float)(this.fRadius / 1.50F * System.Math.Cos(i * 6 * PI / 180)),
                        fCenterX + (float)(this.fRadius / 1.55F * System.Math.Sin(i * 6 * PI / 180)),
                        fCenterY - (float)(this.fRadius / 1.55F * System.Math.Cos(i * 6 * PI / 180)));
                }
            }

            //draw circle at center
           e.Graphics.FillEllipse(new SolidBrush(circleColor), fCenterX - fCenterCircleRadius / 2, fCenterY - fCenterCircleRadius / 2, fCenterCircleRadius, fCenterCircleRadius);
           
        }

        private void ClockTime_Resize(object sender, EventArgs e)
        {
            this.Width = this.Height;
            this.fRadius = this.Height / 2;
            this.fCenterX = this.ClientSize.Width / 2;
            this.fCenterY = this.ClientSize.Height / 2;
            this.fHourLength = (float)this.Height / 3 / 1.85F;
            this.fMinLength = (float)this.Height / 3 / 1.20F;
            this.fSecLength = (float)this.Height / 3 / 1.15F;
            this.fHourThickness = (float)this.Height / 100;
            this.fMinThickness = (float)this.Height / 150;
            this.fSecThickness = (float)this.Height / 200;
            this.fCenterCircleRadius = this.Height / 50;
            timer1.Start();
        
        }
        public Color HourHandColor
        {
            get { return this.hrColor; }
            set { this.hrColor = value; }
        }

        public Color MinuteHandColor
        {
            get { return this.minColor; }
            set { this.minColor = value; }
        }

        public Color SecondHandColor
        {
            get { return this.secColor; }
            set
            {
                this.secColor = value;
                this.circleColor = value;
            }
        }

        public Color TicksColor
        {
            get { return this.ticksColor; }
            set { this.ticksColor = value; }
        }

        public bool Draw1MinuteTicks
        {
            get { return this.bDraw1MinuteTicks; }
            set { this.bDraw1MinuteTicks = value; }
        }

        public bool Draw5MinuteTicks
        {
           
            get { return this.bDraw5MinuteTicks; }
            set { this.bDraw5MinuteTicks = value; }
        }


      
    }
}

Then Design one form with one panel control  and Text what ever you want to write in this I am writing welcome to Nareshkamuni.blogspot.in
My form is like below:


Then write the following code in form.cs file:
Add designed user control  to Panel1 in form_Load event:
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ClockTime obj = new ClockTime();
            panel1.Controls.Add(obj);
        }

Then run the application and see output like below:






Show text with marquee using timer control in windows forms application C#.net:


For show text different way in windows forms output using marquee and timer control  follow the below steps:
First design one window form using C#.net.
Take one label , Timer control and one Stop button from toolbox set some properties if required.
Then the form looks like below , in that I am taking label text as welcome and timer control interval property  100 and button name as Stop.

Then write the following code in form_load, Timer_Tick event and stop button_Click event:
In Form load I am giving label text as Naresh and start the timer control.
In Timer Control click event I am showing text like marquee.
In Stop button click I am stopping text to normal.
public partial class Form1 : Form
    {
        private int xPos = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Width == xPos)
            {
                //repeat marquee
                this.label1 .Location = new System.Drawing.Point(0, 40);
                xPos = 0;
            }
            else
            {
                this.label1.Location = new System.Drawing.Point(xPos, 40);
                xPos++;
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
            label1.Text = "naresh";

        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Stop();
        }
    }

Then Show Output like below:






How to change image when MouseHover and MouseLeave in window forms in C#.net ||How to change cursor to hand symbol when mouseHover in window forms in C#.net


To change image and cursor when mousehover and mouseleave in windows forms  dotnet application follow the below steps:
1.First design one test form with one button name as btnIncome.
2. Change button properties and import two images (mousehover image and mouseleave image) from your system.
In this I am taking two income images with the name income_btn_hover and income_btn_normal.

Then write the following code in btnIncome_MouseHover and btnIncome_MouseLeave event.
To change cursor symbol also I am writing code in mousehover and mouseleave events

private void btnIncome_MouseHover(object sender, EventArgs e)
        {
            this.btnIncome.Image = Properties.Resources.income_btn_hover;
            this.Cursor = Cursors.Hand;

        }


private void btnIncome_MouseLeave(object sender, EventArgs e)
        {
            this.btnIncome.Image = Properties.Resources.income_btn_normal;
            this.Cursor = Cursors.Arrow;
        }


Then run your application then you will get the following output form with one Income normal button .


If you mousehover on Income  button then it will change to Income hover button like below: