Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Dynamically add a button to any form that is opened in a vb.net project.

Posted on 2014-03-27
7
Medium Priority
?
392 Views
Last Modified: 2014-04-12
I have a project that consists of close to 300 forms and user controls.  I'm trying to implement a button or some kind of clickable control to each of the forms in the project that will bring up information relevant to the form (like a help file).  I would really like to add this code in one place and dynamically add the control to the form as it loads.  Would there be a way to do this?  Thanks for any help.
0
Comment
Question by:rcblevins
7 Comments
 
LVL 40
ID: 39959788
Create a simple form that implements the common function(s). Then inherit from that one to create all the others.

To simplify inheritance, simply go Project...Add New Item...Windows Forms...Inherited Form and then select your base form.
0
 
LVL 15

Expert Comment

by:unknown_routine
ID: 39959824
Create a generic form (for example FormInfo).

Then when user clicks on the help button on your other forms, based on that from you should read from a config file or a database table to add the features to this form.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39959955
Yes you can add a dynamic control (using Me.Controls.Add) but the problem is where will you place it on the form?

check http://www.ehow.com/how_7811444_dynamically-add-control-vbnet.html
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:rcblevins
ID: 39960707
I don't think that I did a good job explaining my question.  The project I'm working on already has all of the forms created.  I was looking for an easy way to implement this without have to do code in each form.  Is there no way for the application to know what form or object it's loading and grab it then add the button to dynamically at that time?  Thanks for any help.
0
 
LVL 40
ID: 39960953
Not at the application level. This is something that has to be thought of at design time. A Form is a class same as any other class, and there is no event that enables you to detect that a class is instantiated.

If it is a MDI application however, you have a central point of control and could react to the MdiChildActivate event to know when a form is opened.

But even there, as Éric hinted, and specially with 300 of these things, you would need specific code for each form to know where to put that button.

And it would not do any good for UserControls.

The only way I think of implementing something like that after the fact would be to trap a keyboard shortcut through the Windows API SetWindowsHookEx function. There is sample code of how to do that for the mouse at http://support.microsoft.com/kb/319524. It would need to be adapted to use KeyboardProc instead of MouseProc.

You might find other examples by doing a search for "SetWindowsHookEx KeyboardProc vb.net"
0
 

Author Comment

by:rcblevins
ID: 39965202
Thanks for all your input.  I guess I will have to go back recode something in all of the forms.  I would like to go ahead and implement some inheritance at this time, but is there a way to create a base form that all of the other forms can inherit the code from and not the visual appearance?  I hope that  made sense.
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 2000 total points
ID: 39965301
Yes, you can work from a base form and inherit. That is what I suggested in my first post.

But you will have to go into the designer.vb of each of your forms to change the Inherits line that they already have and change the reference to your own form.

You could however think of the possibility of performing a project wide replace to perform that task. Since inherits can appear only once in a form, this would add the button to all the forms in your application. All you would have to manually do afterward would be to manually bring back the original Inherits in the forms that do not need the button. And also make sure that the new button is not hidden by controls that already were on the original forms.
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question