Show and Invert RGB Colors in C#


This post will show you how to show and convert colors when the RGB value of those colors are given. We simple use a function and supply the Red, Green and Blue components in the parameters. See image below to have a look at the final output of our WinForms application:



Named Arguments Explained

Named Arguments are an alternative way for specifying of parameter values in function calls. They work so that the position of the parameters would not pose problems. Therefore it reduces the headache of remembering the positions of all parameters for a function.

They work in a very simple way. When we call a function, we would write the name of the parameter before specifying a value for the parameter. In this way, the position of the argument will not matter as the compiler would tally the name of the parameter against the parameter value. This makes it unnecessary to remember the organisation of the parameters in the function definition. 

Optional Parameters Explained

Optional Arguments were introduced in C# 4.0 and this article will show you what is meant by Optional Arguments/Parameters in C#. Optional Arguments are necessary when you specify functions that have a large number of arguments. It can cut time by making you pass on argument values to the most needy parameters. 

Optional Arguments are what their name suggests i.e. even if you do not specify them, its perfectly OK. But it doesn't mean that they have not taken a value. In fact we specify some default values that the Optional Parameters take when values are not supplied in the function call. In this way we have the functionality of easily setting values once and using these values several times as a default unless something different is needed in a situation. 

Printing a Windows Form to Printer and File in C#

The objective here is to print a Windows Form to the Printer or a File on the HDD. You can also have a Print Preview before you print a WinForm. See the Form below that we shall print. We will encounter one small problem i.e. the button, radio buttton and textbox are not required for printing. We only wish to print the written text written above in Green color. There is VS 2012 source code at the very end. See the code and explanations below the image:

Display all Files and Folders in a Directory in C#

You can easily list and display all the files and folders in a C# application using the DirectoryInfo class. First have a look at the application screenshot which uses this class to display the Files and Folders in a specified directory. All the Files and Folders are displayed in a ListView



Identify Any Key Pressed on Keyboard




Now you can capture any key pressed on the keyboard, not only restricted to the English or your language letters but also various other keys such as the Windows Key, Properties Key, F1, F2 and other function buttons, the Home, PageUp and PageDown etc. 

Draw a Bitmap Image on WinForm During Runtime

Would you like to draw a bitmap on the screen? Drawing on the WinForms is very easy using the Graphics()  Class and Objects. You can draw any type of image by taking the image from your HDD and including file types like BMP, JPEG, PNG etc. Watch the screenshot of a file that is drawn to the WinForm:


Create a Web Browser in C#

This post shows you how to easily create a Web Browser with minimal functions containing all standard buttons like the Back, Forward Stop and Home buttons shown below:


Multithreading Basics in C#


This WinForms C# Application will show you the basics of creating and using a thread and a bit more. It will actually show you multiple threads working simultaneously. As you know that threads are meant to do multiple jobs at a time by utilizing the different logical cores of the CPU, we will use this to work the value of multiple Int64 variables. See the application screenshot when the application starts and look for the source code at the end:



What we want in our above application is to created two Int64 variables and simply increase their values using two different threads. When we click the START button, the thread initializes and the counting process starts. The value of the variable increases continuously in the background. When we click the Show Current Value button, the value currently stored in the Int64 variable is shown in the TextBox. You may click the Show Current Value button as many times as you want. Everytime you get an incremented value that we have managed to get via an Infinite Loop.

Lets start by adding the code to create the variables in the program:


public Int64 FirstInt = new Int64();

public Int64 SecondInt = new Int64();

Reading a Text File in C# through Stream Reader


This C# program shows you how you can load an external Text File from your Hard Disk into a Multi-Line TextBox. The procedure consists of creating the StreamWriter object to begin working which is the feature of this code.  Watch the image above.

Working With Enumerations in C#


.Net Enumeration Basics


Enumerations or simply Enums in short provide convenient ways to create and access named numeric constants. These constants can have custom Indexable values that represent each individual element. Unlike arrays, they can have values which are not incremental in steps of ‘1’. Also, these values can be of types other than integers (which is the default) such as bytes, Boolean etc.

A single Enumeration itself has a group of values associated with it. We create an Enum Object before attempting to use the functions that are offered. This value when used with a dot operator on an Enumeration gives the state of the Enumeration Object. When we initialize new Enums of a previously defined Enum, we don’t use the new operator. The new Enums are almost immediately created and initialized to an initial value within the Enum.

Create an Enum


Lets take an example to illustrate this. The traffic lights should provide a simple but complete example on how to create and use an Enumeration. Start off by creating the Enumeration itself:

enum TrafficLights
        
 {
       Red,
       Yellow,
       Green
  };

What is Upcasting and Downcasting in C# ?

Upcasting is basically an object creation mechanism in which we create objects by referring to their base class. We do this by replacing the sub-class by the base-class in the object definition. This particularly comes in handy when you know that a specialized object created will not necessarily use all the functions that it has got to offer. So, replace a subclass(inherited class) by a base-class and all you have done is Upcasting.


Read a Value from the Windows Registry in C# 5.0

In the previous post we saw how to create a registry i.e. write a value into the Windows registry. Now we shall retrieve back the exact value that we had written before. To do this we need to retrieve the value in a String variable.

We shall take the value by its exact location & the name of the property. Note that the property resides in a folder and the value we read is the Data of that property. Have a look at the present snapshot of the system:

  • Root Folder is HKEY_CURRENT_USER
  • Parent Folder is CSharp_Website
  • Property/Attribute/Key Name is Really
  • Property/Attribute/Key Value is Yes!

How to Write into Windows Registry in C# 5.0

Developing a real application almost everytime requires the storing of some important values in the Windows Registry. The Windows Registry gives the power to store variables of different types by any application and use them as and when required. You can also store values in the form of texts, external files or saved documents then why use the Registry? Because the Registry allows above all the values to be stored and read in a standardized way which can be read by compatible applications as and when required. Also, the deletion of corresponding files does not alter the information in the Windows Registry. The contents have to be modified explicitly to delete or modify existing values.

The windows registry can be modified using the C# programming interface. In this section we shall see how to write to a known location in the windows registry. The Windows registry consists of different locations in an explorer style and looks as shown below:

Create TextBox Controls at Runtime


Sometimes you might want to create a user control at run-time.  There are times when you are not sure of what controls to include during the designing phase of your forms.  In these cases what usually is done is that we draw all available controls in the designer and at run-time we simply make the controls invisible during run-time leaving us with workable visible controls. But this is not the right method. What should be done is exactly illustrated below. You should draw the controls at run-time. Yes, you should draw the controls only when needed to save memory.