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

Set a Minimum and Maximum Size for a UserControl

PRODUCT :Microsoft Visual Basic for Windows
PROD/VER:WINDOWS:5.0
OPER/SYS:WINDOWS
KEYWORDS:kbVBp500 kbVBp600 kbCtrlCreate kbcode

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

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

SUMMARY
=======

When you create UserControls in Visual Basic, you may want to limit the
size of your UserControl. This article shows how to set a minimum and
maximum size for your UserControl so that a developer who uses the control
can't size the control past the preset minimum or maximum.

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

This example uses a UserControl that is inside a Standard EXE project. You
can use the same technique for a UserControl in an ActiveX Control project.
When the ActiveX control is built, anyone using the ActiveX control will
not be able to size it any larger or smaller then the limits you set inside
the UserControl itself.

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

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

2. Add a UserControl to the Project by clicking Add UserControl on the
Projects menu and then clicking Open.

3. Change the BackColor property of the UserControl to a different color,
such as red.

4. Add two labels to the UserControl.

5. Add the following code to the code window for the UserControl:

Private Sub UserControl_Resize()
' Check to see if the control is larger or smaller than the preset
' minimum or maximum size.
' If it is larger or smaller, set the size of the control.
Select Case Height
Case Is < 2400
Height = 2400
Case Is > 3600
Height = 3600
End Select

Select Case Width
Case Is < 2400
Width = 2400
Case Is > 3600
Width = 3600
End Select
' The label reports the height and width of the control in
' twips even if the container has a different scalemode.
Label1.Caption = "Height: " & Height
Label2.Caption = "Width: " & Width
End Sub

6. Close the UserControl and it appears in the ToolBox. Place it on Form1.

7. Try resizing the UserControl. If you try to make the control larger or
smaller then the limit you set in the Resize event of the UserControl,
the control returns to the maximum or minimum size.
返回

Programmatically Add Child Controls to a CoolBar

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

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

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

SUMMARY
=======

When adding a Child control to a CoolBar Band, you must specify the
container argument in the Controls.Add method call, and then set the Child
property of a Band to the new control.

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

The Child property of a CoolBar Band must be a control that is contained by
the CoolBar. Therefore, while the Container argument of the Add method is
optional, it must be supplied in order to add a Child control to a Band of
a CoolBar control.

The Add method of the Controls Collection has these parts:

object.Add(ProgID, name, container)

Adding a control in this way does not make the control automatically appear
because it is not yet a Child of a Band. Therefore, after you add the
control, set the Child property of the appropriate Band to the new control.
The control now appears. Note that a Band can only have one Child, so
attempting to set the Child property of a Band that already has a Child
control produces an error. See the REFERENCES section of this article for
more information.

Step By Step Example
--------------------

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

2. Select Components from the Projects menu, select "Microsoft Windows
Common Controls-3 6.0", and click OK.

3. Place a CoolBar control onto Form1. It has three Bands by default.

4. Add two CommandButtons to Form1.

5. Place the following code into the form's module:

Private Sub Command1_Click()
Dim MyCtrl As Object
' Add a new CommandButton to the first Band.
Set MyCtrl = Controls.Add("VB.CommandButton", "cmdTest", CoolBar1)
MyCtrl.Caption = "Test Button"
Set CoolBar1.Bands(1).Child = MyCtrl ' place on first Band
' Add a new TextBox to the second Band.
Set MyCtrl = Controls.Add("VB.TextBox", "txtTest", CoolBar1)
MyCtrl.Text = "Testing Text"
Set CoolBar1.Bands(2).Child = MyCtrl ' place on second Band
End Sub

Private Sub Command2_Click()
Dim MyCtrl As Object
' Add a new CheckBox to a new Band.
Set MyCtrl = Controls.Add("VB.CheckBox", "ckTest", CoolBar1)
MyCtrl.Caption = "Check Test"
CoolBar1.Bands.Add Child:=MyCtrl, Visible:=True ' add a Band
CoolBar1.Bands(CoolBar1.Bands.Count).Width = 1000 ' expand it
End Sub

6. Run the project and click Command1. You will see a new CommandButton
appear in the first Band and a new TextBox appear in the second Band.

7. Click Command2 and a fourth Band is added with a new CheckBox. Note
that when you set the Width property of a Band through code, it is not
larger than the minimum necessary to accommodate its Child control, but
you can resize it with the Mouse.
返回

Read Extender Properties from a UserControl

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
=======

There are times when your UserControl needs to check the value of an
Extender Property when your control loads. This example shows how to check
the values of Extender Properties from inside the UserControl. As an author
of a control, you should not attempt to set Extender Properties with code
in the UserControl.

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

Extender properties are provided by the container your control is placed
on, but they appear to be a seamless extension of your control. A
UserControl object can access extender properties through its Extender
object.

To access an extender property, you must make sure your control is sited on
the container. When the ReadProperties event of a UserControl happens, the
control should be sited. However it still may not have access to all the
extender properties. Because of this, the best place to read the value of
an extender property is from the controls Show event.

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

1. Create a new ActiveX Control project. UserControl1 is created by
default.

2. Change the BackColor of the UserControl to a different color such
as red.

3. Add the following code to the UserControl:

Private Sub UserControl_Show()
Debug.Print "WhatsThisHelpID " & Extender.WhatsThisHelpID

Debug.Print "Tooltiptext " & Extender.ToolTipText
Debug.Print "HelpContextID " & Extender.HelpContextID
Debug.Print "Tag " & Extender.Tag
Debug.Print "Name " & Extender.Name
End Sub

4. Close the UserControl's design and code windows.

5. From the File Menu, select Add Project, and add a Standard EXE project.
Form1 is created by default.

6. Make the Standard EXE project the Startup project by choosing Properties
from the Project menu and, on the General tab, pick Form1 as the Startup
Object.

7. Place an instance of the UserControl on Form1. You will see a list of
Extender properties in the Immediate Window.

8. Set the following properties for UserControl1 on Form1:

WhatsThisHelpID = 101
HelpContextID= 101
Tag = "MyControl"
ToolTipText = "Usercontrol"
Name = "Bubba"

8. Save and run the project group. In the Immediate window you will see the
new values for the different Extender properties.
返回

Upgrade Project to Use the New MSCCOMCTL.OCX in VB6

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, version 6.0
---------------------------------------------------------------------

SUMMARY
=======

The following controls are not automatically updated when a Visual Basic
5.0 project is loaded by Visual Basic 6.0:

COMCLTL32.OCX, COMCT232.OCX, and MSCHART.OCX

The project must be manually upgraded to the new Visual Basic 6.0 controls:
MSCOMCTL.OCX, MSCOMCT2.OCX, and MSCHRT20.OCX, respectively.

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

The new controls are not backward compatible with the older versions and,
in accordance with the rules of COM, were given new file names and new
GUIDs. This prevents your application from breaking existing applications
that use the older controls. Because the new controls have new names and
GUIDs, they also have new Typelibs. When you load a Visual Basic project,
it checks the Typelib version. However, Visual Basic does not know the
Controls were updated because the Typelib for the new controls have a
different GUID than the Typelib for the old controls.

To upgrade a project to the newer version, you must do the following:

1. Open the VBP file in a text editor, such as Notepad, and change the
line(s) that reference the old version of the OCX to the following:

Object={831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0; MSCOMCTL.OCX
Object={86CF1D34-0C5F-11D2-A9FC-0000F8754DA1}#2.0#0; MSCOMCT2.OCX
Object={65E121D4-0C60-11D2-A9FC-0000F8754DA1}#2.0#0; MSCHRT20.OCX

Save and close the VBP file.

2. Open any FRM file (or CTL file) that uses one of the above controls
in a text editor, and change the line(s) that reference the old
version of the OCX to the following:

Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
Object = "{86CF1D34-0C5F-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCT2.OCX"
Object = "{65E121D4-0C60-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCHRT20.OCX"

3. Change all text that references the old libraries to the new library
names. This means that you need to replace ComCtlLib, Comctl2, and
MSChartLib with MSComCtlLib, MSComCtl2, and MSChart20Lib. For example,
change:

ComctlLib.ImageList

to:

MSComctlLib.ImageList

4. Save and close the file. When you open the project in Visual Basic 6.0,
it will use the new MSCOMCTL.OCX, MSCOMCT2.OCX and/or MSCHRT20.OCX
controls.

NOTE:

1. IMPORTANT: Before you edit your VBP, FRM, or CTL files, you should
backup all of the files in your project.

2. While the objects listed in step 1 and 2 are very similar, they are not
identical and the upgrade will not work properly if each is not copied
exactly to the correct file.

3. If you get a message stating that the header is corrupt when you attempt
to load the project, you have probably made a copy error. Please go to
your backup and repeat the process.

4. If you prefer to continue using the older versions of these controls,
comctl32.ocx and comct232.ocx are included on the Visual Basic 6.0
product CD-ROM in the \OS\System directory. Mschart32.ocx resides in the
\Common\Tools\VB\Controls directory.
返回

ListBox控件中的CheckBox的技巧

如果将ListBox控件中的Style书香属性设置为1,则可以为ListBox中的每一个项目加上一个
CheckBox,如何得到某一个CheckBox是否被选中呢,可以使用Selected属性数组,例如
Selected(1)。
返回

如何更改CommandButton的背景颜色

很多朋友来Mail问为何改变CommandButton的BackColor属性却无法改变按钮的背景色,实际上光改变BackColor属性是不行的,还需要将CommandButton的Stlye属性设置为Graphical,然后在改变BackColor属性就可以产生效果了。
返回

程序启动时窗口自动处于屏幕中央

只要将Form对象的StartupPosition属性设置为2——vbStartUpScreen就可以使程序启动窗口位于屏幕中央。
返回

ListBox对象的Columns属性

我们印象中ListBox中的项目是从上到下一直排列的,如果项目程度超出了ListBox显示范围,ListBox上就会出现垂直滚动
条上下滚动浏览,使用Columns属性可以使当ListBox中的项目超出了ListBox显示范围后在水平方向排列,下面是范例:
首先在Form中假如一个ListBox控件,然后将控件的Columns属性设置为2,然后在Form1的Load事件中加入以下代码:
List1.Height = 1500
List1.Width = 3300
List1.AddItem "aaa"
List1.AddItem "aaa"
List1.AddItem "aaa"
List1.AddItem "aaa"
List1.AddItem "aaa"
List1.AddItem "aaa"
List1.AddItem "aaa"
List1.AddItem "aaa"
List1.AddItem "aaa"
List1.AddItem "aaa"
List1.AddItem "aaa"
运行程序,看看ListBox发生了什么变化,是不是不一样了?
返回

利用API获得文本框和RichTextBox中的某一行的文本。

利用API函数SendMessage发送EM_GETLINE消息可以获得文本框中的某一行的文本,代码如下:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
        (ByVal hwnd As Long, _
        ByVal wMsg As Long, _
        ByVal wParam As Long, _
        lParam As Any) As Long
        
Const EM_GETLINE = &HC4

Private Type myBuff
    byte1 As Byte
    byte2 As Byte
    sBuff As String * 256
End Type

Private Sub Command1_Click()
    Dim buff As myBuff
    Dim astr As String
    Dim iCount As Long
    
    buff.byte1 = 255
    iCount = SendMessage(RichTextBox1.hwnd, EM_GETLINE, 0, buff)
    If iCount > 0 Then
        astr = Chr(buff.byte1) + Chr(buff.byte2) + Left$(buff.sBuff, iCount - 4)
        Debug.Print astr
    End If
End Sub
其中astr就是获得的RichTextBox1中第一行的文本内容。
返回


建立可以响应事件的Word对象

我们知道可以在VB中建立Word对象,但是如何建立可以相应Word事件的对象呢?利用WithEvents就可以实现:
首先添加Microsoft Word的引用到VB工程,然后添加以下代码:

Dim xApp As New Word.Application
Dim WithEvents xDoc As Word.Document

Private Sub Command1_Click()
    Set xDoc = xApp.Documents.Open("c:\doc1.doc")
    xApp.Visible = True
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Set xApp = Nothing
End Sub

Private Sub xDoc_Close()
    Set xDoc = Nothing
    MsgBox "删除对象!"
End Sub
运行程序,点击Command1就可以建立Word实例并打开c:\doc1.doc。然后关闭文档,程序就会提示文档将关闭,点击
消息框的确定按钮就可以删除xDoc
返回

Back to top