Controls

for RuBoard

In the program we have just discussed, mainMenu1 is an example of a control . It is an instance of the MainMenu class. A control is an object that is contained within a form and is used to add functionality to the form. A control can perform many tasks automatically on behalf of its parent form. It simplifies programming, as you do not have to be concerned with painting, invalidating , working with graphics elements, and so forth. The simple menu that we just illustrated would have required a substantial amount of code if we had to implement it from scratch. Controls provide rich reusable code ”a big benefit from programming with objects.

Step 5: Using a TextBox Control

Step 5 of our SimpleForm application illustrates using a TextBox control to display our greeting text. As with earlier versions of the application, you can reposition the greeting by clicking the left mouse button, and you can clear the greeting by clicking the right mouse button. You can also type in your own greeting text. Now you have full editing capability. You can insert characters wherever you wish in the control, cut and paste (Ctrl+X and Ctrl+V), and so forth. All of this editing capability is provided by the TextBox control. Figure 6-10 illustrates the application after the greeting has been repositioned and we have typed in some text of our own.

Figure 6-10. The greeting text is now displayed using a control (Step 5).

graphics/06fig10.gif

Here is the new version of our program. Note that it has both greater simplicity and more functionality. We no longer need member variables for the coordinates or text of the greeting string (this information is now stored in the TextBox control txtGreeting) . We do not need OnPaint any longer, either, because the text box knows how to paint itself. We can then also get rid of the brush. We don't need to handle KeyPress events, because this functionality is handled (in a much more full-blown way) by the TextBox control.

 // SimpleForm.cs - Step 5  ...  public class Form1: Form  {  private TextBox txtGreeting;  private MenuItem menuExit;     private MenuItem menuFile;     private MainMenu mainMenu1;     public Form1()     {        InitializeComponent();        Size = new System.Drawing.Size(300,200);        Text = "Simple Form - Step 5";     }     private void InitializeComponent()     {        mainMenu1 = new MainMenu ();        menuFile = new MenuItem ();        menuExit = new MenuItem ();        // mainMenu1        mainMenu1.MenuItems.Add(menuFile);        // menuFile        menuFile.Index = 0;        menuFile.MenuItems.Add(menuExit);        menuFile.Text = "File";        // menuExit        menuExit.Index = 0;        menuExit.Text = "Exit";        menuExit.Click += new EventHandler(menuExit_Click);        Menu = mainMenu1;        // txtGreeting  txtGreeting = new TextBox();   txtGreeting.Location = new Point(10, 10);   txtGreeting.Size = new Size(150, 20);   txtGreeting.Text = "Hello, Windows Forms";   Controls.Add(txtGreeting);  this.MouseDown +=           new MouseEventHandler (Form1_MouseDown);     }     protected void Form1_MouseDown (object sender,                                     MouseEventArgs e)     {        if (e.Button == MouseButtons.Left)        {  txtGreeting.Location = new Point(e.X, e.Y);  }        else if (e.Button == MouseButtons.Right)        {  txtGreeting.Text = "";  }     }     private void menuExit_Click(object sender, EventArgs e)     {        Application.Exit();     }     public static void Main(string[] args)     {        Application.Run(new Form1());     }  } 

Using the TextBox control is very easy. As part of the initialization we instantiate it and assign the Location , Size , and Text properties. We add our new control to the Controls collection of our form. In the mouse event handler we reposition the control by assigning the Location property. We clear the text by assigning the Text property.

for RuBoard


Application Development Using C# and .NET
Application Development Using C# and .NET
ISBN: 013093383X
EAN: 2147483647
Year: 2001
Pages: 158

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net