Archive

Archive for the ‘C#’ Category

C# Project: Data Processing Application (1)

June 4th, 2010 No comments

Introduction

I am a Mechanical Engineer. I write C programs to process a lot of experimental data in my daily work. The core module of these programs – how to interpret and process data – is different from project to project and needs to be rewritten for every new project. However, there are two modules which are almost identical for all the projects – reading data before processing and writing data after processing. The codes for these two modules are reusable and I usually copy and paste them to the new program. Having done this for a long time, I now wanted to write a general data processing application with an easy-to-use GUI.

Data Processing Application

  1. Application Architecture
  2. Input Data Files
  3. Here are some assumptions about the input data files.

    • Data files are text files with data sorted in columns.
    • Every data file can contain up to 99 columns of data.
    • Every line of a data file contains same number of data fields.
    • Data files can have blank lines which will be removed during data reading.
    • Data files can have comment lines which will be removed during data reading.
  4. Data Transfer Between Modules
  5. Output Data Files
Categories: C# Tags:

Maze Game

July 20th, 2009 2 comments

Introduction

This is a maze game that I wrote when I was learning C# and Breadth First Search algorithm. The goal of the game is to find a path that connects the Start point (red square) and the End point (dark green square). You use the arrow keys to move your indicator (blue square). The path is marked with light green squares. If you get stuck, just click “Game” -> “Solution” to view the solution. Here is a screenshot of my “Maze Game”:

MazeGame

How to Play

As mentioned above, the goal of the game is to find a path that connects the Start point (red square) and the End point (green square). Click “File” -> “New Maze” to start a new game. Use the arrow keys to move your indicator (blue square). If you get stuck, just click “Game” -> “Solution” to view the solution.

Download

You are welcome to download the game and try it out. The game can be downloaded here. After downloading the file, unzip it. Then double click “MazeGame.exe” to run. If you find any bugs or have any comments, please let me know. Thanks for looking!

Note
You need Microsoft .NET Framework 2.0 to run this tool. Microsoft .Net Framework can be downloaded from Microsoft website.

Categories: C#, Game Tags: ,

Visual C# Note: Open a File in Your Own Program When Double Clicking the File

July 19th, 2009 No comments

Suppose you want to open *.cwd files in your own program when you double click them. Here is how to do it:

  1. First of all, you need to make sure your program understands the structure of *.cwd files and knows how to open them.
  2. Set your program to be the default application to open *.cwd files. This is done by following these easy steps:
    1. Right click a .cwd file.
    2. Select “Open With” -> “Choose Program …”
    3. In the “Open With” dialog, check “Always use the selected program to open this kind of file” and press “Browse…”
    4. Find your application in the open file dialog and click “OK”.

    These way when you double click a .cwd file, the default action will be openning it with your application.

  3. Add a constructor in your main window form class. This constructor takes one parameter (the file name) from the Main(string[] args) function and open the file in your program.

Here is an example code that shows how to open *.cwd files in your C# application:

Program.cs:
 
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
 
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if (args.Length > 0)
                Application.Run(new Form1(args[0]));
            else
                Application.Run(new Form1());
        }
    }
Form1.cs:
 
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        public Form1(string filename)
        {
            InitializeComponent();
            if (filename != null)
                LoadFile(filename);
        }
 
        private void LoadFile(string filename)
        {
            //
            // put your code here to load *.cwd file
           //
        }
    }
Categories: C# Tags:

Card Memory Game

November 17th, 2008 2 comments

Introduction

This is a card game that I wrote when I was learning how to program C#. The goal of the game is to clear the table by pairing all the cards. It has three difficulty levels: “Easy”, “Medium”, and “Hard”. Different level contains different numbers of cards on the table. At the “Hard” level, you will deal with 48 cards, which is pretty challenging. Also, the back image of the cards can be selected from “Island”, “Fish”, “Moon Flower”, “Space”, and “Toy Cars”. Below is a screenshot of my “Memory Card Game”:

cardmemorygame

How to Play

To play a game, choose “File->New Game” and select a level. After the cards are dealt, click on any card and that card will flip to let you see its face. Click on a second card. This second card will flip to show its face too. If the two cards are identical, they will disappear. Otherwise they will flip back so that you won’t be able to see their faces. Keep clicking on the cards until all the cards are paired and disappear.

Download

You are welcome to download the game and try it out. The game can be downloaded here. After downloading the file, unzip it. Then double click “CardMemoryGame.exe” to run. If you find any bugs please let me know. Thanks for looking!

Note
You need Microsoft .NET Framework 2.0 to run this tool. Microsoft .Net Framework can be downloaded from Microsoft website.

Categories: C#, Game Tags: , ,

IE Address Bar Editor

November 13th, 2008 No comments

Introduction

If you want to remove un-wanted URLs that Internet Explorer (IE) stored in the drop-down address bar, you’ll have to open the Windows registry editor, find the URLs that IE stored in the registry, delete un-wanted entries, and re-order the rest URLs. If you do this manually, the whole process is a little tedious and dangerous (messing with Windows registry is always dangerous).

To make this task easier, I wrote a small program called “IE Address Bar Editor” using Visual C#. This little tool reads the URLs that IE stored in the Windows registry. Then you can easily modify (delete, edit, and re-order) these URLs and save the changes back to the Windows registry. Below is a screenshot of my program.

Download

I have tested this program on three computers running Windows XP and it worked fine on all three PCs. You are welcome to download and try it out. The program can be downloaded here. After downloading the file, unzip it. Then double click “IEAddressBarEditor.exe” to run. If you find any bugs please let me know. Thanks for looking!

Note

  1. Before using this tool, you need to close any IE window that is running.
  2. You need Microsoft .NET Framework 2.0 to run this tool. Microsoft .Net Framework can be downloaded from Microsoft website.
Categories: C#, Tools Tags: , ,

Visual C# Note: Embed Wave File in .Net Application

March 20th, 2008 No comments

When writing small-scale applications, I think it’s good practice to embed resources, such as images, sound waves, and icons, in the application. This way you don’t need to worry about remembering to add separate files when you distribute your applications — a single .exe file is all you need.

I just finished a small project (“Tetris”) that utilized embedded wave files. Using an embedded wave file involves two steps. The first step, off course, is to “embed” the wave file in the application. This is actually is very easy. With you project open in Visual Studio 2005, simply drag your wave file and drop it in your project in the “Solution Explorer”. Then set “Build Action” to “Embedded Resource”, see Figure 1. When you build the project next time, the wave file will be embedded in the application.

Wave File Properties
Figure 1. To embed a file, set “Build Action” to “Embedded Resource”.

The second step is to access the wave file when you need to play it. You need to create a stream and associate your embedded wave file to the stream. Then you can use a SoundPlayer object to read from this stream and play the embedded wave file. Below are the codes to play my embedded wave file. Note that when you access the embedded wave file, you have to use format “Namespace.WaveFileName”. In my case, the namespace is “Tetris” and the wave file name is “Tetris.wav”. Therefore I have to use “Tetris.Tetris.wav” to access it.


using System.media;
using System.Reflection;
// Open the embedded wave file using a SoundPlayer object
Assembly asm = Assembly.GetExecutingAssembly();
Stream sm = asm.GetManifestResourceStream("Tetris.Tetris.wav");
// Play embedded wave file
sPlayer = new SoundPlayer(sm);
sPlayer.Play();

Categories: C# Tags:

Visual C# Note: Set Icon of Compiled .exe File

March 20th, 2008 No comments

The default icon for compiled .exe files in Visual Studio 2005 is the boring blank window icon. However, you can create a cool-looking icon and assign it to your compiled .exe file. Here is how to do it:

  1. In the “Solution Explorer”, right click your project name and select “Properties”. The “Properties” window will open.
  2. In the “Properties” window, select the “Application” tab. Click the “…” button on the right of the “Icon” dropdown box to select the icon file you created for your .exe file.
  3. Build the project then you will have a .exe file with your nice-looking icon -:), like “Tetris.exe” in Figure 1.
Exe File with Nice Icon
Figure 1. “Tetris.exe” has a customized icon while “Tetris.vshost.exe” has the default icon.
Categories: C# Tags:

Visual C# Note: Definition of Stream

March 19th, 2008 No comments

Found on About.com:

Definition: A stream is an important concept in modern programming. Earlier programming languages such as Fortran, Cobol and Basic had input and output built in to those languages.

Modern languages like C, C++ and C# tend to avoid having this and instead implement input and output via library functions. Output involves sending a stream of byte to a device and whether that device is the screen, an object in memory, a file on disk, another computer on a network or a printer is unimportant. The same routines can be used with different destinations.

Categories: C# Tags:

Visual C# Note: Object Serialization

February 2nd, 2008 No comments

Sometimes it would be easier to read or write entire objects. C# provides such a mechanism, called object serialization. A serialized object is an object represented as a sequence of bytes that includes the object’s data, as well as information about the object’s type and the types of data stored in the object. After a serialized object has been written to a file, it can be read from the file and deserializedthat is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.

Class BinaryFormatter (namespace System.Runtime.Serialization.Formatters.Binary) enables entire objects to be written to or read from a stream. BinaryFormatter method Serialize writes an object’s representation to a file. BinaryFormatter method Deserialize reads this representation from a file and reconstructs the original object. Both methods throw a SerializationException if an error occurs during serialization or deserialization. Both methods require a Stream object (e.g., the FileStream) as a parameter so that the BinaryFormatter can access the correct stream.

Categories: C# Tags:

Visual C# Note: How to Add a New Line in C# Textbox

February 1st, 2008 No comments

If you’re building a Windows Form Application, for example with C# or Visual Basic, then you may need to add a new line character, for example in a multiline TextBox. You do this using Enviroment.NewLine to insert the line break as follows:

textBox1.Text="First Line" + Environment.NewLine + "Second Line";

Categories: C# Tags: