Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

C# ПІДРУЧНИКИ / c# / Hungry Minds - ASP.NET Bible VB.NET & C#

.pdf
Скачиваний:
128
Добавлен:
12.02.2016
Размер:
7.64 Mб
Скачать

Figure A-1: The VB.NET GUI with the major sections highlighted

If you are new to IDE development or to VB.NET, then take a few minutes to try out some of these features:

§Menu: Usually context-sensitive, gives you a pull-down approach to many commands and environment options.

§Toolbar: Also context-sensitive, the toolbar is a subset of the commands that you will find on the menu. This is a way to quickly call commands, such as

Save, Print, and Run, that are used more frequently than other commands. §Layout bar: This is another kind of toolbar that supplies many of the layout

features used in designing a form. Features like aligning controls to the left, making controls the same size, and making controls equidistant are among the options available. Many other context-sensitive toolbars are available for your use, too; simply right-click any toolbar in a vacant slot and you will see the list of the other available toolbars.

§Solution Explorer: This is a tree-view-style interface that shows you all the pieces to your application, or solution. This shows you all the windows and modules, and any other items of that nature, that are part of the solution and that you may want to edit.

§Properties window: This is where you will spend a lot of time fine-tuning the behaviors of your application. You set the individual features of a particular object (always the one in focus) so that it appears the way you desire.

Some examples are push button text value and size, text label values, form background colors, and so on.

§Toolbox: The selection area for the controls that you want to place on a form you may be designing. Push buttons, drop-down lists, picture buttons, single-line edits, radio buttons, and check boxes are among the many tools in the toolbox at your disposal.

§Development area: This is the area used to lay out your forms, to see how they will appear. Grouping controls together, aligning them to the left, right, or top, and sizing the form are among the functions you can perform here.

Now that the major areas have been pointed out and briefly explained, you can start building the small mailing list system mentioned earlier.

Creating a Mailing List Project

The first thing you need to do is to start a new project. Complete the following steps to do so:

1.Either select New Project from the File menu or click the New Project button on the toolbar. The New Project dialog box will appear, as shown

in Figure A-2. Here you are presented with quite a few options as to what kind of project you want to create.

Figure A-2: The New Project dialog box options

2.Select Visual Basic Projects from the available Project Types, if it is not already highlighted. Then, select from the available Templates. For the purposes of this example, you will be creating a new Windows application, so be sure that the Windows Application icon is selected. Ignore all the other options for now, or if you are interested in them click on the help button in this window and read up on these additional features.

3.Name the project "Mailman" or something appropriate, and indicate the correct Location for your project.

4.Click OK to create the new project.

The new window will appear in the development area. With the new window in focus, click its properties tab on the right side of the display and name the new form. You can also set its Window Style to an MDI parent by making the IsMdiContainer equal to True (see Figure A-3).

Figure A-3: Creating a new project

Many types of Windows are available to the developer. VB.NET is based on a few interface styles and GUI presentation needs. MDI (Multiple Document Interface), SDI (Single Document Interface), and Web interface styles are a few of the most common that you will see. This example application will be an MDI application; however, to keep things small and simple, it will only have a few windows.

Tip New to VB.NET is the disappearing sidebar feature. If you have ever seen the Windows taskbar as it is set to autohide, you'll be familiar with this feature as well. It is designed to save screen space and it is a great feature to have enabled. On the top of the toolbox, for example, you'll see a small icon that looks like a stickpin; click this pin to activate the feature. The toolbox will move off to the left and reveal more of the design surface.

Setting the data elements

The mailing list system will have a small menu control, a data entry screen, and a Help About window. It will also have a small database behind it and therefore will consist of the following features:

§Address entry

§Help About window

§Application shutdown and exit

The database will consist of the data elements defined in Table A-1.

Table A-1: Data element items for mailing list database

Field Name

 

Field

 

Data Type

 

Data

 

 

Descripti

 

 

 

Size

 

 

on

 

 

 

 

 

 

 

 

 

 

 

Mail_id

 

Identifying

 

Autonumber

 

Auto

 

 

number

 

 

 

 

 

 

for the

 

 

 

 

 

 

database

 

 

 

 

 

 

(Primary

 

 

 

 

 

 

Key)

 

 

 

 

 

 

 

 

 

 

FName

 

First name

 

Text

 

25

 

 

 

 

 

 

LName

 

Last name

 

Text

 

20

 

 

 

 

 

 

Address

 

Mailing

 

Text

 

25

 

 

address

 

 

 

 

 

 

 

 

 

 

City

 

City name

 

Text

 

15

 

 

 

 

 

 

Postal

 

ZIP or

 

Text

 

9

 

 

postal

 

 

 

 

 

 

code

 

 

 

 

 

 

 

 

 

 

Province

 

Province

 

Text

 

2

 

 

or state

 

 

 

 

 

 

 

 

 

 

Phone

 

Telephone

 

Text

 

8

 

 

number

 

 

 

 

 

 

 

 

 

 

E_mail

 

Internet e-

 

Text

 

25

 

 

mail

 

 

 

 

 

 

address

 

 

 

 

Note

There is a database file located on the companion Web site for

 

this book. A further reference will be made to this file later in the

 

appendix when it will actually be used.

Adding menu controls

The menu designer is the interface that enables you to define the actions or commands available in your application. Commands such as Print, Copy, Edit, Window Tile, and Help About are some of the common ones.

Note The mailing list system project is going to have only a few menu items, so that you can see some of the functionality of VB.NET. Feel free to add more functionality when you are ready to do so.

To add a menu control to the main form, complete the following steps:

1.Find the MainMenu control on the toolbox window. You may have to reveal the toolbox on the left of the display, depending on how you have your IDE configured.

2.Drag the MainMenu control to the main window and drop it anywhere on the form that is being worked on.

3.After the MainMenu control is attached to the form, you can begin to build your menu structure. The menu design structure will appear on the top of the form, and you can add the menu items by typing directly into the menu option slots that are presented to you. This is a marked difference to the menu designer that VB 6.0 developers are used to.

The menu designer has the following main areas, which are accessed in different portions of the IDE:

§ Text: The portion of the menu that the user of the application actually sees while working with it. This is accessed simply by typing the caption in the area on the menu designer; alternately, you can set this value in the properties tab.

§ Name: The "internal" name that is assigned to the menu item. If a menu item is to be referenced in programming code, this is how it will be addressed. Typically, menu names are prefixed with "mnu" as in mnuFilePrint. This is done to visually cue the software programmer as to what type of entity is being referenced in the code. This name item is accessed in the menu's property page on the right side of the IDE.

§ Short Cut: This is the menu shortcut that can be assigned to each item. Take a look at most Microsoft applications and you see that a shortcut for saving information is usually the Ctrl + S keystroke combination. You can have similar shortcuts assigned to your menu items, but be sure to track the ones you use so that you don't have two or more menu items assigned with the same shortcut.

§ Enabled & Visible: These two attributes to a menu item are the most commonly used of the remaining properties. Enabled means that the menu item will trigger its supporting code if selected from a running application. The opposite of this, to have Enabled turned off, would be similar to a grayed-out option on a menu. It is visible, but because of the context that the application is in, it is inoperable. An example would be an Edit menu with Paste grayed out because nothing is in the computer memory's clipboard to paste. The other popular option, Visible, has similar functionality except that it is based on the visual aspect of the menu item. You may have also noticed that certain menu items in applications appear and disappear depending on the part of the program you're working in. This is the Visibility option at work.

4.Now build the menu structure for the application using the information and properties from Table A-2. Keep the following tips in mind when

creating the menus:

§ To define shortcut keys, simply place an ampersand (&) in front of the letter in the menu name that will work in conjunction with the alternate (Alt) key. For example, &File would appear as File and would open the File menu with the Alt + F keystroke combination. This functionality is triggered simply by the ampersand being in the caption field; you do not need to also define it in the shortcut property.

§ To define menu options as options under, or belonging to, a top-level menu item (for example, File Exit), simply use the space underneath the menu option to enter the value. Also, for lateral menu growth (for example, View Toolbars Standard), use the entry point on the right side of an existing menu item.

§ To define a menu separator bar, simply enter a single dash character in the caption field and name the item accordingly.

Note Some items in Table A-2 are not yet enabled, and enabling them doesn't make sense until the correct situation arises. The

programming code that follows in this example will address such a situation.

Table A-2: Menu information and properties table

Menu

 

Name

 

Shortcut

 

Enable

 

Visible

Caption

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

&File

 

mnuFile

 

 

 

(

 

(

 

 

 

 

 

 

 

&Open

 

mnuOpenAddr

 

Alt + o

 

(

 

(

Address

 

 

 

 

 

 

 

 

Form

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

-

 

mnuFileSep1

 

 

 

(

 

(

(Separat

 

 

 

 

 

 

 

 

or)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

E&xit

 

mnuExit

 

Alt + x

 

(

 

(

Program

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

&Edit

 

mnuEdit

 

 

 

(

 

(

 

 

 

 

 

 

 

 

Cut

 

mnuCut

 

Ctrl + x

 

 

 

(

 

 

 

 

 

 

 

 

Copy

 

mnuCopy

 

Ctrl + c

 

 

 

(

 

 

 

 

 

 

 

 

Paste

 

mnuPaste

 

Ctrl + v

 

 

 

(

 

 

 

 

 

 

 

 

&Window

 

mnuWindow

 

 

 

 

 

(

 

 

 

 

 

 

 

 

Tile

 

mnuTileHoriz

 

 

 

 

 

(

Horizont

 

 

 

 

 

 

 

 

ally

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Tile

 

mnuTileVert

 

 

 

 

 

(

Vertically

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Cascade

 

mnuCascade

 

 

 

 

 

(

 

 

 

 

 

 

 

 

&Help

 

mnuHelp

 

 

 

 

 

(

 

 

 

 

 

 

 

 

About

 

mnuAbout

 

 

 

 

 

(

Your completed menu structure should look like the one shown in Figure A-4.

Figure A-4: The completed menu structure in MDI Form

Next you will write the programming code to make these menus active.

Activating the menu controls

The programming editor will look a little different to the one in previous versions of Visual Basic on at least two fronts. First, you will notice that a lot more code is visible to you, which is generated by .NET. Because this product strives to get closer to true object orientation, it defines more class style code and makes that code visible to the developer. This enables you to see what .NET has already generated for you in case you want your code to interact with the generated code. Also, and this is a personal opinion only, the visible code is there to impress you with the technical skills of Microsoft's .NET development team.

The other thing that you may notice as different from previous versions of Visual Basic is that the editor has a cleaner look and feel with some visual improvements. Take a look at the code editor as it is accessed within the menu editor. To do this, simply double-click a menu item. Figure A-5 shows the code editor for the File Exit menu option.

Figure A-5: The code editor for the File Exit option

Enter the following code after the Sub statement and before the End Sub statement. The cursor should be at the correct location, but be sure it is in the right place just the same.

Me.Close()

This simple command tells the menu to close its owner, which happens to be the main application window. This is not a command instructing the menu to be unloaded as is often confused, rather it is telling the application to close. Keep in mind that the form owns any controls, including menus that are placed on it. After you have entered that code, you can close the code editor whenever you're ready. Again, this would be a good chance to look around this particular editor to get a feel for how it works and operates.

Enter the following code for the other listed menu items. Notice that .NET already has some functionality built in for these more common menu options, like the window tiling actions. Simply implement the commands in the right place.

Tip If you are already in the code editor and want to edit code for multiple events (like the code listings that follow), then simply choose the control entity from the drop-down control on the top left of the code editor. Next, select the event of the entity from the dropdown list on the top right of the code editor. The surrounding code will be generated for you, if it has not already been done, and you can continue with writing code.

Insert the following code to activate the Edit Copy command:

Dim tempChild As Form = Me.ActiveMdiChild

If TypeOf tempChild.ActiveControl Is TextBox Then

Clipboard.SetDataObject(CType(tempChild.ActiveControl, TextBox).SelectedText)

End If

Insert the following code to activate the Edit Cut command:

Dim tempChild As Form = Me.ActiveMdiChild

If TypeOf tempChild.ActiveControl Is TextBox Then

Clipboard.SetDataObject(CType(tempChild.ActiveControl, TextBox).SelectedText)

' Clear the contents of the control since it is a cut process.

CType(tempChild.ActiveControl, TextBox).SelectedText = ""

End If

Insert the following code to activate the Edit Paste command:

'Declare an IDataObject named iData to hold the data retrieved from the clipboard.

Dim iData As IDataObject = Clipboard.GetDataObject()

Dim tempChild As Form = Me.ActiveMdiChild

'Determine whether the data is in a format you can use.

If iData.GetDataPresent(DataFormats.Text) Then

'Yes it can be used, so paste it to the active control

tempChild.ActiveControl.Text = CType(iData.GetData(DataFormats.Text), String)

Else

'No it is not.

MsgBox("Data on clipboard is not retrievable.", MsgBoxStyle.Exclamation,

"Clipboard error")

End If

Insert the following code to activate the Window Tile Horizontal command:

Me.LayoutMdi(MdiLayout.TileHorizontal)

Insert the following code to activate the Window Tile Vertically command:

Me.LayoutMdi(MdiLayout.TileVertical)

Insert the following code to activate the Window Cascade command:

Me.LayoutMdi(MdiLayout.Cascade)

Insert the following code to activate the Help About command:

Dim frmAbout As New frmAbout()

'Set the Parent Form of the Child window.

frmAbout.MdiParent = Me

frmAbout.Show()

Insert the following code to activate the File Open Address command:

Dim frmAdd1 As New frmAddress()

' enable menu items now that a window will be opened...

mnuCopy.Enabled = True

mnuPaste.Enabled = True

mnuCut.Enabled = True

mnuTileHoriz.Enabled = True

mnuTileVert.Enabled = True

mnuCascade.Enabled = True

'Set the Parent Form of the Child window.

frmAdd1.MdiParent = Me

frmAdd1.Show()

Notice in this code that there is an assignment of Me to the MdiParent property of the frmAdd1 form. This is done to be sure that the window that will be shown at the end of this event will belong to the entity known as "me"; in this case, the controlling application object. Also, while you are working on the main form window, be sure to set the IsMDIContainer property to True (if not already done), so that the MDI functionality will also be set on the parent window of the application.

Tip While you are looking at the properties of the main form window, you may also want to set the WindowState property to Maximized, so that when you test-run your application, it will take up your whole computer screen, for a better presentation of what you are designing.

Designing the Help About window

Next, design the Help About window that is referenced in the code, naming the form frmAbout when you are finished. To create a new form, complete the following steps:

1.Right -click the application name in the Solution Explorer and choose Add Add Windows Form.

2.Select the default Windows Form template and enter the form Name in the bottom half of the form definition window, as shown in Figure A-6.

Figure A-6: The new form definition window

Tip Be sure to leave the .vb extension on the forms that you create. This is a "feature" of VB.NET that helps the IDE and compiler to know what parts of the solution are for a particular purpose. If you change a form's name, the form may become useless to you, because .NET will not necessarily know what to do with it.

3.Click the OK button, and the new design surface of the window will be shown as having focus in the design area of the IDE, and you can begin to construct the window as you like. The Help About window can be designed as shown in Figure A-7, or you can suit it to your own taste. Figure A-7 has two Label controls, a Picture control, and a PushButton control. The code behind the push button is as follows:

Figure A-7: The completed Help About window design

Me.Close ()

4.Change the Text property of the push button to OK, or something

similar, to prompt your application users to eventually close this Help About window.

Be sure to save your application often for peace of mind. Now that you have all of the menu code written, you can test your Help About window. Click the Run icon (little blue triangle with point facing to the right icon on the toolbar), and Visual Basic .NET will perform a build and then try to run your application. The example shown in Figure A-8 has the Help About window showing and an active menu pulled down.

Figure A-8: Running Mailman application showing Help About window open Note You will have a build warning message that tells you that

frmAddress does not exist, with the following message: "type not defined: frmAddress." This message appears simply because frmAddress has not been defined in the application yet, but you are making reference to it in the menu code. You have two ways around this situation. First, you can resolve it by actually defining a form with that name. Even though it is simply a blank form, it will at least exist. Second, you can comment out the code in the menu editor that makes reference to this form until you define it at a later time.

Looking at MDI applications

As mentioned previously, there are ways in which your application's forms will "own" other windows that are opened within the program. For example, the name of the window "Mailman.NET About Window" in Figure A-8 appears to be listed on the Window menu as a "switch to"-type command, thus showing that the application has control and actually owns the form. If additional windows were open, they too would be listed on the Window menu.

Make sure as you define new forms in the Mailman application that you set the form's isMDIContainer property to False. This should be the default anyway, but just make sure that it is set as such. Then, every time that this form is opened within the application (through the previously written menu code), it will be "owned" by the application and contained within its visual boundaries. The isMDIContainer property enables the application to know how to add the window names to the available list under the Window menu, and it also allows the application to be able to employ the tile and cascade features that you have coded a little earlier.

Соседние файлы в папке c#