XTab's Blog

Ged Mead's Blog at vbCity

vbCity Blogs moved to:
http://cs.vbcity.com/blogs
  Home :: Syndication  :: Login

JunJuly 2009Aug
SMTWTFS
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

Archives

Topics

Ramblings

VB.NET

Friday, July 31, 2009 #

  I'm a big fan of WPF and WinForms Interop, so have often advised people to try the ElementHost when they want to do something graphically complex on a Windows Form. This useful little container allows you to create a WPF UserControl right there inside the WinForms project and then display those WPF graphics in a Windows Form.

  One of the negative things about this in VS 2008 is that you always get a kind of error message inside the ElementHost at Design Time which reads "Unable to generate a preview". It's not really an error, but it is negative feedback.

Although this message is disconcerting at first, you soon get used to it, and the WPF UserControl will display nicely once you actually run the project.

  I was hoping that VS 2010 would fix this issue and that you would be able to see the WPF content in the ElementHost right there on the Window at Design time. The good news is that you can. The bad news is that the pseudo-error has been replaced with a genuine error!

   The error message is: "Type 'WindowsFormsIntegration.Design.HostedComponent' is not available in the target framework". This message appears whether you choose Version 3.5 or 4 of the .NET Framework for the project. I was particularly surprised that I still got the error with 3.5 apps. You wouldn't have thought that a problem with v4 would be a breaking change in the earlier Framework, but this seems to be the case.

  Anyway, I did some searching and found the fix documented in the Microsoft .NET Framework 4 Beta 1 Readme. It requires an additional Reference which is tucked away in the Visual Studio 2010 sub-folder in the Program Files folder. The exact path in my installation is:

   C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\WindowsFormsIntegration.Design.dll

  You have to use the 'Browse' Tab instead of the more usual .NET tab of course to find and add this dll. You may also see a further warning message regarding the use of this dll with earlier versions of the Framework. This indicates that if you have your Framework set to 3.5 you don't need this dll (because it is a Framework 4 requirement). However, I couldn't get ElementHost to work properly in either 3.5 or 4 mode before I added this Reference, so that all left me a little confused - but happy, because it now works as it should.

  Having sorted that out, I can now see the WPF UserControl on the Windows Form at Design Time, as you can see from the screenshot below. This is much more useful because it is often necessary to tweak the layout of the UserControl or other settings to make it fit in with the layout of other Windows Forms controls on the Form. Now that I can see it at Design time, I don't have to keep running and re-running the project to see the effect of changes.

  I hope that this requirement to add the reference will be fixed by the time the final version of VS 2010 ships, but in the meantime I'll not complain about the small amount of work needed to gain the additional benefit.

  

  Don't forget that you should already have the standard:

  • PresentationCore
  • PresentationFramework
  • WindowsBase
  • WindowsFormsIntegration

References in place for this kind of Interop task.

 

posted @ 8:52 PM | Feedback (0)

  Here's a useful little tip for if you have a numeric value and you need to convert it into your local currency.

  Let's assume you take in a value as a Double and you want the output to be in the format of currency, with two decimal places (in the case of the UK to represent Pence).

  Here's the Double:

Code Copy
     Dim TotalCost As Double = 123.4567

  To show this formatted, you can use the String.Format method, a placeholder for the variable and the 'c' attribute to ensure the currency format is used:

Code Copy
       Label1.Text = String.Format("Total Cost = {0:c}", TotalCost)

  The result would look like this:

  

  Notice that it automatically rounds up for you, changing '4567' to '46'.

posted @ 8:47 PM | Feedback (0)