Working with files in C#

In this tutorial we will be working with files. How to create a file, edit it, rename a file make a copy an existing file or move it etc.

First thing you should do is to reference the System.IO namespace.

All the classes that will help you handle files are located in this namespace.

How to create a file and edit it?

You can create a file like this:

This will create a file “test.txt” in (more…)

Read More

C# – Check if connected to internet

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. Let’s get started.

(more…)

Read More

C# – Autocomplete Textbox

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. It really helps both you or your users when hunting for something. Of course, it seems difficult to set up but really it isn’t that hard. Enough talk! Let’s 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!
(more…)

Read More

C# – Check if Form is already running

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 :

Example use:

Thanks.

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

Read More

Convert color to hex using C#

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:

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

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

Thanks

Read More

How to remove selection rectangle using CSS

Have you ever noticed that when you click a link (in Firefox,IE etc) a dotted rectangle appears around it? (see the image above)

I don’t have a problem with it. But if you find it boring and want to get rid of it. Then I’ve the best solution for you. You can get rid of it with one line of code.

All you have to do is to insert this into your CSS:

And those rectangles (or outlines) will never appear again.

Read More