Solved

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

Posted on 2014-03-27
7
361 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

803 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