dcsimg
Results 1 to 13 of 13

Thread: [RESOLVED] How to dynamically add control to a form

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    252

    Resolved [RESOLVED] How to dynamically add control to a form

    I created a custom control called 'Option', now I want to use this 'Option' control which I created in another control called 'Question'. I created a button on the
    question control so that whenever I click on it, a new instance of the 'Option' control will be created and added to the interface.
    I'm having problem adding the control on the form whenever the button is clicked; though the control adds successfully but i have to do some extra dirty work which
    I see inappropriate. Whenever I click the button, I have to add the instantiate the control and add it to the list of options which is a property of the 'Question'
    control and then call '
    Code:
    Question.Controls.Add(NewOption)
    ' to display the Option control on the interface. Prior to how .Net does its own, whenever you call
    Code:
    Me.Controls.Add(NewButton)
    , it adds a button to the interface automatically and the form knows all its controls. How can I achieve this?
    Code:
     public partial class Question : UserControl
    
        Property QuestionNumber As Integer
        Property QuestionString As String
        Property NumberOfOptions As Integer
        Property Options As Dictionary(Of Char, Option)
        Property IsWithImage As Boolean
        Property QuestionImage As Image
        Property IsFirstQuestion As Boolean
    
     private Sub Question_Load(object sender, EventArgs e) Handles MyBase.Load
            While (NumberOfOptions < 4)
                AddOption()
            End While 'Add four options as default
                
            End Sub
    
     public AddOption()
            
                 Dim NewOption As Option  'create new option
            Dim OptionChars() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I"} 'array of option letter
    If Not (Me.NumberOfOptions = 0) Then
    
                    'there is/are options already in the option list
                    NewOption.SetOptionLetter(OptionChars[this.NumberOfOptions]) 'set the option letter depending on the number of available options
                    'NewOption.Name = "Question" + Options.Count + 1
                    'this.Options.Add(NewOption.OptionLetter, NewOption) 'add the option to the Options list----this one also is giving me problem adding to the dictionary
    		'it throws a nullObjectReference
                    NewOption.Location = GetLocation() 'set the location of the option
                    Me.grpOptions.Controls.Add(NewOption) 'display the control on the interface
                    Me.NumberOfOptions+=1 'increase the number of options
                
                else
                
                    'there are no options in the option list
                    NewOption.SetOptionLetter(OptionChars(0)) 'set the option letter to A
                   ' NewOption.Name = "Question" + Options.Count + 1
                    Me.Options.Add(NewOption.OptionLetter, NewOption) 'add the option to the Options list
                    NewOption.Location = GetLocation() 'set the location of the option
                    Me.grpOptions.Controls.Add(NewOption) 'display the control on the interface
                    Me.NumberOfOptions+=1 'increase the number of options
                End If
            End Sub
    End Class
    Nothing I post is Self Reliable. Use it at your own risk

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    252

    Re: How to dynamically add control to a form

    This is the Question control and the options inside itName:  question.jpg
Views: 39
Size:  11.7 KB
    Nothing I post is Self Reliable. Use it at your own risk

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,756

    Re: How to dynamically add control to a form

    The form doesn't know all of its controls. The form knows only controls that are in its own Controls collection. There are plenty of controls that act as containers for others, such as a panel, groupbox, picturebox, and so on. However, EVERY control has a Controls collection, so ANY control can act as a container, in theory. A control has to be in SOME controls collection, but it doesn't have to be in the Controls collection of the form. If a control is NOT in the Controls collection of the form, then the form doesn't know anything about it.

    So, in what way does the form need to know about your Option? It looks like it is already a part of your user control, so the user control should expose whatever is needed outside of the user control, but otherwise, the Option shouldn't be visible. Figure out what needs to be exposed, and expose only that.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    252

    Re: How to dynamically add control to a form

    Quote Originally Posted by Shaggy Hiker View Post
    The form doesn't know all of its controls. The form knows only controls that are in its own Controls collection. There are plenty of controls that act as containers for others, such as a panel, groupbox, picturebox.........
    Thanks alot, I think you understand what I mean.
    Actually, I'm not adding the Option control to the form but I'm adding it to the Question control. The Option dictionary is like the collection for the Option controls. I don't want to be adding a newly instantiated Option control to the collection and the using that same instance to show it on the form. I want that whenever I add an option control to the collection, it should automatically display on the interface of the Question control like how it's done when you create a new instance of a button the call
    Code:
     Groupbox.Controls.Add(MyButton)
    Nothing I post is Self Reliable. Use it at your own risk

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    252

    Re: How to dynamically add control to a form

    Quote Originally Posted by Shaggy Hiker View Post
    The form doesn't know all of its controls. The form knows only controls that are in its own Controls collection. There are plenty of controls that act as containers for others, such as a panel, groupbox, picturebox.........
    Thanks alot, I think you understand what I mean.
    Actually, I'm not adding the Option control to the form but I'm adding it to the Question control. The Option dictionary is like the collection for the Option controls. I don't want to be adding a newly instantiated Option control to the collection and the using that same instance to show it on the form. I want that whenever I add an option control to the collection, it should automatically display on the interface of the Question control like how it's done when you create a new instance of a button the call
    Code:
     Groupbox.Controls.Add(MyButton)
    Nothing I post is Self Reliable. Use it at your own risk

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,951

    Re: How to dynamically add control to a form

    But isn't that what you are doing right here:
    Code:
    Me.grpOptions.Controls.Add(NewOption)

    ???

    I'm failing to see the problem...

    You seem to be doing everything right... you create the control, you set the location, then add it to the controls collection... that's what it takes...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,658

    Re: How to dynamically add control to a form

    Am I missing something? I don't see in the code in Post #1 where he instantiates NewOption. I see the declaration of the variable, but I don't see the object being created.

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,951

    Re: How to dynamically add control to a form

    No... you're not missing anything... the OP is... I thought it was this part:
    Code:
     public AddOption()
            
                 Dim NewOption As Option  'create new option
    But I see now that you're right... that just dims the var, doesn't actually create it...

    That's not the only issue I have with the code either... but it's not related to the issue at hand so I'm trying to let it go for now.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    252

    Re: How to dynamically add control to a form

    Quote Originally Posted by passel View Post
    Am I missing something? I don't see in the code in .....
    That was error in expression
    Nothing I post is Self Reliable. Use it at your own risk

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    252

    Re: How to dynamically add control to a form

    My problem is this, I don't want to be adding the Option control in the group box,l like this
    Code:
    Me.grpbox.Controls.Add(NewOption)
    Me.Options.Add(NewOption)
    I want it to be done automatically like adding control in a normal group box control.
    I want that whenever I do something like this
    Code:
     Options.Add(NewOption)
    Then the Option will be added to the Options collection and then displayed on the form too
    Nothing I post is Self Reliable. Use it at your own risk

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,951

    Re: How to dynamically add control to a form

    Yeah, I'm still confused... it sounds like you want to be able to add controls, but with out having to actually add them... it doesn't work like that... if you want a control to show up somewhere, you need to add it to the controls collection of the control where you want that control to show up. Period. There's no way around that. Like I said, it takes at least three steps: 1) create the control, 2) set the location where the control will be, and 3) add it to the controls collection of the containing control. End of story. Period. There's no way around that. You can factor that into a function or a method so that it's easy to call, and you can call it the way you want, but you still have to write that code to begin with at some point. There's no magic way to put controls on a form.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Aug 2017
    Location
    Nigeria
    Posts
    252

    Re: How to dynamically add control to a form

    I was thinking that if I only want to be adding it to the collection and then it adding to the control itself, I will need to create an event that triggers if the Options.Count() changes. Right?
    Nothing I post is Self Reliable. Use it at your own risk

  13. #13
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,658

    Re: How to dynamically add control to a form

    Isn't the collection part of your user control? If you add the option object to the collection, then why wouldn't you add it to the user control at the same time since you're right there.

    When you add the control to the collection or other places, you're adding a reference to the control, so you don't have multiple copies of the control, but multiple references to the one control, so it isn't a lot of data to add an additional reference.
    Last edited by passel; Dec 7th, 2018 at 04:54 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width