Solved

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

Posted on 2014-03-27
7
348 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 69

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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 500 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

760 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now