RagismenoBaloni Δημοσ. 22 Δεκεμβρίου 2005 Δημοσ. 22 Δεκεμβρίου 2005 Θα ήθελα να μάθω αν είναι δυνατό να χρησιμοποιήσω ένα control (π.χ. δημιουργώντας το κατά την εκτέλεση) χωρίς προηγουμένως να το έχω δημιουργήσει στη φόρμα. Π.χ. θέλω να χρησιμοποιήσω ένα FileList control. Δηλώνοντας μια μεταβλητή ως FileList control (Dim Dir1 As FileList) δεν κατάφερα να κάνω τίποτα. Υπάρχει κάποιος τρόπος ή άδικα προσπαθώ?
YiannisMn Δημοσ. 23 Δεκεμβρίου 2005 Δημοσ. 23 Δεκεμβρίου 2005 Στη VB6 (στη VB.NET υπάρχει αντίστοιχος τρόπος) ξεκινάς μια εντελλώς άδεια φόρμα, όπου βάζεις τον εξής κώδικα για να πειραματιστείς: >Option Explicit Dim Dir1 As FileListBox Private Sub Form_Load() Set Dir1 = Me.Controls.Add("VB.FileListBox", "flsMyFileList") With Dir1 .Visible = True .Move 128, 128, Me.Width - 256, Me.Height - 640 .Enabled = True .Path = "C:\" End With End Sub Private Sub Form_Unload(Cancel As Integer) Set Dir1 = Nothing End Sub Επίσης, διάβασε αυτό: 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: Part Description 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. Remarks Note The Controls collection is a late-bound collection. This means the compiler cannot determine in advance which controls are contained by the collection, their objects or their interfaces. Without this information, the Auto Statement Builder will not function. This method allows you to add controls to an application at run time. Dynamic control addition can be used to add the functionality of a control to an application, even after the application has been compiled and deployed. For example, you may have several complex user controls, each suited to a different task. Depending on an external factor, such as time or date or user input, a different user control could be added to an existing form in an application. You can also use the container argument of the method to specify a container control (such as the Frame control) to position the control. Or you can design an application that automatically reads a file, database, or registry entry for new controls to load. In this way, you can modify an application without having to redeploy it. Important When you add an unreferenced control that requires a license to an existing (deployed) application, you must also add the license key for the control before using the Add method. For information on when and how to add licenses, see "Licenses Collection" in the See Also list. Adding Unreferenced Controls at Run Time You can also use the Add method to dynamically add a control that is not referenced in the project. (An "unreferenced" control is a control that is not present in the Toolbox.) To do so, you must also add the control's License key to the Licenses collection as well. The example below adds a control's license key before adding the control itself: Option Explicit Private WithEvents extCtl As VBControlExtender Private Sub Form_Load() Licenses.Add "prjWeeks.WeeksCtl", "xydsfasfjewfe" Set extCtl = Form1.Controls.Add("prjWeeks.WeeksCtl", "ctl1") extCtl.Visible = True ' The control is invisible by default. End Sub Note See Add Method (Licenses Collection) in the See Also list for more information about retrieving a control's license key. In order to program the events of such an unreferenced control, however, you must declare an object variable using the WithEvents keyword as a VBControlExtender object (shown above), and set the object variable to the reference returned by the Add method. Then use the VBControlExtender object's ObjectEvent event to program the control's events. An abbreviated example is shown below. Option Explicit Dim WithEvents objExt As VBControlExtender ' Declare VBControlExtender variable Private Sub LoadControl() Licenses.Add "Project1.Control1", "xydsfasfjewfe" Set objExt = Controls.Add("Project1.Control1", "myCtl") objExt.Visible = True End Sub Private Sub extObj_ObjectEvent(Info As EventInfo) ' Program the events of the control using Select Case. Select Case Info.Name Case "Click" ' Handle Click event here. ' Other cases now shown Case Else ' Unknown Event ' Handle unknown events here. End Select End Sub Note You can't assign an intrinsic control to the VBControlExtender variable; any attempt will result in a type mismatch error. You can also program the events of a dynamically added control by declaring an object variable using the WithEvents keyword, and setting the reference returned by the method to the variable, as shown below: Option Explicit ' Declare object variable as CommandButton. Private WithEvents cmdObject As CommandButton Private Sub Form_Load() Set cmdObject = Form1.Controls.Add("VB.CommandButton", "cmdOne") cmdObject.Visible = True cmdObject.Caption = "Dynamic CommandButton" End Sub Private Sub cmdObject_Click() Print "This is a dynamically added control" End Sub If you intend to add a user control or any ActiveX control to your form, you must either add the control to the Toolbox, or add its License key to the Licenses collection. See the Add Method (Licenses Collection) for more information. Note If you add an ActiveX or user control to your project but don't use it on a form, you must also uncheck the Remove Information About Unused ActiveX Controls option on the Make tab of the Project Properties dialog box. If your application attempts to add the control, the Add method will fail because the necessary information has been discarded. Removing Controls To remove any controls added dynamically, use the Remove method. It should be noted that you can only remove controls added using the Add method (in contrast to controls added using the Load statement). The example below removes a dynamically added control: Form1.Controls.Remove "ctl1" ' The control's name is ctl1. -------------------------------------------------------------------------------- Send feedback to MSDN.Look here for MSDN Online resources Αυτή είναι η μέθοδος για να δημιουργήσεις δυναμικά ένα εντελλώς νέο control (from scratch). Εάν θές απλά να προστέσεις ένα νέο member σε κάποιο υπάρχον control array, τότε προτίμησε την εντολή Load/Unload...
sotospez Δημοσ. 23 Δεκεμβρίου 2005 Δημοσ. 23 Δεκεμβρίου 2005 Υπάρχουν δύο τρόποι. 1ος τρόπος με την χρήση Control Array: Τοποθέτησε στην φόρμα σου ένα textbox με όνομα txtbox π.χ. και ένα command Button. Τώρα πρέπει να πούμε στην vb ότι το txtbox είναι μέρος από ένα Control Array. Αυτό γίνεται ως εξής. Στην πινακίδα με τις ιδιότητες του textbox στην ιδιότητα Index από κενό δίνω την τιμή 0. Στην συνέχεια στο command Button τρέχουμε τον εξής κώδικα: Private Sub Command1_Click() Load Check1(1) Check1(1).Caption = "New Checkbox" Check1(1).Visible = True Check1(1).Top = Check1(0).Top + Check1(0).Height End Sub Check1(1).Top = Check1(0).Top + Check1(0).Height Ή μπορείς να χρησιμοποιήσεις έναν βρόχο. for i=1 to κάτι Load Check1(i) Check1(i).Caption = "New Checkbox" Check1(i).Visible = True Check1(i).Top = Check1(i-1).Top + Check1(i-1).Height Next i O δεύτερος τρόπος είναι να δημιουργήσεις το control από την αρχή με την χρήση του Controls Collection. Είναι λίγο μεγάλος και δεν θα τον αναφέρω προς το παρόν. Αν έχεις πρόβλημα με τα παραπάνω πόσταρε ξανά. http://www.insomnia.gr/vb3/showthread.php?threadid=118614
Προτεινόμενες αναρτήσεις
Αρχειοθετημένο
Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.