using System;
using System.Globalization;
class MainClass {
public static void Main() {
// Create some date/time objects
DateTime dt = new DateTime();
DateTime dt1 = new DateTime(2001, 12, 31);
DateTime dt2 = new DateTime(2000, 12, 31, 23, 59, 59);
// Do some date time math
DateTime today = DateTime.Today;
today = today + new TimeSpan(TimeSpan.TicksPerDay);
Console.WriteLine("Tomorrow is: {0}", today.ToString());
today = DateTime.Today - new TimeSpan(7 * TimeSpan.TicksPerDay);
Console.WriteLine("Last Week on this day it was: {0}",today.ToString());
}
}
Software Development
software development notes, main category for Cs, Java, and other programming experiences.
new TimeSpan(2, 12, 0, 0)
using System;
class MainClass
{
public static void Main()
{
TimeSpan timespan1 = new TimeSpan(2, 12, 0, 0);
TimeSpan timespan2 = new TimeSpan(4, 12, 0, 0);
TimeSpan oneWeek = timespan1 + timespan2;
DateTime now = DateTime.Now;
DateTime past = now - oneWeek;
DateTime future = now + oneWeek;
Console.WriteLine("Now : {0}", now);
Console.WriteLine("Past : {0}", past);
Console.WriteLine("Future: {0}", future);
}
}
Timer.Elapsed
using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
class Program {
static int counter = 0;
static string displayString = "This string will appear one letter at a time. ";
static void Main(string[] args) {
Timer myTimer = new Timer(100);
myTimer.Elapsed += new ElapsedEventHandler(WriteChar);
myTimer.Start();
Console.ReadKey();
}
static void WriteChar(object source, ElapsedEventArgs e) {
Console.Write(displayString[counter++ % displayString.Length]);
}
}
Interval, Tick, Stop
using System;
using System.Drawing;
using System.Windows.Forms;
class CloseInFive: Form
{
public static void Main()
{
Application.Run(new CloseInFive());
}
public CloseInFive()
{
Text = "Closing in Five Minutes";
Timer timer = new Timer();
timer.Interval = 5 * 60 * 1000;
timer.Tick += new EventHandler(TimerOnTick);
timer.Enabled = true;
}
void TimerOnTick(object obj, EventArgs ea)
{
Timer timer = (Timer) obj;
timer.Stop();
timer.Tick -= new EventHandler(TimerOnTick);
Close();
}
}
Simple Clock
using System;
using System.Drawing;
using System.Windows.Forms;
class SimpleClock: Form
{
public static void Main()
{
Application.Run(new SimpleClock());
}
public SimpleClock()
{
Timer timer = new Timer();
timer.Tick += new EventHandler(TimerOnTick);
timer.Interval = 1000;
timer.Start();
}
private void TimerOnTick(object sender, EventArgs ea)
{
Invalidate();
}
protected override void OnPaint(PaintEventArgs pea)
{
StringFormat strfmt = new StringFormat();
strfmt.Alignment = StringAlignment.Center;
strfmt.LineAlignment = StringAlignment.Center;
pea.Graphics.DrawString(DateTime.Now.ToString("F"),
Font, new SolidBrush(ForeColor),
ClientRectangle, strfmt);
}
}
Digital Clock with Date
using System;
using System.Drawing;
using System.Windows.Forms;
class DigitalClock: Form
{
public static void Main()
{
Application.Run(new DigitalClock());
}
public DigitalClock()
{
ResizeRedraw = true;
Timer timer = new Timer();
timer.Tick += new EventHandler(TimerOnTick);
timer.Interval = 1000;
timer.Start();
}
private void TimerOnTick(object obj, EventArgs ea)
{
Invalidate();
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
DateTime dt = DateTime.Now;
string strTime = dt.ToString("d") + "
" + dt.ToString("T");
SizeF sizef = grfx.MeasureString(strTime, Font);
float fScale = Math.Min(ClientSize.Width / sizef.Width,
ClientSize.Height / sizef.Height);
Font font = new Font(Font.FontFamily,
fScale * Font.SizeInPoints);
StringFormat strfmt = new StringFormat();
strfmt.Alignment = strfmt.LineAlignment = StringAlignment.Center;
grfx.DrawString(strTime, font, new SolidBrush(ForeColor),
ClientRectangle, strfmt);
}
}
Control Modifier
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ControlModifier
{
/// <summary>
/// Summary description for ControlModifier.
/// </summary>
public class ControlModifier : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public ControlModifier()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
System.Timers.Timer t = new System.Timers.Timer(10000);
t.Elapsed += new System.Timers.ElapsedEventHandler(time);
t.Start();
}
void time(object sender, System.Timers.ElapsedEventArgs e)
{
MessageBox.Show(ControlModifier.ModifierKeys.ToString());
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "ControlModifier";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new ControlModifier());
}
}
}