Basic Windows Programming Skills

Basic Windows Programming Skills

There are a number of essential Windows skills that are demonstrated in the ch07_03 project in the code for this book, which you can see at work in Figure 7.6. For example, clicking the See Message button in this application displays the message "No worries!" in the application's text box, as you see in Figure 7.6. Clicking the Maximize window button maximizes the window, and so on.

Figure 7.6. Running the ch07_03 example.


Besides the buttons and the text box you see in Figure 7.6, note that this application also uses a label control to display the message "Windows Applications" . Labels are simple controls similar to text boxes, except that the user can't enter text in them directly. To create the label in Figure 7.6, drag a label control from the toolbox onto the form, set its Text property to "Windows Applications" , and use its Font property to set the size of the displayed text to 24 points (a point is 1/72 of an inch).

Displaying Text in a Text Box

We've seen that you can set the text in a text box using its Text property at design time. You can also set the Text property of a text box at runtime, so to display the message "No worries!" in textBox1 when the user clicks button1 , we simply have to add this code to button1_Click in this application:

 private void button1_Click(object sender, System.EventArgs e) {  textBox1.Text = "No worries!";  } 

You can also read the text in a text box using the Text property at any time in your code:

 string text = textBox1.Text; 


Forms also have a Text property, which corresponds to the title of the form displayed in its title bar.

Moving Controls

When you click the Move text box button in ch07_03, the code moves the text box down to the position you see in Figure 7.7.

Figure 7.7. Moving a text box.


You can move any control in a Windows form using the SetBounds method, which you can use like this:

  object  .SetBounds(  newX, newY, newWidth, newHeight  ); 

All measurements like newX , newY , newWidth , and newHeight are in pixels in Windows applications. The newX and newY arguments give the new X and Y coordinates of the control's upper-left corner. In a Windows application, the X coordinates are positive downwards and Y coordinates are positive to the right. The origin, (0, 0), is the upper-left pixel in the client area. (The client area is the area where you draw in a Windows applicationit's the area below any title bars, menus and toolbars , and above any status bars at the bottom. It extends from the inside edge of the left border to the inside edge of the right border.) To move the text box to a new location, we can use code like this:

 private void button2_Click(object sender, System.EventArgs e) {  textBox1.SetBounds(168, 148, 104, 23);  } 

Moving Windows

You can also use the SetBounds method to move the whole window as easily as moving a control. However here, the coordinates you use are screen coordinates, not client coordinates, which means the origin, (0, 0), is at upper-left in the screen. To move the window when the user clicks the Move window button, we can use this code:

 private void button3_Click(object sender, System.EventArgs e) {  this.SetBounds(this.Left + 100, this.Top + 100, 336, 300);  } 

Here, we use the Left and Top properties of the form to determine the current location of its upper-left corner, and add 100 pixels to both those values to set its new location. The Left and Top properties are properties of all controls, which are derived from the System.Windows.Forms.Control class. Because forms are derived from the System.Windows.Forms.Control class, they also support the Top and Left properties.


To get or set dimensions, controls and forms support the Left , Right , Top , Bottom , Width , and Height properties, all of which are read/write. However, these properties, like other runtime properties, are only available at runtime. You won't find them in the properties window.

Displaying Message Boxes

You can display a message box to get input from the user using the MessageBox.Show method. Here's how we display a message box with the text "No worries!" , the caption "Message Box" , and an OK and Cancel button when the user clicks the See Message Box button:

 private void button4_Click(object sender, System.EventArgs e) {  if(MessageBox.Show("No worries!", "Message Box",   MessageBoxButtons.OKCancel) == DialogResult.OK){   textBox1.Text = "You clicked OK.";   }  } 

You can see the message box in Figure 7.8.

Figure 7.8. Displaying a message box.


You can pass a member of the MessageBoxButtons enumeration to the MessageBox.Show method to indicate which buttons to display in the message box; for example, MessageBoxButtons.AbortRetryIgnore displays Abort, Retry, and Ignore buttons:

  • MessageBoxButtons.AbortRetryIgnore

  • MessageBoxButtons.OK

  • MessageBoxButtons.OKCancel

  • MessageBoxButtons.RetryCancel

  • MessageBoxButtons.YesNo

  • MessageBoxButtons.YesNoCancel

The MessageBox.Show method returns a member of the DialogResult enumeration indicating which button the user clicked:

  • DialogResult.Abort

  • DialogResult.Cancel

  • DialogResult.Ignore

  • DialogResult.No

  • DialogResult.None

  • DialogResult.OK

  • DialogResult.Retry

  • DialogResult.Yes

In the code for the Show Message Box button in ch07_03, we check to make sure the user clicked the OK button, and if so, display the message "You clicked OK." in the application's text box.

 if(MessageBox.Show("No worries!", "Message Box",     MessageBoxButtons.OKCancel) == DialogResult.OK){     textBox1.Text = "You clicked OK."; } 

Maximizing and Minimizing Windows

You can set a window's statemaximized, minimized, or normalwith its WindowsState property, which you set to a member of the FormWindowState enumeration. Here's how we maximize the ch07_03 window when the user clicks the Maximize window button:

 private void button5_Click(object sender, System.EventArgs e) {  this.WindowState = FormWindowState.Maximized;  } 

Here are the members of the FormWindowState enumeration:

  • FormWindowState.Maximized

  • FormWindowState.Minimized

  • FormWindowState.Normal

Making a Window Top-Most

When the user clicks the Make top-most button, the code makes the application's window a top-most window, which means it will stay on top of any other standard window. Here's what the code looks like:

 private void button6_Click(object sender, System.EventArgs e) {  this.TopMost = true;  } 

Give this a try. When you make the window top-most, it'll ride over any other standard window, even when you click a standard window that the top-most window partially obscures. Top-most windows are great for displaying control panels in multi-window applications.

Adding Controls at Runtime

You can also add controls to a form at runtime, and even connect them to event handlers at the same time. For example, when the user clicks the button labeled Add Button in the ch07_03 application, a new button appears, as you see in Figure 7.9. When this new button is clicked, its caption will change from "Click Me" to "Clicked!" .

Figure 7.9. Adding a new button at runtime.


When the user clicks the button labeled Add New Button, we can use code similar to the code in the hand-written example, ch07_01.cs. Here's the necessary code. Note that we also add an event handler, newButton_Click , to this new button:

  private System.Windows.Forms.Button newButton;  private void button7_Click(object sender, System.EventArgs e) {  newButton = new System.Windows.Forms.Button();   newButton.Location = new System.Drawing.Point(160,   240);   newButton.Name = "newButton";   newButton.Text = "Click Me";   this.Controls.Add(this.newButton);   newButton.Click += new System.EventHandler(this.newButton_Click);  } private void newButton_Click(object sender, System.EventArgs e) {  newButton.Text = "Clicked!";  } 

This code creates a new button, newButton , positions it, gives it a caption, and adds it to the form's Controls collection, which displays it in the form. It also adds the event handler newButton_Click to the new button's Click event. When the button is clicked, its Text property, which corresponds to its caption, is changed to "Clicked!" .

Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: