CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2016
    Posts
    10

    c# conditional formating

    Hi
    how can i format a textbox in c# ,precisely let say if the textbox value is between 100 & 200 the background color will change .
    i appreciate the help
    My regards

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: c# conditional formating

    Look at the BackColor property of the Textbox class in Msdn

  3. #3
    Join Date
    Oct 2015
    Posts
    26

    Re: c# conditional formating

    I would bind the Text to a property and bind the Background to a property

    Code:
    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApplication1"
            mc:Ignorable="d"
            Title="MainWindow" Height="80" Width="200">
        <Grid>
            <StackPanel Orientation="Horizontal" >
            <TextBox x:Name="MyTextBox" HorizontalAlignment="Left" VerticalAlignment="Center" MinWidth="100"  Margin="10,10,10,10" 
                     Text="{Binding Path=TextVal, Mode=TwoWay}"
                     Background="{Binding Path=TextBackColor, Mode=TwoWay}">
            </TextBox>
    
                <Button Content="Dummy" HorizontalAlignment="Left" VerticalAlignment="Center" />
            </StackPanel>
    
        </Grid>
    
    </Window>

    then in the code behind

    Code:
    using System;
    using System.Windows;
    using System.Windows.Media;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window, INotifyPropertyChanged
        {
            private String _textVal;
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            private Brush _textBackColor;
            public Brush TextBackColor
            {
                get { return _textBackColor; }
                set
                {
                    _textBackColor = value;
                    OnPropertyChanged();
                }
            }
    
    
            public String TextVal
            {
                get { return _textVal; }
                set
                {
                    _textVal = value;
    
                    int num;
                    if (int.TryParse(value, out num) && (num > 99) && (num < 201))
                    {
                        TextBackColor = Brushes.LightBlue;
                    }
                    else
                    {
                        TextBackColor = Brushes.Orange;
                    }
                    OnPropertyChanged();
    
                }
            }
    
            public MainWindow()
            {
                InitializeComponent();
                DataContext = this;
            }
    
            private void OnPropertyChanged([CallerMemberName] string propertyName = "")
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured