Wednesday, September 25, 2013

How to make a part of the TextBox ReadOnly in WPF

The following post contains example how to make a part of textbox readonly.


Download Sample

<Window x:Class="PartialReadOnlyTextBox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Icon="Resources/Images/favicon-image.png"
        Title="Partial ReadOnly TextBox"
        Height="157"
        Width="536"
        ResizeMode="NoResize"
        ShowActivated="True"
        WindowStartupLocation="CenterScreen"
        Loaded="Window_Loaded">
    <Grid>
        <TextBox Name="txtControl" VerticalAlignment="Center" ContextMenu="{x:Null}" PreviewKeyDown="txtControl_PreviewKeyDown"/>
    </Grid>
</Window>

**********************************************************************************

using System;
using System.Windows;
using System.Windows.Input;

namespace PartialReadOnlyTextBox
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// Get the original text.
        /// </summary>
        internal String OriginalText { get; private set; }

        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handels the Loaded event of Window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <Developer>Kaushik Kumar Mistry</Developer>
        /// <DateCreated>25 Sep, 2013</DateCreated>
        /// <ModifiedBy>...</ModifiedBy>
        /// <ModifiedDate>...</ModifiedDate>
        /// <ModificationLog>...</ModificationLog>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                this.txtControl.Text = this.OriginalText = "Hello softwareKaffee.";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// Handels the PreviewKeyDown event of txtControl.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <Developer>Kaushik Kumar Mistry</Developer>
        /// <DateCreated>25 Sep, 2013</DateCreated>
        /// <ModifiedBy>...</ModifiedBy>
        /// <ModifiedDate>...</ModifiedDate>
        /// <ModificationLog>...</ModificationLog>
        private void txtControl_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (this.txtControl.CaretIndex < this.OriginalText.Length)
                {
                    e.Handled = true;
                }
                else if (e.Key == Key.Back && this.txtControl.CaretIndex == this.OriginalText.Length)
                {
                    e.Handled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}

Post a Comment