tags:

views:

51

answers:

3

i need to use a single button for all forms in a vb.net project. The single button should be called in the forms to perform operations.

For eg., In a form i need to save, update the records.

When i call the button, save and update buttons should be appeared at runtime using the single button. guide me....

+1  A: 

That's not possible, a control like Button can have only one Parent. Nor can you "call a button", a Button has a Click event. You write an event handler for that event, get started on that quickly by simply double-clicking the button in the designer. Such a handler could look like this:

  Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    Document.Save()
  End Sub

All of the Save buttons can call the common Document.Save() method, you'll have the "save document" logic in only one place.

Note that an MDI form would be a good approach, you can give it a ToolStrip with a Save button and a File + Save menu item so the user is never confused about how to save the document. This walkthrough shows you have to create such a user interface.

Hans Passant
+1  A: 

It sounds like you have a set of forms which will all share a common set of functionality - Save, Update, etc. You may be able to use inherited forms to do this. Have the base form include the common buttons. The derived forms will then all have the same buttons.

Even more flexible is the use of menus and toolbars in inherited forms. You can ensure that the child forms all have the same toolbar buttons, for instance.

John Saunders
A: 

You could also create a custom Interface (IMdiChildForm or something similar), that has Sub Save() Sub Update etc, and have all of the child forms implement this inteface. This way, all your Mdi Parent form has to do is call ActiveMdiChild.Save() (with some defensive code around to make sure the child supports that interface) on the Button_Save event handler. Implement the save functionality on a per child form basis and add a toolstrip to the MdiParent and your sorted. Hope this helps.

Ben