JG Vimalan's Blog

It is all about coding!

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

Advertisements

November 23, 2010 - Posted by | XAML

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: