首页->技巧->控件->详细内容

Set ActiveX Procedure and User Interface Defaults

PRODUCT :Microsoft Visual Basic for Windows
PROD/VER:WINDOWS:5.0,6.0
OPER/SYS:WINDOWS
KEYWORDS:

======================================================================
---------------------------------------------------------------------
The information in this article applies to:

- Microsoft Visual Basic Learning, Professional, and Enterprise Editions
for Windows, versions 5.0, 6.0
---------------------------------------------------------------------

SUMMARY
=======

The purpose of this article is to summarize and clarify the information
presented in the Visual Basic documentation regarding the use of the
Procedure Attributes dialog. Specifically, it clarifies the expected
behavior of the User Interface Default as it applies to the Properties
window, and explains the difference between the Default Procedure ID and
the User Interface Default settings.

MORE INFORMATION
================

The Visual Basic Help states that the User Interface Default (UI Default)
"Determines which property is highlighted in the property browser ..." The
following steps describe the behavior of Visual Basic for UI Default
properties:

1. When a control is selected in design-mode, Visual Basic checks if the
user has highlighted any property for the previously selected control.
If the highlighted property is available for the selected control, that
property will be highlighted. Otherwise, the property marked as UI
Default will be highlighted.

2. When Visual Basic is started, the property window always highlights the
"UI Default" property. As new controls are added, the "UI Default"
property for each control will be highlighted unless the user explicitly
selects some other property.

Step-by-Step Example
--------------------

1. Start a new Visual Basic Standard EXE project. Form1 is created by
default. Click on Form1. The property highlighted is "Caption," which is
"UI Default" for Form.

2. Add a TextBox, Text1, to Form1. Now, the highlighted property is "Text,"
which is the "UI Default" for a TextBox.

3. Add a PictureBox to Form1. Now, the highlighted property is "Picture,"
and it is the "UI Default."

4. Click on the Form, TextBox, etc., and note that the UI Default is
automatically highlighted.

5. Click on Text1. The "Text" property is highlighted. Click on
the Name property to select it.

6. Click on other controls and note that the Name property is
highlighted for all the controls.

7. Click on Text1 and select the "Alignment" property. Select other
controls. If the previously selected property is available, that
property will be highlighted. Otherwise, the UI Default property is
highlighted.

This is not intended to confuse, only to point out that the highlighted
property in the properties window will vary depending on the state of
the IDE, and a property defined as the UI Default will not always be
highlighted.

Difference Between Default Procedure ID and User Interface Default
------------------------------------------------------------------

When you display the Procedure Attributes dialog and press the Advanced
button, you will see a combo box titled "Procedure ID" and a check box with
the description "User Interface Default." These fields serve two different
purposes; the Procedure ID settings are for run-time and the User Interface
Default is a design-time setting.

The Default setting for Procedure ID is available for Properties or
Methods, but not for Events. You may have only one Procedure specified as
"Default" per component. What a default Procedure ID setting means is that
the default method or property will be executed when no specific member is
specified. For example, the following statement on the client form will
cause the default method of the UserControl (in this case a Subroutine) to
execute:

UserControl11

The following line of code will cause the default property of the control
to be set to the string "This is my control," assuming the default property
will accept one:

UserControl11 = "This is my control"

The User Interface Default setting is available for Events or Properties,
but not for Methods. You can have one UI Default property and one UI
Default event per component. The purpose of the UI Default for a property
is to determine which property is displayed in the properties window at
design-time when the control is selected or placed on a form, subject to
the behavior discussed earlier in this article. The purpose of the UI
Default for an event is to specify which event procedure should appear when
you double-click on the control. This applies to the control when it is
seated on a form, not to the control itself.

NOTE: If you are changing User Interface Default properties and they don't
seem to be taking effect, run the form and then go back into design mode
and test again. The UI Defaults, particularly the event default that
determines which event procedure is displayed when double-clicking, may not
get refreshed until the form has been run.
返回

Add Explorer Style Rename Capability to TreeView Control

PRODUCT :Microsoft Visual Basic for Windows
PROD/VER:WINDOWS:5.0,6.0
OPER/SYS:WINDOWS
KEYWORDS:

======================================================================
---------------------------------------------------------------------
The information in this article applies to:

- Microsoft Visual Basic Learning, Professional, and Enterprise Editions
for Windows, versions 5.0, 6.0
---------------------------------------------------------------------

SUMMARY
=======

The following procedure can be used to edit the node name on the TreeView
side of the Windows Explorer:

1. Click on a node with focus to place it in edit mode.

2. Remove all the text from the Node label.

3. Move focus to a different Node.

4. You receive the following error message:

You must type in a file name.

6. Focus returns to the Node you were editing and you remain in edit mode.

7. If you hit the Esc Key, the original text is placed back in the Node
label.

MORE INFORMATION
================

This sample demonstrates how to achieve the same effect using the TreeView
control in Visual Basic. It also shows how to verify that when a user edits
a Node label that it is not left blank.

Step-by-Step Example
--------------------

1. Create a new standard EXE project. Form1 is created by default.

2. From the Project menu, select Components, check "Microsoft Windows
Common Controls 6.0," and then click OK.

3. Add a TreeView and Timer control to Form1.

4. Add the following to the code window of Form1:

Option Explicit

Dim sNodeText As String ' to hold the node text

Private Sub Form_Load()
'Add some nodes to the TreeView
TreeView1.Nodes.Add , , , "test"
TreeView1.Nodes.Add , , , "test 1"
TreeView1.Nodes.Add , , , "test 2"
End Sub

Private Sub Timer1_Timer()
' Put the TreeView in edit mode
TreeView1.StartLabelEdit
Timer1.Enabled = False
End Sub

Private Sub TreeView1_AfterLabelEdit(Cancel As Integer, _
NewString As String)
' Make sure that we have a value in the Label
If Len(NewString) < 1 Then
' The Label is empty
MsgBox "Error! You must enter a value"
' enable the Timer to get us back to edit mode
Timer1.Interval = 100
Timer1.Enabled = True
End If
End Sub

Private Sub TreeView1_BeforeLabelEdit(Cancel As Integer)
' If the label is not empty store the string
If Len(TreeView1.SelectedItem.Text) > 0 Then
sNodeText = TreeView1.SelectedItem.Text
End If
End Sub

Private Sub TreeView1_KeyUp(KeyCode As Integer, Shift As Integer)
' If the user hits the Esc key then restore the old label
If KeyCode = vbKeyEscape Then
TreeView1.SelectedItem.Text = sNodeText
End If
End Sub

5. Save and run the project. Click on a Node to select it, then click on it
again to place it in edit mode.

6. Follow steps 1-7 as described in the Summary above. When you hit the
ESC key, you will see that the old value for the Node label has been
restored.
返回

Add a Horizontal Scroll Bar to a Visual Basic ListBox

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 Learning, Professional, and Enterprise Editions
for Windows, versions 5.0, 6.0
---------------------------------------------------------------------

SUMMARY
=======

The normal ListBox that comes with Visual Basic for Windows does not have a
horizontal scroll bar. This can be a problem when the item in a ListBox
extends past the boundaries of the ListBox. To add a horizontal scroll bar
to the control, you can call the Windows API SendMessage function with the
LB_SETHORIZONTALEXTENT constant.

MORE INFORMATION
================

This message sets the width in pixels by which a ListBox can scroll
horizontally. If the width of the ListBox is smaller than this value,
a horizontal scroll bar will be added to allow horizontally scrolling items
in the ListBox.

Step-by-Step Example
--------------------

1. Create a new Standard EXE project in Visual Basic. Form1 is created
by default.

2. Add a CommandButton and a ListBox to Form1.

3. Paste the following code into the general declaration section of Form1:

Option Explicit
Private Declare Function SendMessageByNum Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, ByVal _
wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Const LB_SETHORIZONTALEXTENT = &H194

Private Sub Command1_Click()
Dim s As String
Static x As Long
s = InputBox("Please enter any text", "List scroll", _
"this is a simple scrollbar sample for demonstration purposes")
List1.AddItem s
If x < TextWidth(s & " ") Then
x = TextWidth(s & " ")
If ScaleMode = vbTwips Then _
x = x / Screen.TwipsPerPixelX ' if twips change to pixels
SendMessageByNum List1.hwnd, LB_SETHORIZONTALEXTENT, x, 0
End If
End Sub

2. Run the project and click on Command1. You will be prompted to enter a
text string to be added to the ListBox. Each time you enter a string,
the code checks to see if it is longer than the previous longest string
and will adjust the scroll bar as necessary.
返回

Install an ActiveX Control for Design-Time Use

PRODUCT :Microsoft Visual Basic for Windows
PROD/VER:WINDOWS:5.0
OPER/SYS:WINDOWS
KEYWORDS:

======================================================================
---------------------------------------------------------------------
The information in this article applies to:

- Microsoft Visual Basic Professional and Enterprise Editions for
Windows, version 5.0
---------------------------------------------------------------------

SUMMARY
=======

This article describes how to use the Microsoft Visual Basic Application
Setup Wizard to install the design-time license information for a custom
ActiveX control developed in Visual Basic.

MORE INFORMATION
================

In order for the Setup Wizard to install the license information a .VBL
file must be created. This is accomplished by specifying that the control
require license information. The following steps will demonstrate how to
create a new control and require a license file for the control.

Step-by-Step Example
--------------------

1. Start Microsoft Visual Basic and create a new ActiveX Control project.
UserControl1 is created by default.

2. On the Project menu, click Project1 Properties, and then click the
General Tab.

3. Select "Require License Key" and click Ok.

At this point, the custom ActiveX control will require a license to be
used in the design environment. When the control is compiled, a .VBL
file will be created that contains the necessary license information. If
using the Setup Wizard to generate an application distribution set, the
Setup wizard will include the license information in the [Licenses]
section of the Setup.lst file.
返回

Suppress Default Pop-up Menu When Using Custom Menu

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 Editions
for Windows, versions 5.0, 6.0
---------------------------------------------------------------------

SUMMARY
=======

Some Visual Basic controls, such as the TextBox, have a default pop-up menu
that will automatically be display when you alternate-mouse click on the
control. This article demonstrates one way to disable this default pop-up
menu in order that either no menu or only a custom pop-up menu is
displayed.

MORE INFORMATION
================

When you alternate-mouse click on the TextBox control, its default pop-up
menu will be displayed. Visual Basic does not have a property or any other
built-in mechanism that directly disables this feature. However, setting
the control's Enabled property to False will prevent the menu from being
displayed although this allows the user to see that the control is
disabled.

One workaround is to use the Windows LockWindowUpdate API in conjunction
with the Enabled property. The LockWindowUpdate function disables or re-
enables drawing in a specified window. After the operation is complete, the
control is re-enabled and the LockWindowUpdate API is called a second time
to resume drawing of the control.

Steps to Create Sample Project
------------------------------

1. Start a new Standard EXE project in Visual Basic. Form1 is created by
default.

2. Add a TextBox control to Form1.

3. Choose Menu Editor from the Tools menu and create a menu named mnuPopUp
on Form1. Deselect the Visible CheckBox and add items such as the
following:

File
New
Open

4. Add the following code to the code window of Form1:

Private Declare Function LockWindowUpdate Lib "user32" _
(ByVal hwndLock As Long) As Long

Private Sub mnuOne_Click()
Text1.Text = "Menu One was clicked"
End Sub

Private Sub mnuTwo_Click()
Text1.Text = "Menu two was clicked"
End Sub

Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, _
X As Single, Y As Single)

If Button = vbRightButton Then
' Avoid the 'disabled' gray text by locking updates
LockWindowUpdate Text1.hWnd

' A disabled TextBox will not display a context menu
Text1.Enabled = False

' Give the previous line time to complete
DoEvents

' Display our own context menu
PopupMenu mnuPopup

' Enable the control again
Text1.Enabled = True

' Unlock updates
LockWindowUpdate 0&
End If
End Sub

5. Save and run the project.

6. Alternate-mouse click on Text1. Only the custom menu is displayed. The
standard editing menu is not shown.

An alternative approach to supressing the default pop-up menu is to
subclass
the control. Through subclassing, you can monitor for the appropriate mouse
messages and handle them accordingly. See the REFERENCES section below for
more information on this topic.
返回

Avoid GPF When Using a VB6 DLL with a VB5 UserControl

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 a PropertyBag interface is passed out of a Visual Basic 5.0
UserControl into a Visual Basic 6.0 DLL, and the Visual Basic 6.0 server
uses the Contents property of the PropertyBag interface, the server
produces the following error:

Compile error: Function of interface marked as restricted, or the
function uses as Automation type not supported in Visual Basic.

MORE INFORMATION
================

A PropertyBag object is passed into an object through the ReadProperties
and WriteProperties events in order to save and restore the state of the
object. The problem described in this article occurs when a Visual Basic
5.0 PropertyBag interface is passed into a Visual Basic 6.0 server, which
then attempts to de-reference a non-existent VTable entry for the Contents
property.

This occurs because the Contents property of the PropertyBag interface in
version 6.0 did not exist in version 5.0.

To avoid binary incompatibility when inter-mixing Visual Basic 5.0 and
Visual Basic 6.0 components, use the PropertyBag_VB5 interface in Visual
Basic 6.0 projects and do not use the Contents property.

Steps to Reproduce Behavior
---------------------------

1. Create a new Visual Basic 6.0 ActiveX DLL project. A project named
Project1 and a class module named Class1 are created by default.
From the Properties Page, change the project Name property to
PropHandler2.

2. From the Class1(Code) window, enter the following code:

Public Sub Test(obj As PropertyBag)
Dim vnt As Variant
vnt = obj.Contents
obj.Contents = vnt
End Sub

3. From the File menu, choose Make PropHandler2.dll to build
PropHandler2.dll. In the Make Project dialog, make a note of the
DLL file name and directory where the DLL is saved, then choose OK.

4. Create a new Visual Basic 5.0 ActiveX Control project. UserControl1
is created by default.

5. The reference to the DLL built in step 3 now needs to be added to the
ActiveX Control project. From the Project menu, choose References, and
then add the reference to PropHandler2.dll. To do this, scroll down
the list of available references to find PropHandler2 and check its
checkbox. Then choose OK.

NOTE: If PropHandler2 is not found, click on Browse and go to the
directory in which PropHandler2.dll was built. Enter PropHandler2.dll
as the File name, then choose Open.

6. From the UserControl1(Code) window, enter the following code:

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
Dim obj As New Class1
obj.Test PropBag
End Sub

7. Close the control's design window. To do this, from the Project
window, double-click UserControl1 to bring its designer to the
front, then press CTRL+F4 to close the window. Closing the
designer's window puts the UserControl1 control in run mode. As
soon as the control is in run mode, its icon (the default toolbox
icon for a user control) is enabled in the Toolbox.

8. From the File menu, choose Add Project. Add a new Standard EXE
project named Project2. Form1 is created by default.

9. From the Toolbox, place the UserControl on Form1.

10. From the File menu, choose Save Project2. At the Save File As
dialog, choose Save. Note that the above compile error will be received.
返回

Change ProgressBar Orientation at Run-Time

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 Learning, Professional, and Enterprise Editions
for Windows, version 6.0
---------------------------------------------------------------------

SUMMARY
=======

When changing the Orientation property of a ProgressBar, the bar changes
position on the form. If this Orientation is changed at run-time, it could
overlap other controls or be displayed off the visible portion of the form.
This article describes how to change the Orientation and keep the control
in the same position.

MORE INFORMATION
================

Step-by-Step Example
--------------------

1. Start a new Visual Basic project. Form1 is created by default.

2. Select Components from the Project menu. Check "Microsoft Windows Common
Controls 6.0" and click OK.

3. Add a Progress Bar, ProgessBar1, to Form1.

4. Add a CommandButton, Command1, to Form1.

5. Add the following code to Form1's code window:

Private Sub Command1_Click()
Dim iWidth As Integer
Dim iHeight As Integer
Dim iLeft As Integer
Dim iTop As Integer

' Store the current position
iWidth = ProgressBar1.Width
iHeight = ProgressBar1.Height
iLeft = ProgressBar1.Left
iTop = ProgressBar1.Top

' This only works with Smooth Scrolling
ProgressBar1.Scrolling = ccScrollingSmooth

' Change the Orientation to the opposite
If ProgressBar1.Orientation = ccOrientationVertical Then
ProgressBar1.Orientation = ccOrientationHorizontal
Else
ProgressBar1.Orientation = ccOrientationVertical
End If

' Reset the new position with the old position
ProgressBar1.Width = iWidth
ProgressBar1.Height = iHeight
ProgressBar1.Left = iLeft
ProgressBar1.Top = iTop

DoEvents
Form1.Refresh
ProgressBar1.Value = 0
ProgressBar1.Max = 1000
For i = 1 To 1000
ProgressBar1.Value = i
Next i
End Sub

5. Run the Project and click Command1 multiple times to see vertical and
horizontal scrolling.
返回

Back to top