Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_vb thread: Dynamic Applications Through VB <Urgent>


Message #1 by css_p2p@y... on Fri, 26 Jul 2002 10:28:45
Sorry -- bug in the first code I sent...replace the first For...Next loop in
cboOptions_Click() with this:

    For i = LBound(msSelectedOptions, 2) + 1 To j
        If msSelectedOptions(1, i) = _
                cboOptions.ItemData(cboOptions.ListIndex) Then
            MsgBox "Option has already been selected"
            Exit Sub
        End If
    Next i

PNK

-----Original Message-----
From: Peter N. Kipe [mailto:pkipe@c...]
Sent: Friday, July 26, 2002 11:21 AM
To: professional vb
Subject: [pro_vb] RE: Dynamic Applications Through VB <Urgent>


Here's one solution:

In a new project, add a combo box (cboOptions), a label (lblOption, Index 0,
Visible = False), and a textbox (txtOption, Index 0, Visible = False).  Put
the following in the form's code:

Option Explicit

Private msAllOptions() As Variant
Private msSelectedOptions() As Variant
Private mbInFormLoad As Boolean
Private miFirstObjectTop As Integer
Private miFirstObjectLeft As Integer

Private i As Integer

Private Sub cboOptions_Click()
Dim liLeft As Integer
Dim liTop As Integer
Dim liHeight As Integer
Dim liWidth As Integer
Dim j As Integer

    If mbInFormLoad Then
        Exit Sub
    End If

    ' test to see if any items exist in selected array
    On Error Resume Next
        j = UBound(msSelectedOptions, 2)
        ' error 9 means array hasn't yet been Dimed
        If Err.Number = 9 Then
            GoTo cboOptions_Click_SkipEdit
        End If
    On Error GoTo 0

    ' items exist - make sure clicked option hasn't already been selected
    For i = LBound(msSelectedOptions, 2) To j
        If msSelectedOptions(1, i) = _
                cboOptions.ItemData(cboOptions.ListIndex) Then
            MsgBox "Options has already been selected"
            Exit Sub
        End If
    Next i

cboOptions_Click_SkipEdit:
    ' add clicked option to selected options array
    ' (ones-based - occurrence zero will be left empty)
    j = j + 1
    ReDim Preserve msSelectedOptions(1, j)
    msSelectedOptions(0, j) = _
        cboOptions.List(cboOptions.ListIndex)
    msSelectedOptions(1, j) = _
        cboOptions.ItemData(cboOptions.ListIndex)

    ' build clicked option's textbox
    liTop = miFirstObjectTop + (txtOption(0).Height * j) +
txtOption(0).Height
    liLeft = miFirstObjectLeft

'    Unload txtOption(0)

    Load lblOption(j)
    lblOption(j).Caption = msSelectedOptions(0, j) & ":"
    lblOption(j).Left = liLeft
    lblOption(j).Top = liTop + 30
    lblOption(j).Visible = True

    Load txtOption(j)
    txtOption(j).Left = liLeft + lblOption(j).Width
    txtOption(j).Top = liTop
    txtOption(j).Visible = True

End Sub

Private Sub Form_Load()
    mbInFormLoad = True

    ' load msArray with combobox list values and item datas
    ReDim msAllOptions(1, 4)
    msAllOptions(0, 0) = "Option 1"
    msAllOptions(1, 0) = 0
    msAllOptions(0, 1) = "Option 2"
    msAllOptions(1, 1) = 1
    msAllOptions(0, 2) = "Option 3"
    msAllOptions(1, 2) = 2
    msAllOptions(0, 3) = "Option 4"
    msAllOptions(1, 3) = 3
    msAllOptions(0, 4) = "Option 5"
    msAllOptions(1, 4) = 4
    Call P_LoadComboBox(cboOptions, msAllOptions, 0, 1)

    ' establish position of text boxes on form
    miFirstObjectTop = 1000
    miFirstObjectLeft = 600

    ' out of form load
    mbInFormLoad = False

End Sub

Add a module and put the following in it:

Option Explicit

Public Sub P_LoadComboBox( _
    ByRef List As Control, _
    ByVal Data As Variant, _
    Optional ByVal DisplayIndex As Integer, _
    Optional ByVal DataIndex As Integer)

Dim lngCount As Long

    If Not ((TypeOf List Is ComboBox)) Then
        Exit Sub
    End If

    ' clear the combo box contents, if any
    List.Clear

    ' load combo box from the passed array, including ItemData if present
    For lngCount = LBound(Data, 2) To UBound(Data, 2)
        Call List.AddItem(Trim(Data(DisplayIndex, lngCount)))
        List.ItemData(List.NewIndex) = Data(DataIndex, lngCount)
    Next lngCount

    ' position combo box to 1st list item
    List.ListIndex = 0

End Sub

HTH, Pete

-----Original Message-----
From: css_p2p@y... [mailto:css_p2p@y...]
Sent: Friday, July 26, 2002 10:29 AM
To: professional vb
Subject: [pro_vb] Dynamic Applications Through VB <Urgent>


Hi All,

I have to do an application that will require something like the following:

When a user selects from a drop-down a few options, let's say the user
selected 3 options, then through VB code I have to create 3 text boxes
automatically so that the user can give names to the options selected..

Then I also have to create 3 variables for the selected options, so that
when the application is used, it will be possible to store data to the
selected options..

All this has to happen dynamically cos the next time, the user may select
5 options, which means instead of 3, the same has to be done for 5..

Please help me on this matter.

Thanks a lot.

Sudharshan.

---
Visual C# - A Guide for VB6 Developers
This book will make it easy to transfer your skills
from Visual Basic 6 to C#, the language of choice
of the .NET Framework.
http://www.wrox.com/ACON11.asp?ISBN=1861007175&p2p0059




---
Visual C# - A Guide for VB6 Developers
This book will make it easy to transfer your skills
from Visual Basic 6 to C#, the language of choice
of the .NET Framework.
http://www.wrox.com/ACON11.asp?ISBN=1861007175&p2p0059



  Return to Index