user2481095
user2481095

Reputation: 2106

How to Share Common Groups of Controls in Winforms C#

The majority of my forms for my project include an OK and Cancel button. (always positioned at the bottom right of the form). Currently I have a base class that inherits from System.Windows.Forms which contains an OK and Cancel button. All forms that use this then inherit from this base form class. Is there a better way of doing this that takes localization into consideration?

Upvotes: 0

Views: 338

Answers (3)

Mazen Alsenih
Mazen Alsenih

Reputation: 101

Doing form inheritance is very useful in many levels:

  1. Make a base form, and name it for ex: FrmBase.
  2. Add the Ok, Cancel Buttons to it and set the Anchor property for both to Bottom.
  3. Set the Buttons "Modifiers" property to "Internal", this way you can access these buttons from inherited forms:
  4. Make as many forms as you want and make each inherit from the FrmBase ex: Form1 : FrmBase
  5. now you can access the buttons from this from, using the properties.

Hope this being useful for you.

Upvotes: 1

PiranhA
PiranhA

Reputation: 164

You could just create a single form that has an empty panel or table layout, where you dynamically load the desired user control. It is basically the composition over inheritance principle.

public partial class MyFormWithButtons : Form
{
  public MyFormWithButtons(UserControl control)
  {
    InitializeComponent();

    control.Dock = DockStyle.Fill;
    myPanel.Controls.Add(control);
  }
}

Upvotes: 1

Jayanta Patra
Jayanta Patra

Reputation: 56

I would use MDI Child Forms for this. Parent Form Can contain OK/Cancel button where as you would have your child form in MDI container.

For More help visit https://msdn.microsoft.com/en-us/library/aa984329(v=vs.71).aspx

Upvotes: 1

Related Questions