Search...

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<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>

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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());
            }
        }
    }
}

No comments: