Random Generator Class in C#


The C Sharp language has a good support for creating random entities such as integers, bytes or doubles. First we need to create the Random Object. The next step is to call the Next() function in which we may supply a minimum and maximum value for the random integer. In our case we have set the minimum to 1 and maximum to 9. So, each time we click the button we have a set of random values in the three labels. Next, we check for the equivalence of the three values; and if they are then we append two zeroes to the text value of the label thereby increasing the value 100 times. Finally we use the MessageBox() to show the amount of money won.

         

AutoComplete Feature in C#


This is a very useful feature for any graphical user interface which makes it easy for users to fill in applications or forms by suggesting them suitable words or phrases in appropriate text boxes. So, while it may look like a tough job; its actually quite easy to use this feature. The text boxes in C Sharp contain an AutoCompleteMode which can be set to one of the three available choices i.e. Suggest , Append or SuggestAppend. Any choice would do but my favourite is the SuggestAppend. In SuggestAppend, Partial entries make intelligent guesses if the ‘So Far’ typed prefix exists in the list items. Next we must set the AutoCompleteSource to CustomSource as  we will supply the words or phrases to be suggested through a suitable data source. The last step includes calling the AutoCompleteCustomSouce.Add() function with the required. This function can be used at runtime to add list items in the box.



Insert & Retrieve from Service Based Database

Now, we go a bit deeper in the SQL database in C# as we learn how to Insert as well as Retrieve a record from a Database. Start off by creating a Service Based Database which accompanies the default created form named form1. Click Next until finished. A Dataset should be automatically created. Next double click on the Database1.mdf file in the solution explorer (Ctrl + Alt + L) and carefully select the connection string. Format it in the way as shown below by adding the @ sign and removing a couple of "=" signs in between. The connection string in .Net Framework 4.0 does not need the removal of @ & "=" signs. The String is generated perfectly ready to use. This step only applies to .Net Framework 1.0 & 2.0.

Fetch Data from Table in C# 5


Fetching data from a table in C Sharp is quite easy. It First of all requires creating a connection to the database in the form of a connection string that provides an interface to the database with our development environment. We create a temporary DataTable for storing the database records for faster retrieval as retrieving from the database time and again could have an adverse effect on the performance. To move contents of the SQL database in the DataTable we need a DataAdapter. Filling of the table is performed by the Fill() function. Finally the contents of DataTable can be accessed by using a double indexed object in which the first index points to the row number and the second index points to the column number starting with 0 as the first index. So, if you have 3 rows in your table then they would be indexed by row numbers 0 , 1 & 2.


Drawing with Mouse in C# 5.0



This C# Windows Forms tutorial is a bit advanced program which shows a glimpse of  how we can use the graphics object in C#. Our aim is to create a form and paint over it with the help of the mouse just as a 'Pencil'. Very similar to the 'Pencil' in MS Paint. We start off by creating a graphics object which is the form in this case. A graphics object provides us a surface area to draw to. We draw small ellipses which appear as dots. These dots are produced frequently as long as the left mouse button is pressed. When the mouse is dragged, the dots are drawn over the path of the mouse. This is achieved with the help of the MouseMove event which means the dragging of the mouse. We simply write code to draw ellipses each time a MouseMove event is fired.