Search In Site

27 June, 2013

Open And Save As Dialog boxes in Vb

Using The Open And Save As Dialogs
Probably the most common use of the Common Dialog control is to display File Open and File Save As dialog boxes, and you display those dialog boxes with the Common Dialog control's ShowOpen and ShowSave methods. These methods need no arguments passed to them to set various options, you set the Common Dialog control's Flags property (see the next topic), such as overwriting existing files and so on.
You can also set the Filter property so the dialog box displays only certain types of files, such as text files. See Setting File Types (Filters) In Open, Save As Dialogs, a little later in this chapter. To find out what file the user wants to work with, you check the Common Dialog's FileName property after the user clicks on OK in the dialog box. That property holds the fully qualified (that is, with path) name of the file to open. If you just want the file's name, use the FileTitle property. Let's see an example. In this case, we'll let the user select a file to open, and then display the file's name and path in a message box. Start by adding a Common Dialog control to a form, then set the control's CancelError property to True so we can check if the user clicked Cancel. To check that, we use On Error GoTo: 

Private Sub Command1_Click()
On Error GoTo Cancel
...
Cancel:
End Sub

Then we display the Open dialog box:

Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.ShowOpen
...
Cancel:
End Sub

Finally, assuming the user clicked on OK, we can display the name of the file they selected in a message box using the FileName property:

Private Sub Command1_Click()
On Error GoTo Cancel
CommonDialog1.ShowOpen
MsgBox "File to open: " & CommonDialog1.FileName
Cancel:
End Sub

When you run this code and click the button, the Open dialog box appears.
If you make a file selection and click on OK, the Open dialog box closes and the program displays the name of the file you selected, along with its path, in a message box. Our program is a success; the code for this program is located in the opendialog folder on this books accompanying CD-ROM.
Setting Open And Save As Flags
You can set a wide variety of options when you display File Open and File Save As dialog boxes by setting the Common Dialog control's Flags property. Here are the possible settings:
" cdlOFNAllowMultiselect_&H200; specifies that the File Name list box allows multiple selections.
" cdlOFNCreatePrompt_&H2000; the user can select more than one file at runtime by pressing the Shift key and using the up arrow and down arrow keys to select the desired files. When this is done, the FileName property returns a string containing the names of all selected files. The names in the string are delimited by spaces.
" cdlOFNCreatePrompt_&H2000; specifies that the dialog box prompts the user to create a file that doesn't currently exist. This flag automatically sets the
cdlOFNPathMustExist and cdlOFNFileMustExist flags.
" cdlOFNExplorer_&H80000; displays the Explorer-like Open A File dialog box template. Works with Windows 95 and Windows NT 4.
" cdlOFNExtensionDifferent_&H400; indicates that the extension of the returned file name is different from the extension specified by the DefaultExt property. This flag isn_t set if the DefaultExt property is Null, if the extensions match, or if the file has no extension. This flag value can be checked upon closing the dialog box. This can be useful if you want to track the kind of file the user wants to open.
" cdlOFNFileMustExist_&H1000; specifies that the user can enter only names of existing files in the File Name text box. If this flag is set and the user enters an invalid file name, a warning is displayed. This flag automatically sets the cdlOFNPathMustExist flag.
" cdlOFNHelpButton_&H10; causes the dialog box to display the Help button.
" cdlOFNHideReadOnly_&H4; hides the Read Only checkbox.
" cdlOFNLongNames_&H200000; enables the use of long file names.
" cdlOFNNoChangeDir_&H8; forces the dialog box to set the current directory to what it was when the dialog box was opened.
" cdlOFNNoDereferenceLinks_&H100000; disables the use of shell links (also known as shortcuts). By default, choosing a shell link causes it to be interpreted by the shell.
" cdlOFNNoLongNames_&H40000; disables long file names.
" cdlOFNNoReadOnlyReturn_&H8000; specifies that the returned file won't have the Read Only attribute set and won't be in a write-protected directory.
" cdlOFNNoValidate_&H100; specifies that the Common Dialog allows invalid characters in the returned file name.
" cdlOFNOverwritePrompt_&H2; causes the Save As dialog box to generate a message box if the selected file already exists. The user must confirm whether to overwrite the file.
" cdlOFNPathMustExist_&H800; specifies that the user can enter only valid paths. If this flag is set and the user enters an invalid path, a warning message is displayed.
" cdlOFNReadOnly_&H1; causes the Read Only checkbox to be initially checked when the dialog box is created. This flag also indicates the state of the Read Only checkbox when the dialog box is closed.
" cdlOFNShareAware_&H4000; specifies that sharing violation errors will be ignored.
You can set more than one flag for a dialog box using the Or operator. For example:
CommonDialog1.Flags = &H10& Or &H200&
(Although this shows what we're doing numerically, it's usually better to use constants to make your code more readable.) Adding the desired constant values produces the same result.

Window Common Dialog Boxes In Visual Basic

Creating And Displaying A windows Common Dialog
 Your program, SuperDuperTextPro, is great,but why is the File Save As dialog box the size of a postage stamp? And why is it colored purple? Shouldn't it match the uniform kind of dialog box that other Windows programs use?
To make your dialog boxes look just like the dialog boxes other programs use (and add professionalism to your program), you can use the Windows Common Dialogs, which are wrapped up in the Windows Common Dialog control. The Common Dialog control can display five different dialog boxes.Open A File, Save A File, Set A Color, Set A Font, and Print A Document, and you can also display Windows Help.
Adding a Windows Common Dialog control to your program is easy: just follow these steps:

1. Select the Project|Components menu item.
2. Select the Controls tab in the Components box that opens.
3. Select the entry labeled Microsoft Common Dialog Control, then click on OK to close the Components box.
4. Add a Common Dialog control to a form in the usual way_just double-click the Common Dialog tool in the toolbox, or select it and paint the control on the form.
5. Add the code you want to open the dialog box and make use of values the user sets.
 

To display various dialog boxes, you use these Common Dialog methods (for example, commonDialog1.ShowColor):
" ShowOpen_Show Open dialog box
" ShowSave_Show Save As dialog box
" ShowColor_Show Color dialog box
" ShowFont_Show Font dialog box
" ShowPrinter_Show Print or Print Options dialog box
" ShowHelp_Invokes the Windows Help engine

You can also set the Common Dialog_s Action property to do the same thing (and in fact, that_s the way you used to display Common Dialogs until recent Visual Basic releases). Microsoft says that using the preceding methods adds functionality, but in fact, the two ways of displaying dialog boxes are equivalent at this writing (although using methods like ShowHelp instead of Action = 6 makes code a little clearer). Here
are the values you can place in the Action property:
" 0_No action
" 1_Displays the Open dialog box
" 2_Displays the Save As dialog box
" 3_Displays the Color dialog box
" 4_Displays the Font dialog box
" 5_Displays the Print dialog box
" 6_Runs winhelp32.exe

Now that you've added a Common Dialog control to your program, refer to the individual topics in this chapter for the dialog box you want to work with to see how to retrieve values from the user. TIP: Before displaying the Font and Help dialog boxes, you need to set the Common Dialogs control's Flags property or nothing will appear.

Setting A Common Dialog's Title
The Aesthetic Design Department is calling again: can_t you change the text in the title bar of those dialog boxes? How about changing the title of the Open dialog box from Open to Select A File To Open?
Although some programmers may question the wisdom of changing a Common Dialog's title, you can do it using the DialogTitle property. As an example, here we're changing the title of an Open dialog box to Select a file to open.

Private Sub Command1_Click()
CommonDialog1.DialogTitle = "Select a file to open"
CommonDialog1.ShowOpen
End Sub


WARNING! Note that this property, DialogTitle, does not work for the Color, Font, and Print dialog boxes.

Using Fonts And Bullets in rich text boxes in Visual Basic

Setting Fonts And Font Sizes In Rich Text Boxes
Another call from the Field Testing Department. It seems that the users want to use different fonts in your word-processor program. Well, some people are never satisfied but rich text boxes can help here, too. 
To set a selection's font, you just set the SelFontName to the new font name (for example, Arial or Times New Roman). To set a selections font size, you just set the SelFontSize property. That's all it takes.Here's an example. In this case, we'll display the text This rich text box supports fonts like Arial and Courier in different sizes. In a rich text box, and format the words Arial  and Courier in those fonts, and in different font sizes. We start by placing that text in a rich text box:
Private Sub Command1_Click()
RichTextBox1.Text = "This rich text box supports fonts like Arial and Courier in different sizes."
...

Next, we select the word Arial:
Private Sub Command1_Click()
RichTextBox1.Text = "This rich text box supports fonts like Arial and Courier in different sizes."
RichTextBox1.SelStart = RichTextBox1.Find("Arial")
RichTextBox1.Span ("Arial")
...

Then we display that word in Arial font, with a 24-point size:

Private Sub Command1_Click()
RichTextBox1.Text = "This rich text box supports fonts like Arial and Courier in different sizes."
RichTextBox1.SelStart = RichTextBox1.Find("Arial")
RichTextBox1.Span ("Arial")
RichTextBox1.SelFontName = "Arial"
RichTextBox1.SelFontSize = 24
...

We do the same for the word Courier, displaying it in 18-point size:

Private Sub Command1_Click()
RichTextBox1.Text = "This rich text box supports fonts like Arial and Courier in different sizes."
RichTextBox1.SelStart = RichTextBox1.Find("Arial")
RichTextBox1.Span ("Arial")
RichTextBox1.SelFontName = "Arial"
RichTextBox1.SelFontSize = 24
RichTextBox1.SelStart = 0
RichTextBox1.SelStart = RichTextBox1.Find("Courier")
RichTextBox1.Span ("Courier")
RichTextBox1.SelFontName = "Courier"
RichTextBox1.SelFontSize = 18
End Sub

Using Bullets In Rich Text Boxes
Rich text boxes support bullets, those black dots that appear in lists of items that you want to set off in text. Putting a bullet in front of each item gives the list a snappy appearance and helps the reader assimilate the information quickly.  To set bullets, you use the SelBullet and BulletIndent properties. The SelBullet property displays a bullet in front of the paragraph in which the current selection is; the BulletIndent property indicates how much you
want the bullet to be indented from the left. 

Private Sub Command1_Click()
RichTextBox1.Text = "This rich text box shows how to use bullets and indent bulleted text."
...
We set the indentation for this paragraph to 200 twips:
Private Sub Command1_Click()
RichTextBox1.Text = "This rich text box shows how to use bullets and indent bulleted text."
RichTextBox1.SelIndent = 200
...

Next, we set the bullet_s indent to 90 twips, so it_s set off from the rest of the text. We set that indent with the BulletIndent property:
Private Sub Command1_Click()
RichTextBox1.Text = "This rich text box shows how to use bullets and indent bulleted text."
RichTextBox1.SelIndent = 200
RichTextBox1.BulletIndent = 90
...

Finally, we add the bullet with the SelBullet property:

Private Sub Command1_Click()
RichTextBox1.Text = "This rich text box shows how to use bullets and indent bulleted text."
RichTextBox1.SelIndent = 200
RichTextBox1.BulletIndent = 90
RichTextBox1.SelBullet = True
End Sub

25 June, 2013

Handling MDI Form And MDI Child Menus

Handling MDI Form And MDI Child Menus
You’ve created your new program, the super Wizard Text Editor, and made it an MDI program. But now there’s a call from the Testing Department users are getting confused. Why is the Edit menu still visible when no documents are open to edit? Can you fix this? Yes you can. Visual Basic lets you specify two menus in an MDI program, one for the MDI form and one for the MDI child form (and more if you have several types of MDI child forms). If the MDI form has a menu and the MDI child form has no menu, the MDI form’s menu is active at all times. If, on the other hand, the MDI child form has a menu, that menu takes over the MDI form’s menu system any time one or more of those child forms is open.

What this means in practice is that you give the MDI form a rudimentary menu system (typically just File and Help menus) and save the full menu system (like File, Edit, View, Insert, Format, Tools, Window, Help, and so on) for the child windows to ensure the full menu system is on display only when documents are open and those menus apply.

For example, you might add just this simple menu system to the MDI form in an MDI program.

Note that you should, at a minimum, give the user some way to open a new or existing document, and you should provide access to Help:

File
....New
....Open
Help
....Contents


Here’s an example of a full menu system you might then give to the MDI child form, which will take over the main MDI form’s menu system when a child form is open:

File
....New
....Open
....Save
....Save As
Edit
....Cut
....Copy
....Paste
Tools
....Graphics Editor
....Charts Editor
....Exporter
Help
....Contents

becomes active again it’s only when MDI child forms are open that their menus take over the main menu system.

 
Twitter Bird Gadget