After completion of today's lab, you should be able to
The .NET framework provides a number of predefined dialog box classes which you can adapt for your own applications. Rather than writing your own Open File Dialog or Page Setup Dialog, for example, you can use the predefined classes and methods in your program to do the work.
You then write the code to work with the Windows Forms Dialog Boxes
Activity: Create a New Project called WindowsUI in Solution Lab4
WindowsUI as the project name, U:\ as the Location, and Lab4 as the solution name.MainForm.cs
Activity: Add Controls to the MainForm
openFileDialog1 unless we change itopenFileButton
private void openFileButton_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
}
}
One of the greatest inventions since sliced bread is I/O streams. We can easily read in text from a file using the .NET StreamReader class
if (result == DialogResult.OK)
{
System.IO.StreamReader reader = new System.IO.StreamReader(openFileDialog1.FileName);
this.textBox1.Text = reader.ReadToEnd();
reader.Close();
}
Activity 3: Display the contents of the file in textBox1
openFileButton_Click to create a StreamReader object and read to the end of the file, displaying the text in textBox1Note: Being careful programmers, we should use a try/catch/finally block for any I/O, placing the Close method call in a finally block.
System.IO.StreamReader reader = null;
try
{
reader = new StreamReader(openFileDialog1.FileName);
this.textBox1.Text = reader.ReadToEnd();
}
catch (Exception ex)
{
MessageBox.Show("File Open Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (reader != null)
reader.Close();
}
Many modern GUI applications use some version of menus: drop-down menus and context menus (pop-up menus accessible by right-click an object) being the most common.
Activity: Add a main menu to your form
MenuStrip control to your FormopenToolStripMenuItemexitToolStripMenuItemNote: by default, access keys will not show up as underlined. Press the alt key to see them show up. To change the default behavior under Windows XP, right-click the desktop and select properties. Then select Appearance tab > Effects button. Un-check the box "Hide underline letters"
The MenuStrip's smart task can be used to Insert Standard Items
Activity: Add an ExitToolStripMenuItem event handler to end the application
Close) the form, ending the application
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
Activity: Call the openFileButton_Click event handler if the user selects the File > Open menu item
openFileButton_Click for the Click eventThere is a lot of functionality provided with menus, and should you decide to use them in your CS 446 project, take some time on your own to study up on the options
Context Menus provide users with an easily accessible menu (usually by right-click) of frequently used commands associated with the selected object.
The items in a context menu are often a subset of the items from main menus that appear elsewhere in the application.
TextBoxes come with a basic functional context menu built in.
Toolbars are built-in .NET controls often used to display small buttons that provide easy and fast access to frequently used commands. Modern toolbars are not limited to the display of just buttons however. In addition, they may include drop downs, combo boxes, and other advanced controls.
Essentially, a Toolbar control consists of an ImageList object and a collection of buttons.
Visual Studio .NET makes creating toolbars for your application very easy.
Activity: Add an ToolBar to your MainForm
The ToolStripContainer control that provides panels along each of the four sides of a form to contain menus, toolbars, and status bars.
The user clicking on a toolbar button causes a toolStripButton Click event. Normally we will want to re-use our menu click event handler with our tool bar buttons
URL: http://www/cwu.edu /~gellenbe/446/labs/lab4.php
Author: Ed Gellenbeck, Department of Computer Science, Central Washington University, gellenbe@cwu.edu
Copyright 2006 Ed Gellenbeck, Central Washington University
Last modified: April 07, 2008