Reputation: 447
I have a 3rd party code library that I'm using; part of this library is a winforms application for editing the configuration files used by this library. I would like to embed their configuration editor app into my application.
I have the source code to their library and the configuration editor is (as far as I can tell) a straight forward Winforms app using standard controls. I'm trying to convert the app's main form into a UserControl so that I can host it inside my application which is WPF (WPF's WindowsFormsHost won't host a Form object, I get an exception).
I changed the form object to inherit from UserControl instead of Form and fixed all the compiler errors (there weren't many, just property initializations that don't exist on UserControls) but what's happening is my newly converted control is just blank.
When I run my test app I don't see any of the controls that make up the original form, just a blank page.
Any ideas? I really don't want to have to re-implement their app from scratch, that would suck.
Edit: I forgot to mention I'm testing this in a WinForms application, not WPF, to just get the control working before trying to use it from WPF.
Upvotes: 4
Views: 14400
Reputation: 51
As long as you fix within the designer code any reference to the Form class (comment out incase you need to rollback) just change the class declaration from : Form to : UserControl. Any form specific event handlers would have to be removed. I have been doing this with a few forms and its worked just fine.
Upvotes: 0
Reputation: 1
I had a similar problem and only addig controls in InitializeComponent was missing. like this
this.Controls.Add(this.button1);
this.Controls.Add(this.button2);
Upvotes: 0
Reputation: 447
I have no idea why, but their InitializeComponent() method was basically setting all the controls Visible properties to False. I guess it must have used some form startup event to change them to visible that doesn't get called as a UserControl, setting their visibilities back to true fixed it.
Upvotes: 1
Reputation: 941217
Clearly the InitializeComponent() method isn't running properly anymore. Not sure why of course.
Perhaps the better approach is to turn the form in a control:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
var f2 = new Form2();
f2.TopLevel = false;
f2.Location = new Point(5, 5);
f2.FormBorderStyle = FormBorderStyle.None;
f2.Visible = true;
this.Controls.Add(f2);
}
}
Upvotes: 6