Search This Blog

Saturday, July 25, 2009

free ebooks

www.free-ebooks-download.org
Website to download latest free e-books on C#, vb.net, asp.net, 2.0, ado.net, sql 2005, web services, ajax, .net framework, compact framework, xml, sharepoint, exchange server, seo, crystal reports and more.. All free ebooks download, Hurry!

Thursday, July 9, 2009

Populate a TreeView with all drives in system

private void LoadDriveTypes()
{
DriveInfo[] driveList = DriveInfo.GetDrives();
// Loop through the list of drives
foreach (DriveInfo drive in driveList)
{
// Determine what kind of drive it is
switch (drive.DriveType)
{
// It's a HDD
case DriveType.Fixed:
treeDrives.Nodes.Add(drive.ToString(), drive.ToString(), 3);
break;
//// It's a CD-ROM, DVD-ROM, CD-RW, DVD+-RW or any other similar drive
case DriveType.CDRom:
treeDrives.Nodes.Add(drive.ToString(), drive.ToString(), 0);
break;
//// It's a floppy drive (or other removable drive, i.e; USB Flash Drive)
case DriveType.Removable:
treeDrives.Nodes.Add(drive.ToString(), drive.ToString(), 2);
break;
//// It's a network drive
case DriveType.Network:
treeDrives.Nodes.Add(drive.ToString(), drive.ToString(), 4);
break;
//// It's a RAM drive
case DriveType.Ram:
treeDrives.Nodes.Add(drive.ToString(), drive.ToString(), 6);
break;
}
}
}

Populate a TreeView with all drives in system

private void LoadDriveTypes()
{
DriveInfo[] driveList = DriveInfo.GetDrives();
// Loop through the list of drives
foreach (DriveInfo drive in driveList)
{
// Determine what kind of drive it is
switch (drive.DriveType)
{
// It's a HDD
case DriveType.Fixed:
treeDrives.Nodes.Add(drive.ToString(), drive.ToString(), 3);
break;
//// It's a CD-ROM, DVD-ROM, CD-RW, DVD+-RW or any other similar drive
case DriveType.CDRom:
treeDrives.Nodes.Add(drive.ToString(), drive.ToString(), 0);
break;
//// It's a floppy drive (or other removable drive, i.e; USB Flash Drive)
case DriveType.Removable:
treeDrives.Nodes.Add(drive.ToString(), drive.ToString(), 2);
break;
//// It's a network drive
case DriveType.Network:
treeDrives.Nodes.Add(drive.ToString(), drive.ToString(), 4);
break;
//// It's a RAM drive
case DriveType.Ram:
treeDrives.Nodes.Add(drive.ToString(), drive.ToString(), 6);
break;
}
}
}

get a Bitmap object directly from a web resource

/*
* A function to get a Bitmap object directly from a web resource
*
*/

///
/// Get a bitmap directly from the web
///

/// The URL of the image
/// A bitmap of the image requested
public static Bitmap BitmapFromWeb(string URL)
{
try {
// create a web request to the url of the image
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
// set the method to GET to get the image
myRequest.Method = "GET";
// get the response from the webpage
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
// create a bitmap from the stream of the response
Bitmap bmp = new Bitmap(myResponse.GetResponseStream());
// close off the stream and the response
myResponse.Close();
// return the Bitmap of the image
return bmp;
} catch (Exception ex) {
return null; // if for some reason we couldn't get to image, we return null
}
}

Using the BackgroundWorker in C#

http://www.dreamincode.net/forums/showtopic112547.htm

Load an XML file

OpenFileDialog odlg = new OpenFileDialog();
odlg.Title = "Select a XML file";
odlg.Filter = "XML File|*.xml;*.bmp;*.gif|All File|*.*";
if (odlg.ShowDialog() != DialogResult.Cancel)
{
Application.DoEvents();
txt_XMLFileName.Text = odlg.FileName;

System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
xDoc.Load(odlg.FileName);

tvw_XML.Nodes.Clear();

foreach (System.Xml.XmlNode xn in xDoc.ChildNodes)
{
System.Xml.XmlNode xnx = xn;

TreeNode ttn = tvw_XML.Nodes.Add(xn.Name);
LoadNode(ref ttn,ref xnx);
}

tvw_XML.ExpandAll();

xDoc.Save(odlg.FileName);
}


private void LoadNode(ref TreeNode tn,ref System.Xml.XmlNode xn1)
{
foreach (System.Xml.XmlNode xn in xn1.ChildNodes )
{
System.Xml.XmlNode xnx = xn;
if (xn.Name == "#text")
{
TreeNode ttt = tn.Nodes.Add(xn.Value);
ttt.ForeColor = Color.Blue;
}
else
{
TreeNode ttn = tn.Nodes.Add("<" + xn.Name + ">");
ttn.NodeFont = new Font("Tahoma",8F, FontStyle.Bold);
LoadNode(ref ttn, ref xnx);
}
}
}
}

Save a image from the picture box

SaveFileDialog sdlg = new SaveFileDialog();

sdlg.Title = "Select a file name to save Image";
sdlg.Filter = "JPEG File|*.jpg|Bitmap|*.bmp|GIF|*.gif|All File|*.*";
if (sdlg.ShowDialog() != DialogResult.Cancel)
{
Application.DoEvents();

// System.IO.FileInfo f = new System.IO.FileInfo(sdlg.FileName);


switch (System.IO.Path.GetExtension(sdlg.FileName).ToLower())
{
case ".jpg":
pictureBox1.Image.Save(sdlg.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
break;

case ".bmp":
pictureBox1.Image.Save(sdlg.FileName, System.Drawing.Imaging.ImageFormat.Bmp );
break;

case ".gif":
pictureBox1.Image.Save(sdlg.FileName, System.Drawing.Imaging.ImageFormat.Gif);
break;

default:
break;
}




}

Load a image into picture box

OpenFileDialog odlg = new OpenFileDialog();
odlg.Title = "Select a Iamge file";
odlg.Filter = "Image File|*.jpg;*.bmp;*.gif|All File|*.*";
if (odlg.ShowDialog() != DialogResult.Cancel)
{
Application.DoEvents();
txt_ImageFile.Text = odlg.FileName;
Image img = Image.FromFile(odlg.FileName);

pictureBox1.Image = img;

}

Simple code to save a text file

SaveFileDialog sdlg = new SaveFileDialog();

sdlg.Title = "Select a file name to save";
sdlg.Filter = "Text File|*.txt|All File|*.*";
if (sdlg.ShowDialog() != DialogResult.Cancel)
{
Application.DoEvents();

System.IO.File.WriteAllText(sdlg.FileName, txt_TextContainer.Text);

}

simple code to read a file's contents into text box

OpenFileDialog odlg = new OpenFileDialog();
odlg.Title = "Select a text file";
odlg.Filter = "Text File|*.txt|All File|*.*";
if (odlg.ShowDialog() != DialogResult.Cancel)
{
Application.DoEvents();
txt_TextFile.Text = odlg.FileName;
string s = System.IO.File.ReadAllText(odlg.FileName);

txt_TextContainer.Text = s;

}

Add a tooltip to checked list box

Add a Tooltip object to your form and then add an event handler for the CheckedListBox.MouseHover that calls a method ShowToolTip(); Add MouseMove event of your CheckedListBox which has the following code:

//Make ttIndex a global integer variable to store index of item currently showing tooltip.
//Check if current location is different from item having tooltip, if so call method
if (ttIndex != checkedListBox1.IndexFromPoint(e.Location))
ShowToolTip();

Then create the ShowToolTip method
private void ShowToolTip()
{
ttIndex = checkedListBox1.IndexFromPoint(checkedListBox1.PointToClient(MousePosition));
if (ttIndex > -1)
{
Point p = PointToClient(MousePosition);
toolTip1.ToolTipTitle = "Tooltip Title";
toolTip1.SetToolTip(checkedListBox1, checkedListBox1.Items[ttIndex].ToString());

}
}

Wednesday, July 8, 2009

drawBackgound for a form in c#

private void drawBackgound()
{
Bitmap bmp = new Bitmap(this.Width, this.Height);
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
Graphics g = Graphics.FromImage(bmp);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(new System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.White, Color.FromArgb(203, 225, 252), 90), rect);

this.BackgroundImage = bmp;
}

Tuesday, July 7, 2009

DataView filtering

private void MakeDataView()
{
DataView dv = new DataView();

dv.Table = DataSet1.Tables["Suppliers"];
dv.AllowDelete = true;
dv.AllowEdit = true;
dv.AllowNew = true;
dv.RowFilter = "City = 'Berlin'";
dv.RowStateFilter = DataViewRowState.ModifiedCurrent;
dv.Sort = "CompanyName DESC";

// Simple bind to a TextBox control
Text1.DataBindings.Add("Text", dv, "CompanyName");
}

Get all the Logging user in Window

System.Security.Principal.WindowsIdentity identity =
System.Security.Principal.WindowsIdentity.GetCurrent();

Thursday, July 2, 2009

How To Get IP Address Of A Machine

public static int Main (string [] args)
{

String strHostName = new String ("");
if (args.Length == 0)
{
// Getting Ip address of local machine...

// First get the host name of local machine.

strHostName = DNS.GetHostName ();
Console.WriteLine ("Local Machine's Host Name: " + strHostName);
}
else
{
strHostName = args[0];
}

// Then using host name, get the IP address list..

IPHostEntry ipEntry = DNS.GetHostByName (strHostName);
IPAddress [] addr = ipEntry.AddressList;

for (int i = 0; i < addr.Length; i++)
{
Console.WriteLine ("IP Address {0}: {1} ", i, addr[i].ToString ());
}
return 0;
}

India deafeats West Indies in cricket ODI