Handouts

advertisement

DCO10104 User-centered Design and Testing/ Lab 5/P.1

DCO10104 User-centered Design and Testing

Laboratory 5 Menu Creation and Textbox Methods

Overview:

This tutorial will teach you some more controls such as menu, textbox and multipledocument-interface (MDI) forms. Before you start the exercises, you are required to download the working files from BlackBoard. It is highly recommended that you work with one of your classmates (learning partner), so that you cannot only learn from the note, but also from your peer.

References:

MSDN on MDI ; textbox ; and menu

Practical Exercises:

1. Menu Structure

Most Windows programs contain common menu commands. You should follow this pattern as closely as you can. Group your file-related commands on the menu bar's

File option so your users will feel right at home with your application. Your application will require some menu options that no other application uses, and your application certainly may not be as complete as VB's, but use as much overlap as you can so your users can adapt as quickly as possible to your application's interface.

1.

Open TextEdit project. In this project, you will find that it is partially completed.

2.

Set the form’s WindowState property to Maximised and the IsMDIContainer property to true.

3.

From the Toolbox, drag a MainMenu control to the form. You are asked to create a menu as follow. [Note: Type – (a hyphen) in the Text property if you want to create a line separator.]

Menuitem

File

New

Close

Exit

Edit

Cut

Copy

Name mnuFile mnuFile_New mnuFile_Close mnuFile_Exit mnuEdit mnuEdit_Cut mnuEdit_Copy

Shortcut

CtrlN

CtrlX

CtrlC

Text

&File

New

&Close

E&xit

&Edit

Cut

Copy

Paste

Find mnuEdit_Paste mnuEdit_Find

CtrlY

CtrlF

Paste

Find

Find next mnuEdit_FindNext F3 Find next

Window mnuWindow &Window

4.

Select the menu item that corresponds to the &Window menu item and set the

MdiList property to true. This will enable the Window menu to maintain a list of open MDI child windows with a check mark next to the active child window.

DCO10104 User-centered Design and Testing/ Lab 5/P.2

5.

In Solution Explorer, right-click the project, point to Add, and then select Add

New Item.

6.

In the Add New Item dialog box, select Windows Forms from the Templates pane.

Click the Open button to add the form to the project.

7.

Add a textbox control on the screen and rename it as txtEdit. In the Properties window, empty the content and then set the Multiline property to True and the

Dock property to Fill (Click the square in the middle).

8.

Switch to the design of the parent form – FrmEdit. Double-click the New option from the menu and type the following codes under the Click event..

Private Sub mnuFile_New_Click…

Dim NewMDIChild As New Form1

'Set the Parent Form of the Child window.

NewMDIChild.MdiParent = Me

'Display the new form.

NewMDIChild.Show()

End Sub

9.

Save and press F5 to run the program. Terminate the application by clicking the

Close button.

10.

You will now coding the item options from the menu. First we code the Close option and Exit option. Close option is used to close the active form while Exit option will terminate the whole application.

11.

Double-click the Exit option under the File menu in order to open its click event.

Private Sub mnuFile_Exit_Click…

Dim Answer As MsgBoxResult

Answer = MsgBox("Do you want to quit now?",

MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2)

If Answer = MsgBoxResult.Yes Then

End

End If

End Sub

12.

Double-click the Close option and type Me.ActiveMdiChild.Close() under the

Click event..

13.

You will learn Clipboard object in order to code the copy, cut and paste options.

14.

Select the Edit menu and then double-click Copy option in order to open its click event. Type the following codes under the mnuEdit_Copy’s Click event.

Private Sub mnuEdit_Copy_Click…

' Determine the active child form

Dim activeChild As Form = Me.ActiveMdiChild

'Find the active control, which in this example should be a TextBox

Dim theBox As TextBox = CType(Me.ActiveMdiChild.ActiveControl,

TextBox)

If theBox.SelectionLength > 0 Then

theBox.Copy() ' Copy the selected text to the Clipboard.

End If

End Sub

DCO10104 User-centered Design and Testing/ Lab 5/P.3

15.

Select the Edit menu and then double-click Cut option in order to open its click event. Type the following codes under the mnuEdit_Cut’s Click event.

Private Sub mnuEdit_Cut_Click…

' Determine the active child form

Dim activeChild As Form = Me.ActiveMdiChild

'Find the active control, which in this example should be a

TextBox

Dim theBox As TextBox =

CType(Me.ActiveMdiChild.ActiveControl, TextBox)

If theBox.SelectionLength > 0 Then

theBox.Cut() ' Cut the selected text to the Clipboard.

End If

End Sub

16.

Select the Edit menu and then double-click Paste option in order to open its click event. Type the following codes under the mnuEdit_Paste’s Click event.

Private Sub mnuEdit_Paste_Click()…

' Determine the active child form

Dim activeChild As Form = Me.ActiveMdiChild

'Find the active control, which in this example should be a

TextBox

Dim theBox As TextBox =

CType(Me.ActiveMdiChild.ActiveControl, TextBox)

' Paste current text in Clipboard into text box.

theBox.Paste()

End Sub

17.

Save and press F5 to test the options. Terminate the application by clicking the

Close button.

18.

Now we are going to code the Find and Find Next options. You will use Instr function which is used to determine if the string appears in another string. The syntax is instr(start, string1, string2 [, compare]).

19.

Select the Edit menu and then double-click Find option in order to open its click event. Type the following codes under the mnuEdit_Find’s Click event.

Private Sub mnuEdit_Find_Click()…

Dim intRetVal As Integer

Dim intFoundPos As Integer

strSearchFor = InputBox("Find what?", "Find") 'prompt user for text

Dim theBox As TextBox = CType(Me.ActiveMdiChild.ActiveControl, TextBox)

intFoundPos = InStr(1, theBox.Text, strSearchFor, 1) 'search for text

If intFoundPos = 0 Then 'if text was not found

theBox.SelectionStart = 0

theBox.SelectionLength = 0

intRetVal = MsgBox("The search string was not found.",

MsgBoxStyle.Information, "Find")

Else 'if text was found

theBox.SelectionStart = intFoundPos - 1 'highlight text

DCO10104 User-centered Design and Testing/ Lab 5/P.4

theBox.SelectionLength = Len(strSearchFor)

End If

End Sub

20.

Declare a form-level variable (strSearch) as it will be used by Find and FindNext events. At the General Declaration section of the frmEdit, type Dim strSearchFor

As String .

21.

Select the Edit menu and then double-click Find Next option in order to open its click event. Type the following codes under the mnuEdit_FindNext’s Click event.

Private Sub mnuEdit_FindNext_Click()

Dim intRetVal, intFoundPos, intBegSearch As Integer

Dim theBox As TextBox = CType(Me.ActiveMdiChild.ActiveControl,

TextBox)

intBegSearch = theBox.SelectionStart + 2

intFoundPos = InStr(intBegSearch, theBox.Text, strSearchFor, 1)

If intFoundPos = 0 Then 'if text was not found

intRetVal = MsgBox("The search has been completed.",

MsgBoxStyle.Information, "Find")

Else 'if text was found

theBox.SelectionStart = intFoundPos - 1 'highlight text

theBox.SelectionLength = Len(strSearchFor)

End If

End Sub

22.

Save and press F5 to test the entire program.

2. A taste of controls

If you have time left, I want you to watch two movies about Dialog boxes. Please go to the VB@movie ( http://msdn.microsoft.com/vbasic/atthemovies ) , click the following controls under the How-to section. a.

Folder Browser b.

Print Preview

Apart from these two dialog boxes, there are many different dialog boxes you can use, such as FontDialog, ColorDialog, and OpenFileDialog. You can refer to MSDN for more details.

What have you learnt today?

-End-

Download