|
Create
a Generic Error Handler for your Application
PRODUCT :Microsoft Visual Basic for Windows
PROD/VER:
OPER/SYS:WINDOWS
KEYWORDS:
======================================================================
---------------------------------------------------------------------
The information in this article applies to:
- Microsoft Visual Basic Learning, Professional, and Enterprise Editionsfor
Windows, versions 5.0, 6.0
- Microsoft Visual Basic Standard, Professional, and Enterprise Editions,
16-bit and 32-bit, for Windows, version 4.0
---------------------------------------------------------------------
SUMMARY
=======
The Visual Basic ON ERROR statement is designed to encapsulate error handling
for each procedure or module in an application. This behavior is by design,
and is intended to conform with object oriented programming (OOP) conventions.
When you create an application, you may wish to have a generic error handler
that traps all errors for the application, and handleexceptions to this
routine as they occur in various modules.
The ON ERROR statement does not allow the use of an application wide ON
ERROR routine. However, it is possible to create a generic routine that
iscalled from within each procedure or method in the application.
MORE INFORMATION
================
If the error routine is placed in a global module, it can be called manually
from each procedure or method in your application. The following code
example demonstrates how to set up a global error handling routine,and
illustrates how this can be combined with specific error handling in aprocedure
or method.
This example creates a project that contains two forms and one code module.Each
form contains command buttons that cause errors to occur. Some errors
are handled within the click method for the command button. Other errors
are passed to the generic error handler.
Steps to Reproduce Behavior
---------------------------
1. Create a new standard EXE project in Visual Basic. Form1 is created
by default.
2. Add the following code to the Load method of Form1. This code causes
two errors to occur when the form is loaded. The first error will be handled
by the form load method. The second error will be passed to the
generic error handler:
Private Sub Form_Load()
On Error GoTo FormLoadErr
Err.Raise 76
Err.Raise 70
Exit Sub
FormLoadErr:
Select Case Err.Number 'Evaluate Error Number
Case 76
MsgBox "Form_Load Error Handler. Form Does Not Exist"
Case Else
AppWideErr (Err.Number) 'Pass Error to generic module
End Select
End Sub
3. Add a CommandButton to the form. This code causes two errors to occur.
The first error will be handled by the click method. The second error
will be passed to the generic routine. Change the Caption property to
"Cause Error 53 and 70." Add the following code to the Click
method of the CommandButton:
Private Sub Command1_Click()
On Error GoTo Cmd1Err
Err.Raise 53 'Handled locally
Err.Raise 70 'Handled by generic module
Exit Sub
Cmd1Err:
Select Case Err.Number
Case 53
MsgBox "Command 1 Error Handler"
Case Else
AppWideErr (Err.Number)
End Select
Resume Next 'Process the next (70) error
End Sub
4. Add a second CommandButton to the form. If an error occurred in this
method, no error handler would be called. Change the Caption property
to "Show Form 2." Add the following code to the Click event:
Private Sub Command2_Click()
'No error handling is coded in this method
'AppWideErr would not be called
Form2.Show 'Use VB's default error handling only
End Sub
5. Add a second form to the project. Add a CommandButton to the form.This
CommandButton passes all errors to the generic error handler and does
not perform any special processing for specific errors. Change the Caption
property of the button to "Cause Error 17." Add code to the
Click method as follows:
Private Sub Command1_Click()
On Error GoTo ThisSubErr
Err.Raise 17
Exit Sub
ThisSubErr:
AppWideErr (Err.Number)
End Sub
6. Add a module to the project. Add the following code to the module:
Public Sub AppWideErr(lnErrNumber)
Select Case lnErrNumber 'Evaluate error passed to routine
Case 70
'The following two lines of code should be typed on one line.
'Added title to MsgBox to make it clearer where error message
'came from.
MsgBox "Generic Routine. Access Denied. See Net Administrator."
, , "AppWideErr"
Exit Sub
Case Else
'The following two lines of code should be typed on one line.
'Added title to MsgBox to make it clearer where error message
'came from.
MsgBox "Generic Routine. Unhandled Error: " + Err.Description
+
" # " & lnErrNumber , , "AppWideErr"
Exit Sub
End Select
End Sub
7. Save and run the project. Note that some errors will be handled by
the
error routine specific to a method. The logic for each method invokes
the generic AppWideErr procedure for any errors not explicitly handled
by the method. The error number is passed as a numeric argument to
AppWideErr.
返回
Force Application
Setup to Use the Default Directory
PRODUCT :Microsoft Visual Basic for Windows
PROD/VER:WINDOWS:4.0,5.0,6.0
OPER/SYS:WINDOWS
KEYWORDS:
======================================================================
---------------------------------------------------------------------
The information in this article applies to:
- Microsoft Visual Basic Professional and Enterprise Editions for
Windows, versions 4.0, 5.0, 6.0
---------------------------------------------------------------------
SUMMARY
=======
Although it is not recommended, the Visual Basic Setup Toolkit provides
a
means to disable the user interface for changing the default installation
directory of your application's setup routine. This forces the installation
process to use the default directory as the destination for the
application's files.
MORE INFORMATION
================
It is recommended that you allow the user to choose the destination
directory for Windows applications setup processes. However, a Visual
Basic
application that has a setup program created by the Visual Basic Setup
Wizard on Visual Basic versions 4.0 and 5.0 or the Package and Deployment
Wizard (PDW) on Visual Basic 6.0 does provide an option that allows the
programmer to force the user to use a specific destination directory.
The Setup.lst file is a standard Windows initialization file. It informs
the Setup.exe and Setup1.exe programs of the distribution set how to
perform setup. To force a user to use a specific destination directory,
you
must provide the default directory name and add the keyword
"ForceUseDefDir" to the Setup.lst file.
Step-by-Step Example
--------------------
1. Run the Application Setup Wizard or PDW to create a distribution set
for
your application.
2. Open the Setup.lst file (found on disk #1 of the distribution set)
in a
text editor, such as Notepad.
3. Specify the default installation directory by modifying the "DefaultDir"
key in the "Setup" section of the Setup.lst file. For example:
DefaultDir=C:\MyProgramDir
4. Insert the following line immediately after the "DefaultDir"
line:
ForceUseDefDir=1
5. Save the Setup.lst file. Note that the Setup Wizard makes maximum usage
of each disk. If you cannot save the file due to insufficient space on
the diskette, delete one or more of the
"; XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
lines
at the bottom of the Setup.lst file.
返回
Use a Satellite
DLL to Localize an Add-In
PRODUCT :Microsoft Visual Basic for Windows
PROD/VER:WINDOWS:6.0
OPER/SYS:WINDOWS
KEYWORDS:
======================================================================
---------------------------------------------------------------------
The information in this article applies to:
- Microsoft Visual Basic Professional and Enterprise Editions for
Windows, version 6.0
---------------------------------------------------------------------
SUMMARY
=======
When developing an Add-In, it is possible to localize its Display Name
and
Description using a Satellite DLL. This article describes how to accomplish
this task.
MORE INFORMATION
================
Microsoft Visual Basic features a Connect designer for Add-Ins. Under
the
"Advanced" tab of this designer, there is an option to specify
a Satellite
DLL. The Satellite DLL will contain resource information for localizing
an
Add-In.
Create a Satellite DLL
----------------------
The sample below demonstrates how to create a Satellite DLL containing
string resources necessary for localizing the Display Name and Description
of an Add-In. It requires the Resource Editor to create the string
resources. If the Resource Editor is not already installed within the
Visual Basic IDE, select Add-In Manager under the Add-Ins menu and
double-click on "VB 6 Resource Editor" so that the Load Behavior
is Loaded.
1. Create a new ActiveX DLL project in Microsoft Visual Basic.
2. From the Tools menu, select Resource Editor.
3. In the Visual Basic Resource Editor dialog box, click the Edit String
Tables button on the toolbar.
4. In the Edit String Tables dialog box, type "This is My Add-In"
(without
the quotes) for resource Id 101.
5. Click the Insert New Row button on the Toolbar to create a new resource
item 102, then type "This is a description of My Add-In" (without
the
quotes) for resource Id 102.
6. Close the Edit String Tables dialog box.
7. In the Visual Basic Resource Editor dialog box, right-click the Resource
File entry and click Save. Save the file as SatTest.res.
8. Close the Visual Basic Resource Editor.
9. On the Project menu, click Project1 Properties, and then click the
General tab.
10. In the Project Name box, enter SatTest and click OK.
11. On the File menu, click Make SatTest.DLL.
12. On the File menu, click Save Project to save the SatTest.DLL project.
Using the Satellite Resource DLL with an Add-In
-----------------------------------------------
In the satellite DLL (SatTest.DLL) created above, two string resources
were
created :
- String resource #101, "This is My Add-In"
- String resource #102, "This is a description of My Add-In"
The sample below shows how to bind SatTest.DLL to an Add-In, and use the
string resources to determine the Display Name and Description of the
Add-In.
1. On the File menu, click New and create a new Add-In project.
2. In the Project Explorer, expand the Designer folder and double-click
the Connect designer.
3. In the Connect Designer dialog box, enter #101 in the Add-In Display
Name box and enter #102 in the Add-In Description box.
NOTE: These values correspond to the resource IDs of the strings
contained within SatTest.DLL.
4. Click the Advanced tab, and enter SatTest.DLL in the Satellite DLL
Name
box. This field binds the Add-In to the satellite DLL and will look in
it for any requested resources.
5. Close the Connect Designer dialog box.
6. On the File menu, click Make MyAddin.dll.
7. On the File menu, click Save Project to save the MyAddin.dll project.
NOTE : Make sure the Satellite DLL (SatTest.DLL) and the compiled Add-In
(MyAddin.DLL) reside within the same directory.
Test the Add-In
---------------
1. On the File menu, click New and create a Standard EXE project.
2. On the Add-Ins menu, click Add-In Manager.
3. In the Add-In Manager Dialog Box, "This is My Add-In" is
listed under
"Available Add-Ins."
4. Click on "This is My Add-In." The Description field displays
"This is a
description of My Add-In."
返回
快速建立程序模板
有时候你需要一个窗口模板以遍在以后可以利用这个模板快速的建立程序,其实十分简单,只要按照你的需要建立一个工程,然后将该工程保存到VB目录的
Template\Project 目录下,然后点击VB菜单的 File|New Project项,看看在New Project对话框中是否多了一个选项?
返回
自动更动成中文输入
如果使用的VB5.0,则查看有没有一个属性是IMEMode,如果有就设定为1 代表开启,那 便可以每次进入这个Control项时就切换成中文输入。如果没有,那只好自己做。
'以下在.Bas Public Declare Function GetKeyboardLayout Lib "user32"
(ByVal dwLayout As Long) As Long Public Declare Function ImmIsIME Lib
"imm32.dll" (ByVal hkl As Long) As Long Public Declare Function
ImmSimulateHotKey Lib "imm32.dll" (ByVal hwnd As Long, ByVal
dw As Long) As Long Const IME_THOTKEY_IME_NONIME_TOGGLE = &H70 PuBlic
Sub Chg2Chinese(ByVal hwnd As Long) '传入Control项或Form 的hwnd Dim hkb As
Long hkb = GetKeyboardLayout(0) '取得目前Thread的Keyboard Layout If ImmIsIME(hkb)
= 0 Then '代表不是中文输入 ImmSimulateHotKey hwnd, IME_THOTKEY_IME_NONIME_TOGGLE
'模拟按Strl-Space End If End Sub PuBlic Sub Chg2English(ByVal hwnd As Long)
'传入Control项或Form 的hwnd Dim hkb As Long hkb = GetKeyboardLayout(0) '取得目前Thread的Keyboard
Layout If ImmIsIME(hkb) = 1 Then '代表是中文输入 ImmSimulateHotKey hwnd, IME_THOTKEY_IME_NONIME_TOGGLE
'模拟按Strl-Space End If End Sub '以下在Form Private Sub Form_Load() Call Chg2Chinese(Me.hwnd)
End Sub
更改出现输入法的顺序
loadKeyboardLayout()可以改变order,只要第一个叁数传
您要使之变成Top的KeyboardlayoutName,第二个叁数传KLF_REORDER
便可,例如底下的例子便是
aa = LoadKeyboardLayout("e0060404", KLF_REORDER) '使大易 变top
aa = LoadKeyboardLayout("00000409", KLF_REORDER) '使英文变top
aa = LoadKeyboardLayout("e0010404", KLF_REORDER) '使注音变top
如此,使顺序变成
注音
英文
大易
而如何得知每个输入法的keyboardlayName呢? 则使用
GetKeyboardLayoutname()它会传回Current KeyBoardLayoutName
配合GetKeyboardLayoutList 取得所有install的KeyBoard Handle
再用activateKeyboardlayout()来设定目前的keyboardLayout如下:
Private Declare Function GetKeyboardLayoutList Lib "user32" Alias _
"GetKeyboardLayoutList" (ByVal nBuff As Long, lpList As Long) As Long
Private Declare Function GetKeyboardLayoutName Lib "user32" Alias _
"GetKeyboardLayoutNameA" (ByVal pwszKLID As String) As Long
Private Declare Function ActivateKeyboardLayout Lib "user32" Alias _
"ActivateKeyboardLayout" (ByVal HKL As Long, ByVal flags As Long) As Long
Private Declare Function LoadKeyboardLayout Lib "user32" Alias "LoadKeyboardLayoutA" _
(ByVal pwszKLID As String, ByVal flags As Long) As Long
Const KLF_REORDER = &H8
Private Form_Load()
Dim hkb5(24) as Long, i as Long
Dim kln As String
Dim BuffLen As Long
Dim Buff As String
Dim RetStr As String
Dim RetCount As Long
Buff = String(255, 0)
BuffLen = 255
kln = String(8, 0)
LayOutNO = GetKeyboardLayoutList(25, hkb5(0))
For i = 0 To LayOutNO - 1
ActivateKeyboardLayout hkb5(i), 0
res = GetKeyboardLayoutName(kln)
RetCount = ImmGetDescription(hKB5(i - 1), Buff, BuffLen)
RetStr = Left(Buff, RetCount)
Debug.Print RetStr, kln '列印各种输入法,及其输入法名称代号
Next i
End Sub
Back to top
|