Sunday, October 25, 2009

Autocomplete textbox in C#

Here is the code to develop auto complete text box.
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;
using System.Data.SqlClient;
private void Form1_Load(object sender, EventArgs e)
{
AutoCompleteStringCollection autolist = new AutoCompleteStringCollection();
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
con.Open();

SqlCommand comm = new SqlCommand("select * from users", con);
SqlDataAdapter da = new SqlDataAdapter(comm);

DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow r in dt.Rows)
{
autolist.Add(r[0].ToString());
}
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteCustomSource = autolist;
}

Wednesday, October 21, 2009

DateTime Format String

DateTime has its own set format string modifiers because there are so many ways to display a date and time.

Standard Format String

This is basically built in short hand for custom format string. You pass in the one character string to denote which custom format you want.

i.e.
now.ToString("d"); // "10/21/2009"
now.ToString("D"); // "Wednesday, October 21, 2009"
now.ToString("G"); // "10/21/2009 10:16:01 PM"


Custom Format String
Custom format string gives you the flexibility to build your own formatting. When using a single character format string specifier, you will need to prepend it with a "%", otherwise it will be interpreted as a Standard Format String. Here are the basics for building your own string:

Example:
StringBuilder strBuilder=new StringBuilder ();
DateTime now = new DateTime(2009,10, 21, 22, 16, 01, 08, DateTimeKind.Local);
strBuilder.AppendLine ("now.ToString() ==> " + now .ToString() );
strBuilder.AppendLine("now.ToString(\"d\")" + now.ToString("d"));
strBuilder.AppendLine("now.ToString(\"D\")" + now.ToString("D"));
strBuilder.AppendLine("now.ToString(\"G\")" + now.ToString("G"));

// Year
strBuilder.AppendLine("YEAR ");
strBuilder.AppendLine ("now.ToString(\"%y\") ==> " + now.ToString("%y") );
strBuilder.AppendLine ("now.ToString(\"yy\") ==> " + now.ToString("yy") );
strBuilder.AppendLine ("now.ToString(\"yyy\") ==>" + now.ToString("yyy") );
strBuilder.AppendLine ("now.ToString(\"yyyy\") ==>" + now.ToString("yyyy") );

//Month
strBuilder.AppendLine("MONTH ");
strBuilder.AppendLine ("now.ToString(\"%M\") ==> " + now.ToString("%M") );
strBuilder.AppendLine ("now.ToString(\"MM\") ==> " + now.ToString("MM") );
strBuilder.AppendLine ("now.ToString(\"MMM\") ==>" + now.ToString("MMM") );
strBuilder.AppendLine("now.ToString(\"MMMM\") ==>" + now.ToString("MMMM"));


//Day
strBuilder.AppendLine("Day ");
strBuilder.AppendLine("now.ToString(\"%d\") ==> " + now.ToString("%d"));
strBuilder.AppendLine("now.ToString(\"dd\") ==> " + now.ToString("dd"));
strBuilder.AppendLine("now.ToString(\"ddd\") ==>" + now.ToString("ddd"));
strBuilder.AppendLine("now.ToString(\"dddd\") ==>" + now.ToString("dddd"));

//Hour
strBuilder.AppendLine("HOUR ");
strBuilder.AppendLine("now.ToString(\"%h\") ==> " + now.ToString("%h"));
strBuilder.AppendLine("now.ToString(\"hh\") ==> " + now.ToString("hh"));
strBuilder.AppendLine("now.ToString(\"hhh\") ==>" + now.ToString("hhh"));
strBuilder.AppendLine("now.ToString(\"hhhh\") ==>" + now.ToString("hhhh"));
strBuilder.AppendLine("now.ToString(\"%H\") ==> " + now.ToString("%H"));
strBuilder.AppendLine("now.ToString(\"HH\") ==> " + now.ToString("HH"));
strBuilder.AppendLine("now.ToString(\"HHH\") ==>" + now.ToString("HHH"));
strBuilder.AppendLine("now.ToString(\"HHHH\") ==>" + now.ToString("HHHH"));

//Minutes
strBuilder.AppendLine("MINUTES ");
strBuilder.AppendLine("now.ToString(\"%m\") ==> " + now.ToString("%m"));
strBuilder.AppendLine("now.ToString(\"mm\") ==> " + now.ToString("mm"));
strBuilder.AppendLine("now.ToString(\"mmm\") ==>" + now.ToString("mmm"));
strBuilder.AppendLine("now.ToString(\"mmmm\") ==>" + now.ToString("mmmm"));

//Seconds
strBuilder.AppendLine("SECONDS ");
strBuilder.AppendLine("now.ToString(\"%s\") ==> " + now.ToString("%s"));
strBuilder.AppendLine("now.ToString(\"ss\") ==> " + now.ToString("ss"));
strBuilder.AppendLine("now.ToString(\"sss\") ==>" + now.ToString("sss"));
strBuilder.AppendLine("now.ToString(\"ssss\") ==>" + now.ToString("ssss"));

//Milliseconds
strBuilder.AppendLine("MILLISECONDS ");
strBuilder.AppendLine("now.ToString(\"%f\") ==> " + now.ToString("%f"));
strBuilder.AppendLine("now.ToString(\"ff\") ==> " + now.ToString("ff"));
strBuilder.AppendLine("now.ToString(\"fff\") ==>" + now.ToString("fff"));
strBuilder.AppendLine("now.ToString(\"ffff\") ==>" + now.ToString("ffff"));
strBuilder.AppendLine("now.ToString(\"%F\") ==> " + now.ToString("%F"));
strBuilder.AppendLine("now.ToString(\"FF\") ==> " + now.ToString("FF"));
strBuilder.AppendLine("now.ToString(\"FFF\") ==>" + now.ToString("FFF"));
strBuilder.AppendLine("now.ToString(\"FFFF\") ==>" + now.ToString("FFFF"));

//Kind
strBuilder.AppendLine("KIND ");
strBuilder.AppendLine("now.ToString(\"%K\") ==> " + now.ToString("%K"));
strBuilder.AppendLine("now.ToString(\"KK\") ==> " + now.ToString("KK"));
strBuilder.AppendLine("now.ToString(\"KKK\") ==>" + now.ToString("KKK"));
strBuilder.AppendLine("now.ToString(\"KKKK\") ==>" + now.ToString("KKKK"));

//TimeZone
strBuilder.AppendLine("TIMEZONE ");
strBuilder.AppendLine("now.ToString(\"%z\") ==> " + now.ToString("%z"));
strBuilder.AppendLine("now.ToString(\"zz\") ==> " + now.ToString("zz"));
strBuilder.AppendLine("now.ToString(\"zzz\") ==>" + now.ToString("zzz"));
strBuilder.AppendLine("now.ToString(\"zzzz\") ==>" + now.ToString("zzzz"));

//Other
strBuilder.AppendLine("OTHER ");
strBuilder.AppendLine("now.ToString(\"%g\") ==> " + now.ToString("%g"));
strBuilder.AppendLine("now.ToString(\"gg\") ==> " + now.ToString("gg"));
strBuilder.AppendLine("now.ToString(\"ggg\") ==>" + now.ToString("ggg"));
strBuilder.AppendLine("now.ToString(\"gggg\") ==>" + now.ToString("gggg"));

strBuilder.AppendLine("now.ToString(\"%t\") ==> " + now.ToString("%t"));
strBuilder.AppendLine("now.ToString(\"tt\") ==> " + now.ToString("tt"));
strBuilder.AppendLine("now.ToString(\"ttt\") ==>" + now.ToString("ttt"));
strBuilder.AppendLine("now.ToString(\"tttt\") ==>" + now.ToString("tttt"));

textBox1.Text = strBuilder.ToString();


Output:

now.ToString() ==> 10/21/2009 10:16:01 PM
now.ToString("d")10/21/2009
now.ToString("D")Wednesday, October 21, 2009
now.ToString("G")10/21/2009 10:16:01 PM
YEAR
now.ToString("%y") ==> 9
now.ToString("yy") ==> 09
now.ToString("yyy") ==>2009
now.ToString("yyyy") ==>2009
MONTH
now.ToString("%M") ==> 10
now.ToString("MM") ==> 10
now.ToString("MMM") ==>Oct
now.ToString("MMMM") ==>October
Day
now.ToString("%d") ==> 21
now.ToString("dd") ==> 21
now.ToString("ddd") ==>Wed
now.ToString("dddd") ==>Wednesday
HOUR
now.ToString("%h") ==> 10
now.ToString("hh") ==> 10
now.ToString("hhh") ==>10
now.ToString("hhhh") ==>10
now.ToString("%H") ==> 22
now.ToString("HH") ==> 22
now.ToString("HHH") ==>22
now.ToString("HHHH") ==>22
MINUTES
now.ToString("%m") ==> 16
now.ToString("mm") ==> 16
now.ToString("mmm") ==>16
now.ToString("mmmm") ==>16
SECONDS
now.ToString("%s") ==> 1
now.ToString("ss") ==> 01
now.ToString("sss") ==>01
now.ToString("ssss") ==>01
MILLISECONDS
now.ToString("%f") ==> 0
now.ToString("ff") ==> 00
now.ToString("fff") ==>008
now.ToString("ffff") ==>0080
now.ToString("%F") ==>
now.ToString("FF") ==>
now.ToString("FFF") ==>008
now.ToString("FFFF") ==>008
KIND
now.ToString("%K") ==> +05:00
now.ToString("KK") ==> +05:00+05:00
now.ToString("KKK") ==>+05:00+05:00+05:00
now.ToString("KKKK") ==>+05:00+05:00+05:00+05:00
TIMEZONE
now.ToString("%z") ==> +5
now.ToString("zz") ==> +05
now.ToString("zzz") ==>+05:00
now.ToString("zzzz") ==>+05:00
OTHER
now.ToString("%g") ==> A.D.
now.ToString("gg") ==> A.D.
now.ToString("ggg") ==>A.D.
now.ToString("gggg") ==>A.D.
now.ToString("%t") ==> P
now.ToString("tt") ==> PM
now.ToString("ttt") ==>PM
now.ToString("tttt") ==>PM

How to get string in Title case

Here is the code example for converting the string to title case.

//initialize the datetime object with today's date
DateTime todaysDate = DateTime.Now;

//get the current culture
CultureInfo properCase = System.Threading.Thread.CurrentThread.CurrentCulture;

//get text info object from the current culture
TextInfo textInfoObject = properCase.TextInfo;

//the textinfo ToTitleCase method will return the text in title case
MessageBox.Show(textInfoObject.ToTitleCase(todaysDate.ToString("MMMM")));



Output:

October

Here, the todaysDate.ToString("MMMM") prints the complete month like October, December etc.