Results 1 to 7 of 7

Thread: How do I add a custom control?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    7

    How do I add a custom control?

    Hi there,

    I'm still quite new to the NET and Visual Basic (2005), and I'm trying to create a custom (user-) control, which is nothing more than a couple of buttons on a container.

    I have followed a couple of examples (from "Beginning Visual Basic 2005" and others) letter by letter but somehow I just don't get the results as described/expected from the examples.

    If I'm to believe the example(s), it should be as 'easy' as selecting a Visual Basic >"Windows Control Library" project, adding the actual controls to the container, building the solution and then when adding a new project the control should be accessible from the Toolbox...however, I cannot add the custom control to a form (in the new project) because it is disabled.

    Actually I can see it's not right, because the controls is not only disabled in the toolbox but instead of seeing the (reference-) name like the other controls (Textbox, checkBox, ComboBox) all I get is the (disabled-) text of the path to the actual vb file, as follows :

    "Text : C:\Dev\MyTestProject\MyTestControl.vb"

    Looking around on the internet I found a sample project to illustrate the creation of custom/user Controls but when opening it I got the same effect on the toolbox (disabled reference to the actual vb file), and more strangely, I was presented with the custom controls present on a form, but NO CLUE of how that was achieved, and I was/am unable to get more instances of those controls on the form myself.

    Any idea anyone?
    I know I'm probably missing an essential step in the process, maybe even so simple that the books/samples forget to bring it up.

    Thanks a lot in advance for any help provided.

    Vic

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: How do I add a custom control?

    you have to include the custom control as part of the project, unless its compiled as an .ocx custom control, in which case you can add it to the toolbox like any control

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do I add a custom control?

    Follow these steps and see if they work for you:

    1. Open VS and create a new project of type Windows Control Library.
    2. To the default UserControl add a Button and a TextBox.
    3. Build your solution by selecting Build -> Build Solution from the main menu.
    4. In the Solution Explorer, right-click the solution (first node) and select Add -> New Project.
    5. Save the current project to the default location if prompted.
    6. Select Windows Application as the new project type.
    7. Build the solution again.
    8. Open the Toolbox and look for the tab at the top named for your original project.
    9. Double-click the item named for your UserControl, which should be UserControl1.

    Now, does that add an instance of your UserControl to the form? If so then your original issue must have been something to do with that project specifically. If it doesn't work then something is amiss and maybe your installation is corrupted.

    Note that all controls and components declared in the current solution will be displayed in the Toolbox automatically. If you want to make those components available to other solutions then you will need to add them to the Toolbox manulay. You do this by right-clicking the Toolbox and selecting Choose Items, then navigating to the DLL containing the component(s) you want to add.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    7

    Re: How do I add a custom control?

    Hi,

    Thanks to both for the prompt responses.

    Jmcilhinney, I followed your instructions step by step.
    Actually, what I had tried before was pretty much the same, only then I had changed the default name of the UserControl1 to something else, and had added a little code behind the buttons.

    Of course, I know that if that was the reason for my example not to work, then that would already mean there was something wrong with it, but just to have 'evidence' that it's not working I did nothing more than what you suggested, but to no avail...no toolbar added after building the solution (after adding the 2nd project) and thus no way to access my UserControl.

    You say that if it does not work then there must be something wrong with the VS2005 installation, and seeing that it's the case, I'm wondering how that's possible, since I have not used it that much to have 'corrupted' it 'accidentally'.

    The installation itself takes quite some time, and I'm afraid that afterwards it will still be the same, after all, it's still the same computer, the same OS (Windows XP Pro SP2), and the same VS2005 version...so what would be the unknown 'variable' to hope for success the 2nd time around?

    Now, that I think about it, I did install ServicePack 1 for right after installing VS2005. Are there any known issues against SP1? Would you say it's a necessary update?
    I guess I will just have to reinstall it, and first see if it works before installing SP1.

    If anyone out there has had this same problem and somehow found a solution for it, I would appreciate hearing about it very much.

    Thanks again.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do I add a custom control?

    Just to be absolutely sure, you're saying that when you create a Windows Control Library project with a UserControl and a Windows Application with a Form, after building the solution you do not get this:
    Attached Images Attached Images  
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    7

    Re: How do I add a custom control?

    That's right...That's what I do not get.

    But thanks a lot for the screenshot, because I was curious what icon the usercontrol would get when appearing on the tab (which I do not get either, not even by selecting "Show All").

    By the way, do you have SP1 installed on your VS2005?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do I add a custom control?

    I always install all service packs as soon as they are released.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width