Skip to main content
Home Forums Silverlight Programming Report a Silverlight Bug Have a clicked button make itself invisible gives an HRESULT E_FAIL error.
3 replies. Latest Post by madmath on March 28, 2008.
(0)
timstallc
Member
260 points
36 Posts
03-14-2008 11:43 PM |
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;}}}
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)
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;}}}
Yi-Lun L...
All-Star
25052 points
2,747 Posts
03-17-2008 4:43 AM |
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?
03-21-2008 10:25 PM |
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
madmath
16 points
5 Posts
03-28-2008 6:50 AM |
Another solution is to put the button in a canvas and hide the canvas, it worked for me.
Mathieu GarsteckiIntern at Winwise