Page view counter
Have a clicked button make itself invisible gives an HRESULT E_FAIL error.
Last post 03-28-2008 6:50 AM by madmath. 3 replies.
Sort Posts:
03-14-2008 11:43 PM
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

timstallc

Loading...
Joined on 11-17-2007
Posts 36
03-17-2008 4:43 AM
Marked as Answer
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.

Yi-Lun Luo - MSFT

Loading...
Joined on 10-29-2007
Posts 2,688
03-21-2008 10:25 PM
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

timstallc

Loading...
Joined on 11-17-2007
Posts 36
03-28-2008 6:50 AM
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

madmath

Loading...
Joined on 03-28-2008
Posts 5
Microsoft Communities