JG Vimalan's Blog

It is all about coding!

SEHException was unhandled by user code

The ‘SEHException was unhandled by user code (External component has thrown an exception) ‘ was displayed in my WPF application at runtime as shown below,

This happened in a line of code which tries to create an instance of a class. After analyzing, I found that, the particular .xaml page loads videos and the necessary codec is not installed. So, after installing the codec (klcodec490f) and restarting the system, the WPF application worked fine.

March 7, 2011 Posted by | XAML | Leave a comment

Auto scroll button text on focus in WPF

There may be situations where you need to set button text dynamically.  The button text may be lengthier than expected and full text may not be visible. At this point, one of the best option is to auto scroll the text when user places the mouse on the button. Here is the code sample on how I did that in my wpf application.

XAML

<Window x:Class=”WpfApp.Window1″
    xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation
    xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml
    Title=”Window1″ Width=”500″ Height=”100″>
    <Grid>
        <Grid x:Name=”grdButton” Margin=”0,0,47,0″ >
            <Grid>
                <Rectangle
                    VerticalAlignment=”Stretch”
                    Stroke=”#FFCCCCCC”
                    StrokeLineJoin=”Round”
                    StrokeThickness=”2″
                    RadiusX=”10″
                    RadiusY=”10″
                    Margin=”6″ />

                <Grid x:Name=”grdFocus”>
                    <Grid.RowDefinitions>
                        <RowDefinition Height=”0.46*”/>
                        <RowDefinition Height=”0.54*”/>
                    </Grid.RowDefinitions>

                    <Rectangle Grid.RowSpan=”2″
                               Margin=”-5″
                               RadiusX=”12″
                               RadiusY=”12″
                               StrokeThickness=”0″>
                        <Rectangle.Fill>
                            <SolidColorBrush Opacity=”0.2″ Color=”DarkBlue”/>
                        </Rectangle.Fill>
                    </Rectangle>
                    <Rectangle x:Name=”recFocus” RadiusX=”12″ RadiusY=”12″ Grid.RowSpan=”2″>
                        <Rectangle.Fill>
                            <LinearGradientBrush StartPoint=”0.682896,1.05319″ EndPoint=”0.682896,0.0212779″>
                                <GradientStop Color=”DarkBlue” Offset=”0″/>
                                <GradientStop Color=”DarkBlue” Offset=”0.561″/>
                                <GradientStop Color=”#FFFFA300″ Offset=”1″/>
                                <GradientStop Color=”#FFFF801F” Offset=”0.235″/>
                            </LinearGradientBrush>
                        </Rectangle.Fill>
                    </Rectangle>

                    <Path Fill=”White” Stretch=”Fill” Opacity=”0.229995995759964″ Data=”F1 M709.168,234.827 L95.5,234.827 95.5,215.83225 C95.5,205.98525 100.70132,201.5 108.52932,201.5 L697.4544,201.82573 C705.2794,201.82573 709.50651,208.26538 709.50651,218.11238 z” Margin=”2″/>

                </Grid>

                <Label x:Name=”lblMessageText” Content=”This is a wpf application. This sample text will scroll automatically on mover hover.”   Foreground=”White” FontWeight=”Bold”
                       VerticalAlignment=”Center” FontSize=”18″ HorizontalAlignment=”Left” Margin=”10,0,0,0″ Width=”465″/>                       

                <Button x:Name=”btnOption” Opacity=”0″ Cursor=”Hand” MouseEnter=”btnOption_MouseEnter” 
           MouseLeave=”btnOption_MouseLeave” GotFocus=”btnOption_GotFocus” LostFocus=”btnOption_LostFocus”/>
            </Grid>
        </Grid>       
    </Grid>
</Window>

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfApp
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private DispatcherTimer timer;
        private int _textScrollLength = 28;
        private string _displayText = string.Empty;

        public Window1()
        {
            InitializeComponent();
            _displayText = lblMessageText.Content.ToString();
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 0, 0, 100);
            timer.Tick += new EventHandler(timer_Tick);
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            try
            {
                if (lblMessageText.Content.ToString().Length == 0)
                    lblMessageText.Content = ”                                                  ” + _displayText;
                else
                    lblMessageText.Content = lblMessageText.Content.ToString().Substring(1);
            }
            catch { }
        }

        private void BtnGotFocus()
        {
            try
            {
                grdFocus.Visibility = Visibility.Visible;
              
                if (!string.IsNullOrEmpty(_displayText))
                {
                    if (_displayText.Length > _textScrollLength)
                    {
                        if (timer != null)
                        {
                            timer.Start();
                        }
                    }
                }
            }
            catch { }
        }

        private void BtnLostFocus()
        {
            try
            {               
                if (timer.IsEnabled)
                {
                    lblMessageText.Content = _displayText;
                    timer.Stop();
                }
            }
            catch { }
        }

        private void btnOption_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            BtnGotFocus();
        }

        private void btnOption_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            BtnLostFocus();
        }

        private void btnOption_GotFocus(object sender, RoutedEventArgs e)
        {
            BtnGotFocus();
        }

        private void btnOption_LostFocus(object sender, RoutedEventArgs e)
        {
            BtnLostFocus();
        }
    }
}

Output:

The complete source code is available for download at http://cid-670f5e7529eed46d.office.live.com/self.aspx/.Public/WPF/WpfApp.zip

November 23, 2010 Posted by | XAML | Leave a comment

Set hotkey for buttons in XAML application at runtime

I got a task where I need to set hotkeys for the buttons generated based on the records displayed in a grid. Each record displayed in the grid will have a button. The first record which have a button whose shortcut key will be “shift 0”.  Similarly, for every records displayed, a button will be  created whose hotkey will be “shift 1, shift 2… upto shift 9 respectively. So, when the user presses shift 1, the corresponding button’s click event will be invoked. To achieve this, follow the steps given below,

a. Add a string array as shown below in your application. Here, I am using a XAML application for performing this example.

string[] appShiftKey = new string[10] { “)”, “!”, “@”, “#”, “$”, “%”, “?”, “&”, “*”, “(” };

These symbols are available in the keyboard and they were ordered to represent the numbers 0,1, 2, 3..  9 in the keyborad.

b. In the XAML button control, use the content property to set the hotkey.

public string myButtonHotKey
        {
            get { return submitButton.Content.ToString(); }
            set { submitButton.Content = value; }
        }

c. In the code, set the hotkey while iterating throught the records. Limit the record display to 10 in the grid to achieve this fuctionality.

myButtonHotKey = string.Format(“_{0}”, appController.appShiftKey[itemIndex])}

d. The outcome will be as follows, when you click the respective hotkeys, the buttons click event will fire and you can perform desired acition in that event.

November 14, 2010 Posted by | XAML | Leave a comment

The file or folder “…” is not a valid Blend project.

I got this error message when I tried to open a XAML file created using a VS 2008 WPF project in Expression Blend 3. My OS is Windows 7.

So, to resolve the issue, I opened the Expression Blend 3 application and browsed to the solution (not the project) of my WPF application. Now, I was able to open the WPF application solution using Expression Blend and edit my XAML files.

November 13, 2010 Posted by | XAML | Leave a comment