[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to Close Form with Wpf User Control in Element Host

Posted on 2010-09-08
7
Medium Priority
?
2,439 Views
Last Modified: 2013-11-27
I have a windows form  that has Wpf Content inside. I would like to put a close button in wpf user control to close windows form. Is there any easy way to do this?
Thanks in advance.
0
Comment
Question by:Zherquen
[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
  • 3
  • 3
7 Comments
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33625371
Expose an event within your wpf usercontrol and subscribe to that in the windows form.

Close the windows form on trigger.
0
 
LVL 10

Expert Comment

by:james-ct16
ID: 33625604
Howdy

An alternative to the above suggestion would be to create an interface eg iWindowsFormsHostedActions that contains definitions of any number of actions you may want to perform from the wpf control on you winforms host ie your close member.

Your winforms host then implements this interface. Your wpf controls have a property added to them that is of type iWindowsFormHostedAction (good idea here to be consistent with implementing another interface or create yourself an abstract class for consistency and interoperability). So after you create the instance of your wpf control in the winform form you also set the new property = this

At this point you can then simply call the close method on the iWindowsFormHostedAction property.

This method involves just a touch more work upfront but once you have set it up you can add additional functionality very quickly and easily.

Hope that helps

James
0
 

Author Comment

by:Zherquen
ID: 33630309
Thanks for detailed description James. I see what you mean but not totally. Can you show me that with an example please?
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 10

Expert Comment

by:james-ct16
ID: 33633361
Howdy

Sorry its taken a little while to get back to you, attached you should find some rough pseudo code providing an example of the above description. Im happy to provide any more help you may need.

James
// this is our shared interface for performing actions on our windows host
public interface iWindowsFormsHostedActions
{
  void CloseWindow();
}

// interface to impliment on wpf control
public interface iWpfInteraction
{
  iWindowsFormsHostedActions WinFormsHost {get;set;}
}

//winforms form
public class MainWindow: Form, iWindowsFormsHostedActions
{

public MainWindow()
{
// I assume the following is similiar to how you are adding your wpf control
System.Windows.Forms.Integration.ElementHost host = new System.Windows.Forms.Integration.ElementHost();
System.Windows.UIElement obj = new WpfControl();
((iWpfInteraction)obj).WinFormsHost = this;
host.Child = obj;
host.Dock = DockStyle.Fill;
this.Controls.Add(host);
}

public void CloseWindow()
{
this.Close();
}
}

public class WpfControl : UserControl, iWpfInteraction,iWindowsFormsHostedActions
{
WindowsFormsHostedActions WinFormsHost 
{
get
{
// get logic
}
set
{
// set logic
}
}

public void CloseWindow()
{
if (WinFormsHost != null)
{
WinFormsHost.CloseWindow();
}
}

}

Open in new window

0
 

Author Comment

by:Zherquen
ID: 33635350
Thanks for your reply James. While building your sample it get an error like this.
Missing partial modifier on declaration of type 'WpfOnForms.WpfControl'; another partial declaration of this type exists

I generate the  public class WpfControl inside WpfControl.xaml.cs (Changed  WindowsFormsHostedActions --> iWindowsFormsHostedActions )

Where might the error raised from?

Thanks In advance
0
 
LVL 10

Accepted Solution

by:
james-ct16 earned 2000 total points
ID: 33636146
Good evening

Sorry maybe an element of misunderstanding here, WpfControl was just a pseudo example of a wpf control. With any luck now I have access to a dev environment here should be a complete worked example. You should be able to download it from here http://www.kobler.com.au/WpfInteractionExample.zip, Please let me know once you have a copy as Ill remove it.



James
0
 

Author Closing Comment

by:Zherquen
ID: 33638358
Good Evening James,
I have download the example and complied. It is working as intended :). This example is going to improve my programming quality by the way. Thanks for your advices and helps :) .
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

649 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