C#Builder WinForm Application Development Tutorial by Clay Shannon

By: Clay Shannon

Abstract: Learn how to build a simple .RTF viewer with C#Builder in this short introductory article that touches on the C#Builder IDE, escaping strings, and Wacky Warble McGorkle.

So you wanna be a C# coder? Without any ado whatsoever (that is to say, no "ado" as in "much ado over nothing" as well as no "ADO" as in "ADO.NET", as we won't be working with database connections in this article) let us begin by creating a Windows GUI app (with C#Builder you can create console apps, web apps (ASP.NET), WebMethods (aka Web Services), as well as incorporating database connectivity into these (ADO.NET), but for simplicity's sake (the devil take Butterick) we will concentrate on a simple one-form WinForms app (corresponding to a "generic" Windows GUI app).

To play along and follow this step-by-step tutorial, fire up C#Builder if you haven't already.

Select File | New | C# Application. The New Application dialog displays:

The default name will be auto-generated. For a more descriptive name, replace the automatically generated with one of your own, like this:

I have left the default location as is (C:Documents and Settings<Current User>My DocumentsBorland Studio Projects<AppName>

Select the OK button to open the new project in the IDE:

 

2-click the RichTextBox component from the Tool Palette to place said component on the form. It will align to the Northwest corner of the form. Enlarge the form to 480 X 600. Use the Size.Width and Size.Height properties of the Layout category to accomplish this. If necessary, display the Object Inspector's properties to be displayed by category rather than alphabetically by name (right-click on the OI and select Arrange | By Category) and set Layout.Size.Width to 480 and Layout.Size.Height to 600. Now click the richTextBox1 component and change its Width and Height properties to 580 and 528, respectively. To change what a Delphi programmer would think of as the "Left" and "Top" properties, edit the Layout.Location.X and Layout.Location.Y properties (to 6 and 10, respectively).

Place a button component on the form by clicking Button in the Tool Palette and then clicking on the southwest corner of the form. Change the Name property of the Button to btnOpenAFixedFile, and the Text property to Open a File (the ampersand will underscore the following character, making it available via the hotkey); change the Layout.Size.Width property to 128.

2-click btnOpenAFixedFile and add the following code, substituting the path to the file loaded here to a file on your system that you'd like to load.


private void btnOpenAFixedFile_Click(object sender, System.EventArgs e) 
{
     richTextBox1.LoadFile(@"C:\Program  
       Files\Borland\BDS\1.0\readme.rtf");
}

Note the "at" sign preceding the filepath string. This instructs the compiler to treat the string as a literal, rather than parsing it for escape characters. If the @ sign were omitted, you would get the compile-time error message "unrecognized escape sequence". This is caused by the backslashes in the string, which the compiler mistakes for malformed escape sequences. Your other option is to double all of the backslashes in the string, like this:


richTextBox1.LoadFile("C:\\Program   
  Files\\Borland\\BDS\\1.0\\readme.rtf");

but that's kludgy--the "@" method is preferred. Running the app and selecting the btnOpenAFixedFile button looks like this:

Now we'll add another button and an Open File dialog so that you can open any (RTF) file into the richTextBox.

Place a button on the form in the same manner as before, this time to the right of the previous one. Navigate down to the Dialogs category of the Tool Palette and 2-click OpenFileDialog to place one on-no, not the form, but a region below the form which is kind of a built-in nonvisual component repository for the form. Name the button btnOpenAnyFile, and give it the caption "Open Any File"; change its Layout.Size.Width value to 108.

Add the following code after 2-clicking btnOpenAnyFile:


private void btnOpenAnyFile_Click(object sender, System.EventArgs e) 
{
     DialogResult result;

     result = (openFileDialog1.ShowDialog());
     if (result == DialogResult.OK)
       richTextBox1.LoadFile(openFileDialog1.FileName);
}

Running the app and selecting btnOpenAnyFile (and then selecting a file) looks like this (if you happen to select "theWackyMisadventuresOfWarbleMcGorkle.rtf", that is):

This simple app could be enhanced, of course, but this is enough to give you an introduction to creating .NET WinForm apps using C#Builder. Now that you've gotten your feet wet, you can jump into the vast ocean of possibilities that C#Builder makes available.

Finally, I would like to thank Borland for naming C#Builder's source code file extension after me--but now that I will be world-famous for all eternity, what incentive is left? I may as well retire or become a VB "programmer". Oh, well

Clay Shannon is a Borland and PDA-certified Delphi developer and the author of "Tomes of Delphi: Developer's Guide to Troubleshooting" (Wordware, 2001) as well as the novel he claims is the strangest one ever written, "the Wacky Misadventures of Warble McGorkle" (see Wacky Warble, etc. for more information on the 4 Novels application, which contains this and three other novels he has penned).

You can find out more about Clay at: http://hometown.aol.com/bclayshannon/myhomepage/index.html
You can look into Clay's shareware and determine his current availability at:
http://hometown.aol.com/bclayshannon/myhomepage/business.html
You can contact him at:
BClayShannon@aol.com

Server Response from: SC2