2

我试图在 VB6 中创建一个向导类型的控件并遇到了一个绊脚石。

我想让控件的用户能够使用属性页向设计时控件添加和管理 CWizardPage(s)。

我使用的第一种方法是直接使用 Collection 将向导页面添加到 OCX,但是我遇到了两个问题,即 Collection 类不可持久化(而且我找不到一种简单的方法来实现它)并且VB6 在运行时实例化控件的能力似乎非常有限 - 所以实际上重新实例化它们似乎很困难。

我的下一个想法是只允许用户在设计时绘制向导页面。这种方法很有效,但是在另一个向导页面中而不是在 CWizardContainer 中绘制一个向导页面太容易了。

那么谁能告诉我如何在设计时向表单添加控件而不使用拖放?

4

3 回答 3

1

You can dynamically add controls to a form or other container (such as a UserControl) using the Add method of the container's Controls collection. For example, to add a TextBox named myTextBox to a form called frmMyForm, you could do this:

frmMyForm.Controls.Add "VB.TextBox", "myTextBox"

Here is a snippet from the VB6 help file:

Add Method (Controls Collection)


Adds a control to the Controls collection and returns a reference to the control.

Syntax

object.Add (***ProgID, name, container*)**

The Add method syntax has these parts:

object (Required)

An object expression that evaluates to an object in the Applies To list.

ProgID (Required)

A string that identifies the control. The ProgID of most controls can be determined by viewing the Object Browser. The ProgID is composed of the Library and Class of the control. For example, the CommandButton control's ProgID is VB.CommandButton. In cases where the ProgID differs from that shown in the Object Browser, Visual Basic displays an error message that contains the correct ProgId.

name (Required)

A string that identifies the member of the collection.

container (Optional)

An object reference that specifies a container of the control. If not specified or NULL, defaults to the container to which the Controls collection belongs. You can put a control in any existing container control (such as the Frame control) by specifying this argument. A user control or an ActiveX document can also be a container.

于 2008-11-17T01:05:34.790 回答
0

...VB6 seems very limited in it's ability to instantiate controls at run time...

Nothing could be further from the truth. Instantiating controls at run time in VB6 is trivial.

  1. At design time, set the Index property of the control to 0. This turns the control into a control array.
  2. At runtime, load new instances of the control as needed.

As an example, create new Standard EXE project drop a TextBox onto the form, set its Index to 0 and put the following in the Form_Load event:

Private Sub Form_Load()

    Dim newIndex As Integer

    newIndex = Text1.UBound + 1
    Load Text1(newIndex)
    Text1(newIndex).Top = Text1(newIndex - 1).Top + Text1(newIndex - 1).Height
    Text1(newIndex).Visible = True

End Sub
于 2008-11-17T02:51:51.473 回答
0

我曾开发过一个应用程序,该应用程序定期根据所需字段动态制作许多完整的数据输入屏幕。一直指定 Top、Left、Width 和 Height 是很冗长的。因此,您通常拥有控件的“模板”并将这些设置复制到附加的设置中。

于 2009-02-24T20:32:34.773 回答