Skip to main content

Microsoft Silverlight

Answered Question Have a clicked button make itself invisible gives an HRESULT E_FAIL error.RSS Feed

(0)

timstallc
timstallc

Member

Member

260 points

36 Posts

Have a clicked button make itself invisible gives an HRESULT E_FAIL error.

Hello,

This is a very reproducible bug. Make a new silverlight project. On the page.xaml, add a button. In that button's click event, set the button itself to invisible. Clicking the button will throw an exception (the button's click event passes, but the exception is caught in the Application_UnhandledException method in App.xaml):

Message "Error HRESULT E_FAIL has been returned from a call to a COM component."

at MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData)
at System.Windows.DependencyObject.MethodEx(String methodName, CValue[] cvData)
at System.Windows.UIElement.ReleaseMouseCapture()
at System.Windows.Controls.Primitives.ButtonBase.ReleaseMouseCaptureInternal()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(Object sender, MouseButtonEventArgs e)
at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

This seems too obvious a bug, so maybe there's something messed up with my install or machine - but everything else in Silverlight is working great.

Here's my exact code:

Page.xaml:

<UserControl x:Class="ErrorMouseButton.Page"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">

<Canvas>
  <StackPanel>
    <TextBlock x:Name="Txt1" Text="Hello World"></TextBlock>
    <Button x:Name="Btn1" Content="Click Me" Click="Button_Click" />
  </StackPanel>
</Canvas>
</UserControl>
 

Page.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ErrorMouseButton
{
  public partial class Page : UserControl
{
public Page()
{
  InitializeComponent();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
  this.Txt1.Text += ".";
  this.Btn1.Visibility = Visibility.Collapsed;
}
}
}
 

Tim Stall
http://timstall.dotnetdevelopersjournal.com

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Answered Question

Re: Have a clicked button make itself invisible gives an HRESULT E_FAIL error.

Hello, thanks for reporting. This is a known issue. Button will need to release mouse capture on its MouseLeftButtonUp event handler. If Visibility is Collapsed or IsHitTestVisible is false, ReleaseMouseCapture will throw an exception. We'll fix it in the next public beta. For now, can you set Opacity or set size to 0 instead?

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

timstallc
timstallc

Member

Member

260 points

36 Posts

Re: Have a clicked button make itself invisible gives an HRESULT E_FAIL error.

Ok, good to know. I actually took a different approach:

    public void HideButton(Button b, bool blnShouldShow)
    {
      if (blnShouldShow)
      {
        b.Opacity = 1;
        b.IsEnabled = true;
      }
      else
      {
        b.Opacity = 0;
        b.IsEnabled = false;
      }
    }

setting opactity is insufficient because you can still click a completely transparent button, so I set the Enable property as well.

Thanks

 

Tim Stall
http://timstall.dotnetdevelopersjournal.com

madmath
madmath

Member

Member

16 points

5 Posts

Re: Have a clicked button make itself invisible gives an HRESULT E_FAIL error.

Another solution is to put the button in a canvas and hide the canvas, it worked for me.

Mathieu Garstecki
Intern at Winwise

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities