Loading .Net2.0 Form onto .Net3.0 Window






1.44/5 (4 votes)
To load a Windowsform of .Net2.0 onto .Net3.0 Window
Introduction
This article explains us about how to load a windowsform that is .Net framework 2.0 based to load it on your window based on .Net framework 3.0 or WPF.
Background
You need to aware of using the Windows form at the basic level only. I only added a simple form that loads a Calendar.
Using the code
The code is very simple to use and understand. I added comments to the code as much as i can to explain about it.
Let see how we can acheive it? Here, firstly I made a small application based on Window Forms 2.0 and created a dll and named it as 'WindowsForm_2_0'of it. I am not attaching that code as I imagine that you can create a simple window form dll that contains a winForm and some controls on it. I took a simple calendar and a Label mentioning the heading of the form. I set the FormBorderStyle to None as I am uploading that form onto my WPF Window. I provided only the dll that is attached to my WPF Window application.
Now, i created a simple .Net Framework 3.0 application and named my application as 'Window_3_0'. From the Toolbox I had drag and dropped the StackPanel onto my window and named it as 'stackPnl'.
Now, we need to add three references to your application. One would be 'System.Windows.Forms', where we need to have to access all the properties of the Form that we are loading, 'WindowsFormsIntegration' , we will discuss below while using it why we are adding this reference and the dll that we created earlier as discussed i.e., 'WindowsForm_2_0'.
We add the reference to you application and create an object of your form.
WindowsForm_2_0.WinForm formObj = new WinForm();
Now, lets write a few lines of code. We create a function named 'Intialiseform' which load the form to your Window. I created a host that is used to load the form as below. Now, later I set the stackpanel width and height as of the WindowsForm so that the form data may not me missed. Here, we used a term named TopLevel and that is set to False because this feature to tell the application that the form that is loading is the mainForm of the sub-form. Here, the form that we took is sub-form so we are setting it to false. Next, we are adding the form to the host and then host to the stackpanel. We need to know one thing that we cannot upload the form directly to the stackpanel, we need to load it by means of a host.
void Intialiseform() { try { //creating a host to load the form System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost(); //assigning the width and height of the form to the stackpanel so that the whole //data get displayed. stackPnl.Width = (int)formObj.Width; stackPnl.Height = (int)formObj.Height; //Disabling the TopLevel so that it may not act as the MainForm //if you don't assign this application throws an exception. formObj.TopLevel = false; //loading the winForm to the host. host.Child = formObj; //loading the host to the stackpanel. this.stackPnl.Children.Add(host); } catch (Exception ex) { //throws the exception message to the message box MessageBox.Show(ex.ToString()); throw; } }
That's all we are done. Run your application which will load your windows Form onto your Window. General, error you may face is that if you don't set the form(2.0) toplevel to false your code will throw an exception 'child control cannot be a top-level form'.
Points of Interest
This a very simple but a very good feature that is provided by the microsoft to use your old Windowform 2.0 to use in windows 3.0 by means of 'WindowsFormsIntegration'. This feature will be mainly useful when you like to draw something on your window. As, i feel that people are more convinient on drawing on forms than windows this would be useful for us.