Monday, April 14, 2008

C# Code Snippits

I will be posting some Handy code snippets which i came across through Googling while building my Software

1. To Show a Message Box Insert the button , Label or any other instance on which u want to display the Dialog....


Double Click It and write this code

MessageBox.Show(" Hello World "); // This calls the mesage box...replace Hello World with your Text

2. To Exit From The Application :

Again create an instance and duoble Click it..then type

Application.Exit(); // This Exits the application


3. To Run any Application Programatically

Select an Instance and double click it...then type this code

System.Diagnostics.Process.Start("iexplore",""); // Start the web browser. ( replace iexplore with your application

or you can also try this

System.Diagnostics.Process.Start("filename.html"); // opens an HTML file with the name "Filename"

4. To Download any image or any form of data to your computer from a website :

using System.Net;

WebClient webClient = new WebClient();
webClient.DownloadFile("http://mysite.com/myfile.txt", @"c:\myfile.txt"); // This will download myfile.txt from www.mysite.com and will save it into C: Drive by the name myfile.txt

5. If you are using Taskbar Notification in your app..you will find this useful

Show();
WindowState = FormWindowState.Normal; // If you assign it to a Contextlabel..when u will click it it will open the minimised application

6. Opening an Image in a pictureBox ( make sure you edit the code if your picturebox is not 1

try

{

OpenFileDialog open = new OpenFileDialog();

open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";

if (open.ShowDialog()==DialogResult.OK)

{

pictureBox1.Image = new Bitmap(open.FileName);

}

}

catch (Exception)

{

throw new ApplicationException("Failed loading image");

}

7. To stretch The image

pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;

8. To auto Size the Picture Box

pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;

9 To Center Image

pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;

10. To Save images in Clipboard

if (Clipboard.GetDataObject() != null)
{
IDataObject data = Clipboard.GetDataObject();

if (data.GetDataPresent(DataFormats.Bitmap))
{
Image image = (Image)data.GetData(DataFormats.Bitmap,true);

image.Save("image.bmp",System.Drawing.Imaging.ImageFormat.Bmp);
image.Save("image.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
image.Save("image.gif",System.Drawing.Imaging.ImageFormat.Gif);
}
else
{
MessageBox.Show("The Data In Clipboard is not as image format");
}
}
else
{
MessageBox.Show("The Clipboard was empty");
}


11. Login Panel :)

Ever Wanted To Create a Login Panel through which you will goto ur Form..well here it is..Enjoy the code

string text = textBox1.Text;   // change textBox1 to ur TextBox number
if (text == "//your text//")  // PLace your code or text in the quotes
{
frmAbout frm = new frmAbout();  // change frmAbout to Your Forms Name
frm.ShowDialog(this);
frm.Dispose();
}
else
{
MessageBox.Show("error : The key you typed is incorrect");   // change the message to ur needs
}








No comments: