C# – Check if connected to internet

Posted on July 23rd, 2010 in C#, Tutorials | No Comments »

I was working on an application which checked for updates. I used WebRequest/WebResponse for checking updates. It showed a continuous Progressbar while checking for updates. But there was a problem. It worked properly when you were connected to internet but it  just showed the progressbar all the time if not connected.

The solution for that was to check for internet connection before checking for updates. If connected then check for updates otherwise show an error.

Checking for the state of internet connection is very simple.

Firstly, import System.Runtime.InteropServices namespace. Add this to the top of your code:

using System.Runtime.InteropServices;

Then use this code for checking connection state:

For C# Beginners: Add this code above or below public Form1() { … }

// API Method
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);

// A method for checking the state
public static bool IsConnected()
{
    int Description;
    return InternetGetConnectedState(out Description, 0);
}

Now you can use the above method to check for connection before doing anything that requires internet connection.

Example use:

// If internet connection is active
if (IsConnected())
{
    // do something
}
else
{
   MessageBox.Show("Please connect to the internet.");

If you have any questions please don’t hesitate to post a comment.

Google Chrome 5 Stable offline installer

Posted on May 27th, 2010 in Uncategorized | No Comments »

Google Chrome 5 stable version has been released. This stable version is not only for Windows but also for Mac & Linux.

Here’s the standalone/offline installer for the latest stable version:

Download Google Chrome 5.0 Stable

Crop Image – C# and VB.NET

Posted on May 24th, 2010 in C#, VB.NET | 5 Comments »

For cropping an image in C# & VB.NET we can use the Graphics class. We’ll use a method (or function in VB.NET) which will take 2 parameters, a source image and a rectangle of the section that should be cropped.  Let’s code!

The Code


C#:

public Bitmap CropImage(Bitmap source, Rectangle section)
{

 // An empty bitmap which will hold the cropped image
 Bitmap bmp = new Bitmap(section.Width, section.Height);

 Graphics g = Graphics.FromImage(bmp);

 // Draw the given area (section) of the source image
 // at location 0,0 on the empty bitmap (bmp)
 g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);

 return bmp;
}

// Example use:

Bitmap source = new Bitmap(@"C:\tulips.jpg");
Rectangle section = new Rectangle(new Point(12, 50), new Size(150, 150));

Bitmap CroppedImage = CropImage(source, section);


VB.NET

Function CropImage(ByVal source As Bitmap, ByVal section As Rectangle) As Bitmap

 ' An empty bitmap which will hold the cropped image
 Dim bmp As New Bitmap(section.Width, section.Height)

 Dim g As Graphics = Graphics.FromImage(bmp)

 ' Draw the given area (section) of the source image
 ' at location 0,0 on the empty bitmap (bmp)
 g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel)

 return bmp
End Function

' Example use:

Dim source As New Bitmap("C:\tulips.jpg")
Dim section As New Rectangle(new Point(12, 50), new Size(150, 150))

Dim CroppedImage As Bitmap = CropImage(source, section)

Explanation

The above code will work perfectly but it seems confusing, doesn’t it? You might have been wondering how it works. Well…

We first make an empty bitmap of the same width/height as the section rectangle. This bitmap will hold the cropped image. We use the section rectangle’s width/height because it determines the size of the cropped image. Then we use the Graphics class’s DrawImage() method (function) for the cropping. It draws the given area (section) of the source image on the empty bitmap and that’s it. The empty bitmap now holds the cropped image.

Source Code

Download Source Code for C#

Download Source Code for VB.NET

If you have any questions, don’t hesitate to post a comment.

Thanks

C# – Autocomplete Textbox

Posted on May 15th, 2010 in C#, Tutorials | 6 Comments »

There are times when you’ll need an autocomplete textbox. Autocomplete textbox makes the user’s life easier. It shows suggestions as the user types. In case of a web browser, if you type “goo” it will automatically show all the suggestions starting with or containing “goo” like “Google, Goofy” etc. Enough talk! Lets get on with it.

Fortunately, textbox has a built-in autocomplete feature! We don’t have to do any coding.

You can either set it up from the Designer (if you are using Visual Studio or #Develop) or do it via code. It is simple both ways. Lets do it!

Setting up AutoComplete

For setting up autocomplete you’ll have to use three properties of the TextBox class — AutoCompleteSource, AutoCompleteCustomSource and AutoCompleteMode. Here’s an explanation of these properties: Read the rest of this entry »

C# – Check if Form is already running

Posted on May 12th, 2010 in C#, Tutorials | 2 Comments »

To check if a form is already running you’ll have to look for it in the Application.OpenForms collection. When a form is opened it is added to that collection.

Here’s a boolean method that checks whether a form is open or not :

private bool CheckForm(Form form)
{
   foreach (Form f in Application.OpenForms)
       if (form == f)
          return true;

 return false;
}

Example use:

// if form2 is not running

if (!CheckForm(form2))
  {
      // do something
  }

Thanks.

If you have any questions, don’t hesitate to post a comment.

Blancpain Replica

Posted on April 27th, 2010 in Reviews | No Comments »

Blancpain watch is a brand, which believes in the tradition of time and honors it with the most cutting edge technology. In the year 1735, Jehan-Jacques Blancpain established one of the finest watch making companies in the history of time and made it revolutionary in many aspects. Opted by armies of the most powerful countries in the past like US, Italy, France etc. for its scientific know how, this brand has been hoisting its flags for over two centuries now. It has created thinnest and the smallest master pieces in watches and heralded the era for exceptional and revolting concepts in the global watch industry.

Blancpain watches have mastered the art of bringing along the past and present with their classic contemporary models like Women’s Collection, Sports series, L-Revolution, Villeret, Le Brassus and Leman amongst others. Capturing the unimaginable movements and complications in its techniques Blancpain watches still retain the traditional watch shapes to root their believes to the olden culture. These watches have been ruling the rooster in the world of glamour, sports, and entertainment world. Rich and celebrated people adorn this watch to grace the oomph factor in their personalities and tech savvy people adore this brand for its constant surprises in the technical upgradation.

Bring out the charm and magnetism of your persona and swagger in confidence and excellence of Blancpain watches but at most unbelievable costs. The Blancpain replica watch presents to you the perfect style and grace of the original brand and promise to lace you with the most contagious panache and seductiveness. Care should be taken not to expose these replica watches to water. Capture your world with the flamboyance of this replica watch and let people turn green with envy. Unearth the charisma of Blancpain replica and vouch for its impact yourself before the stock gets over.

C# Auto click button and auto fill form

Posted on February 17th, 2010 in C#, Tutorials | 19 Comments »

da-app

This tutorial will show you how to auto fill forms and click buttons in a website using the webBrowser control. When you learn to do this you can make your own web bots!

To show you how autoclick/autofill works we’ll make a simple Google AutoSearch Bot.

So lets begin… Read the rest of this entry »

Convert color to hex using C#

Posted on December 29th, 2009 in C#, Tutorials | 4 Comments »

So you want convert a .NET color to hex or a hex color to .NET color. Well, its very easy to do this. We won’t have to write lots of code or a special class for it. Thanks to ColorTranslator class. We can convert between these with only 1 line of code. Here’s how its done:

To convert a Hex color to .NET color use this code:

Color color = ColorTranslator.FromHtml("#000000");

For converting a .NET color to Hex color, use thise code:

Color color = Color.FromArgb(0, 100, 50, 100);

// Get the hex color
string HexColor = ColorTranslator.ToHtml(color);
 

That was simple & easy. If you have any questions don’t hesitate to leave a comment.

Thanks