C# Code Snippet Index

Very simple console barchart

Well, I don't think I can make this any simpler. I hope newbies in the C# language can learn from it. (Read More)
1

Bouncing ball

Wanted to let two forms work together. One form would be the input form and the main form would consume this data. I managed to succeed with the help of a few of my friends here at daniweb., who helped me setup a static form. Just posted a bit of the most relevant code here, the rest of the... (Read More)
1

Sample C# Windows Form Login Screen

Here is a sample code snippet of logging in a user against a user table in an MSSQL database with password encryption. Scott Knake Custom Software Apex Software, Inc. (Read More)
1

Check for Single-Instance

A simple way to check if the current C# program is the only instance of itself running. (Read More)

Switch Variable Values

The xor operator allows you to switch the values of two integer variables without the need for a third, temporary variable. (Read More)

Simple .NET Remoting Demonstration - Part II - Broadcasting

Demonstrates using .NET Remoting to broadcast to client subscribers. There are three parts: 1) Server.cs - for console app responsible for broadcasting messages (date/time stamp currently) 2) Client.cs - for console app that registers with server to subscribe to broadcasts 3) Agent.cs - class... (Read More)

Play a wave file (C#)

A small Windows Application to show you how to play a wave (.wav) sound file. I am using the C# IDE from SharpDevelop and the runtime dotnetfx 1.1 from Microsoft, both free downloads. This forms a small and fast student system to write and debug C# programs. From there you can later graduate to... (Read More)

check if a Yahoo! IM user is online

This simple static class contains a static method called sOnline that accepts a username as a string param. it make a call to an old yahoo api that will determine if a Y! user is online. This only returns true if the user is not using stealth, this will return false if a user is online but... (Read More)
4

Chasing lights, running ants.

Ever wanted to know how to implement running lights? well here is your chance to find out. Open a new Forms application enlarge the Form a bit and drop a Panel and a Timer on it. Add the class ChasingLights to the solution, see code. Implement a form Load, panel Paint and Timer Tick event. See... (Read More)

Passing data between forms

A common question that routinely comes up is how to share data between forms. I have outlined two common ways to pass data between forms, among many others. I have used a public property on the output form to accept a value and update the display. I also have a method accepting parameters from... (Read More)
1

FileSystemWatcher (exploring a myriad of things Windows does behind the scenes)

The FileSystemWatcher class is phenomenally powerful—and staggeringly simple. Start up a new Windows Forms application. Then drop FolderBrowserDialog, FileSystemWatcher, label, listbox, and button controls onto the form. Setting up Properties: Set up the name properties of the controls;... (Read More)
2

Animation: Rotate a string with a timer.

Start a new Forms application. Drop a Panel and a Timer control on it. Set up a Timer_Tick, Form_Load and a Panel_Paint eventhandler and fill in the code. Run the app and watch the string rotate. (Read More)
2

Simple .NET Remoting Demonstration

Quite a few people have been asking how they can get their applications to talk to each other. More specifically, they have been wanting to know how to access information from another running application using .NET Remoting. Not having done this before, I decided to take up the challenge and... (Read More)
1

Numbers-Only Textbox

Use regular expressions, regex, to create an efficient textbox that only takes in digits as input. (Read More)

Export to excel

the code tries to export the table memberinfo to excel. (Read More)

Delegates are function pointers

Way back years ago I tried to understand delegates. See the snippet how I did it. (Read More)
1

How to make your controls moveable?

Here's I wrote some code to move controls on the form 1- Assign (Control_MouseMove, Control_MouseDown, and Control_MouseUp) to the controls you need it movable. (Read More)

How to bind ComboBox to an array of object?

Here's I wrote some code to say binding not means control and data from database, it may be class holds some data (Read More)

Sort a list of integers descending

The Sort method of the List class sorts everything the default way : ascending. You can use the Reverse method of this class to get what you want or you could implement the IComparer interface like in this snippet. (Read More)

Random number string between 1 and 50

This generates a random number string. I needed to post this code to link to it :P (Read More)
1

Simple Class and Method Example

I wanted to make something for beginners, to show and easy way of creating and using a class and it's methods. So here is a customer class, and a Console application that takes advantage of the class. It is mainly set, and get methods, with one or two extra methods you can play around with. It... (Read More)

Using List and Random

I haven't really used Lists very much in the past so I wanted to mess around with them today and see if I could do anything that was neat. In this snippet I create a list from a text file. In my case I used names of people I know. The program creates a list from the file using StreamReader. If... (Read More)

Simple Data Access Layer against SQL Server

If you need to execute SQL Statements\Stored Procedures\UDFs which return\don't return results, Here I've developed this class to be used in Data Access Layer. (Read More)

How to validate user input?

Here's I wrote some code to validate user input without using if\else statements. I believe in scalability is the most important aspect in our problems solution. Let's say we are required to permit only numerics in some text boxes; we shouldn't develop something like that if(((e.KeyChar < '0' ||... (Read More)

How to insert\retrieve image in SQL Server database?

Here's I wrote some code to insert\retrieve images from SQL Server database 1- Create table to hold Image ID, Name and itself USE GO /****** Object: Table . Script Date: 07/10/2009 23:46:46 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO (Read More)

How to create SQL Server Login programmatically?

Here's I wrote some code to create login on SQL Server. 1- Add references (Microsoft.SqlServer.Smo, Microsoft.SqlServer.SqlEnum and Microsoft.SqlServer.ConnectionInfo) to your project. 2- Call method CreateLogin which takes to some arguments (documented) (Read More)

How to backup database programatically?

Here's I wrote some code to backup your database 1- Add references (Microsoft.SqlServer.Smo and Microsoft.SqlServer.ConnectionInfo) to your project. 2- Call method BackupDatabase which takes to arguments database name and backup file path with extension .bak (preferable) (Read More)

Moving a rectangle around the screen with the Matrix class.

The matrix class in .NET is not a full featured matrix class as mathematicians would like to see it, still you can do amazing things with it. Create a new windows form application. Fill in the code and you are ready to experiment. In the code I do something with a translate transform, but you... (Read More)

SQL Query Aggregator

This is a query aggregator that stacks up queries and parameters until you hit the hard limit of 2010 set by the SQL Server/Driver, or you call .RunQuery() Scott Knake Custom Software Apex Software (Read More)

Check if Excel Installed

three lines of code to check MS Excel is installed on PC (Read More)

Embedding Resources to Executable

Instead of creating a new folder in users application folder under program files and putting there some visible resources, embedding those resources to single executable is better. in this snippet i will show playing an embedded sound file. (Read More)

Process Manager

Shows how to create a simple 'Process Manager'. and control processes (Read More)

Capturing numeric input in a TextBox

Sometimes you only want to enter numeric data in a textbox. You have two options here : Let a user type whatever he/she wants into a textbox, validate the input and show him or her an annoying message that he/she has done something wrong.:angry: Or: capture the key before input, check if it's... (Read More)

Custom checkbox

I wanted to make an array of bits which I could click on and off at will. Sort of a register you could manipulate. First thing that came up was a checkbox control. But I wanted to change the usual checkmark with a 1 or a 0. With tips from JerryShaw and Ramy Marhous I came up with the following. (Read More)

Read/Write To Text File

This seems to come up every so often, so thought I would add it here so people can have a look if they search the site. Reading and writing to a text file is a very basic thing in C#, most of us have to do it from very early on and several times later. It's not exactly secure (as you can... (Read More)

Leap year tester

Although the DateTime structure in .NET has an IsLeapYear method. Lots of people in here like to do this on their own. See this snippet on how it can be done. This is in C# but other language adepts might benefit from it also. (Read More)

Check if a string contains only letters

Just a little snippet to check if a string contains only letters. (Read More)

Convert Xml document to String

Converts Xml document to string (Read More)

Checking on Date and Time (C#)

Is it Mother Date and Father Time? Any way, getting all the different date and time displays working can be imperturbably perplexing at times. You take a look at it in this Windows Console Application. (Read More)

The 2000 first primes

Blink and you will see them appear in a Console window. This is a translation to C# of a Modula-2 implementation by N.Wirth, the inventor of Pascal. The tested integers are obtained by incrementing alternatively by 2 and 4, thereby avoiding multiples of 2 and 3 in the first place. This list starts... (Read More)

Forum Tools


About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC