Friday, May 16, 2008

How to change background color of Listview in C#

Hello guyz. I have simple code here to change the background color of Listview and it's cells during runtime.

Step 1:
Create first a Listview.

Step 2:
Put this code at your form_load event.

listView1.Items.Add("Hello World");
listView1.Items[0].SubItems[0].BackColor = Color.Blue;

Wednesday, May 14, 2008

How to Clear all Text in a TextBox in C#

Im using C sharp 3.0

Try This Codes:


private void button1_Click(object sender, EventArgs e)
{
foreach(Control sayre in this.Controls)
{
if (sayre is TextBox)
{
(sayre as TextBox).Clear();
}
}
}

How to Get Date and Time Difference in C#

Hello guyz. I have example here on how to get the difference of date and time in C#.

Step: 1
Create a timer then set the interval to 10 and enabled to true.

Step: 2
Create 3 Label. Label1,Label2,Label3.

Step: 3
Create 2 Datepicker. Name it dtfrom and dtto.

Step: 4
Create a Button1

Then Copy this Code at your form:

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{label1.Text = dtfrom.Value.ToString();}
private void timer1_Tick(object sender, EventArgs e)
{label2.Text = string.Format("{0:G}", DateTime.Now);}
private void button1_Click(object sender, EventArgs e)
{
TimeSpan ts = new TimeSpan();
DateTime dt1,dt2 = new DateTime();
dt1 = dtfrom.Value;
dt2 = Convert.ToDateTime(label2.Text);
ts=dt1.Subtract(dt2);
label3.Text = ts.ToString(); //Answer
}


Thursday, May 8, 2008

Different Date Format in C#

DateTime sai = DateTime.Now; //Get Date and Time


Console.WriteLine("d format: {0:d}", sai);
Console.WriteLine("D format: {0:D}", sai);

Console.WriteLine("t format: {0:t}", sai);

Console.WriteLine("T format: {0:T}", sai);

Console.WriteLine("f format: {0:f}", sai);

Console.WriteLine("F format: {0:F}", sai);

Console.WriteLine("g format: {0:g}", sai);

Console.WriteLine("G format: {0:G}", sai);

Console.WriteLine("m format: {0:m}", sai);

Console.WriteLine("M format: {0:M}", sai);

Console.WriteLine("r format: {0:r}", sai);

Console.WriteLine("R format: {0:R}", sai);

Console.WriteLine("s format: {0:s}", sai);

Console.WriteLine("u format: {0:u}", sai);

Console.WriteLine("U format: {0:U}", sai);

Console.WriteLine("y format: {0:y}", sai);

Console.WriteLine("Y format: {0:Y}", sai);

Console.Read();