locked
Make a form read only RRS feed

  • Question

  • What is the easiest way of making all controls on a form read only?

    Previously I set IsEnabled = false on an object that contains the entire form, which works all the way up/down the control tree.  This is very simple, but I've had usability issues with the greyed out text - its just too hard read.

    So, is there an easy way to change the greyed out text color, or is there another way to stop the user from making changes to a form?
    Monday, March 30, 2009 4:07 AM

Answers

  • Did you know that a lot of controls (Textbox, combobox) have a .IsReadOnly property on them?

    If you just set them on each control using a property setter as a resource that should
    do the job.  No more greyed out text :)

        <Style TargetType="TextBox">
            <Setter Property="IsReadOnly" Value="true" />
        </Style>
    
    • Proposed as answer by clockwise_music Tuesday, March 31, 2009 4:02 AM
    • Marked as answer by Hua Chen Tuesday, March 31, 2009 9:15 AM
    Monday, March 30, 2009 5:37 AM
  • Hi,

    One solution is change the system color for different cases. For example.

    <Window x:Class="WpfBinding.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
        <Window.Resources>
            <SolidColorBrush Color="Green"  x:Key="{x:Static SystemColors.GrayTextBrushKey}"/>
        </Window.Resources>
        <StackPanel>
            <TextBox Text="Hello" IsEnabled="False" Width="100" Height="20"/>
        </StackPanel>
    </Window>
    In my sample, I changed the SystemColors.GrayTextBrushKey to Green color. And TextBox in disbaled state will use SystemColors.GrayTextBrushKey to draw text. So this achieve you requirement.

    The disadvantage is might be others controls will use other SystemColors key as its gray text color. This means you might need to define servals different system colors.

    Hope this helps.
    Yiling, MVP(Visual C++)
    • Marked as answer by Hua Chen Tuesday, March 31, 2009 9:15 AM
    Tuesday, March 31, 2009 8:24 AM

All replies

  • Did you know that a lot of controls (Textbox, combobox) have a .IsReadOnly property on them?

    If you just set them on each control using a property setter as a resource that should
    do the job.  No more greyed out text :)

        <Style TargetType="TextBox">
            <Setter Property="IsReadOnly" Value="true" />
        </Style>
    
    • Proposed as answer by clockwise_music Tuesday, March 31, 2009 4:02 AM
    • Marked as answer by Hua Chen Tuesday, March 31, 2009 9:15 AM
    Monday, March 30, 2009 5:37 AM
  • Apologies, I meant programatically.

    I read some similar posts in this forum, and noted people suggested they post in the Windows Forms General forum instead - so I created a topic there:

    http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/95cef902-a9d4-454e-af62-1927c34d2cd6/

    It seems to be getting more replies, that are more relevant.
    Tuesday, March 31, 2009 7:17 AM
  • Hi,

    One solution is change the system color for different cases. For example.

    <Window x:Class="WpfBinding.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
        <Window.Resources>
            <SolidColorBrush Color="Green"  x:Key="{x:Static SystemColors.GrayTextBrushKey}"/>
        </Window.Resources>
        <StackPanel>
            <TextBox Text="Hello" IsEnabled="False" Width="100" Height="20"/>
        </StackPanel>
    </Window>
    In my sample, I changed the SystemColors.GrayTextBrushKey to Green color. And TextBox in disbaled state will use SystemColors.GrayTextBrushKey to draw text. So this achieve you requirement.

    The disadvantage is might be others controls will use other SystemColors key as its gray text color. This means you might need to define servals different system colors.

    Hope this helps.
    Yiling, MVP(Visual C++)
    • Marked as answer by Hua Chen Tuesday, March 31, 2009 9:15 AM
    Tuesday, March 31, 2009 8:24 AM