Thursday, August 17, 2006

IE doesn't respect set-only properties in hosted Windows Forms controls

While working on a simple Windows Forms control to determine how Windows XP styles are supported in Windows Forms 2.0 controls hosted in Internet Explorer -- more on that later -- I found that Internet Explorer 6 doesn't respect public properties of hosted/embedded controls that define a "set" but not a "get".

I had a property like this in my test control:

public bool UseRedBackgroundColor
{
  set
  {
    if (value == true)
    {
      this.BackColor = Color.Red;
    }
  }
}

I had a param set in the object tag in my test HTML page to set the property to true, but the control didn't render with the red background:

<object id="XPTest" name="XPTest"
classid="http:/webtest/XPStylesTest.dll
#XPStylesTest.XPStylesTest"
    width="158" 
    height="250">
  <param name="UseRedBackgroundColor" value="True" />
</object>

I changed my public property to have a "get" section, and after a rebuild of the project and a reopen of IE, the control rendered with a red background as expected:

public bool UseRedBackgroundColor
{
  get
  {
    return (this.BackColor == Color.Red);
  }
  set
  {
    if (value == true)
    {
      this.BackColor = Color.Red;
    }
  }
}

So if a public property of a Windows Forms control hosted in IE isn't being activated as expected, check to make sure that a "get" section has been defined for the property.

No comments:

Post a Comment

Non-spammers: Thanks for visiting! Please go ahead and leave a comment; I read them all!

Attention SPAMMERS: I review all comments before they get posted, and I REPORT 100% of spam comments to Google as spam! Why not avoid getting your account banned as quickly -- and save us both a little time -- by skipping this comment form and moving on to the next one on your list? Thanks, and I hope you have a great day!