C# Simplified, part 6: Working with Windows Applications - Working with WinForm controls using Visual Studio .NET
(Page 4 of 4 )
It is very easy to add controls using Visual Studio .NET. You don't have to write any code as explained in the above walkthroughs.
Start a new Visual C# .NET project (File | New | Project -> Visual C# Projects -> Windows Application) and add a TextBox and a Button control using ToolBox. You should be able to see the Properties Window on the right side. Otherwise, press F4 to make it visible. You can modify the various properties of a WinForm control using this window as shown in Figure 6.3.

Figure 6.3 Properties Window
You can also select the required control from the top drop-down list and read a short description of each of the properties from the bottom section of this window. Double click anywhere on the Form to view the code generated by Visual Studio .NET.
Working with TextBoxes
With the help of Textboxes, you can enter information. That is why this control plays an important role in all applications. The entered information can be saved to a file, XML file or a database, according to your requirements. Textboxes are of two types, SingleLine and MultiLine. The difference between these two types is that multi-line boxes contain scroll bars and users can enter more that one line of text into them. It is similar to TextArea in HTML.
The super class of this control is TextBoxBase. This class provides functionalities for advanced controls like RichTextBoxes. Listing 6.6 illustrates the working of a multi-line textbox control:
Listing 6.6
001: // MyText.cs
002: // ---------
003: using System;
004: using System.Drawing;
005: using System.Windows.Forms;
006:
007: public class MyText: Form
008: {
009: // Object of TextBox created
010: TextBox t1 = new TextBox();
011:
012: MyText()
013: {
014: this.Controls.Add(t1);
015: t1.Location = new Point(50,50);
016: t1.Size = new Size(200,70);
017: t1.Multiline = true;
018: t1.ScrollBars = ScrollBars.Vertical;
019: t1.TextAlign = HorizontalAlignment.Center;
020: t1.CharacterCasing = CharacterCasing.Upper;
021: this.Text = "TextBox Control Demo";
022: }
023:
024: public static void Main()
025: {
026: Application.Run(new MyText());
027: }
028: }
Compilation
csc MyText.cs
Execution
MyText
Output

In the Textbox, whatever you enter will appear in uppercase, because of line 20. Also, the text will be aligned to center due to the statement in line 19. In Visual Studio .NET, you can change the above properties using the properties window after selecting the TextBox.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |