Solved

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

Posted on 2014-03-27
7
375 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

738 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