Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
387 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

660 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