Validation Problem

Page 1 of 1

7 Replies - 1085 Views - Last Post: 19 September 2011 - 03:23 AM Rate Topic: -----

#1 falcon eyes  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 53
  • Joined: 16-April 07

Validation Problem

Posted 15 September 2011 - 05:16 AM

Hi everybody
I'm writing a timer application,and my program logic as below

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;

namespace Timer
{
   
    /// <summary>
    /// Interaction logic for Mainwindow.xaml
    /// </summary>
    public partial class MainWindow : Window,System.ComponentModel.INotifyPropertyChanged
    {
      
        enum State {Start,Stop,Reset }
        
       
        State _timrState;
        
        System.Timers.Timer secTimer;
       
        int _sec;
        int _min;
        int _ahr;
        public MainWindow()
        {
            InitializeComponent();
            
            secTimer = new System.Timers.Timer(1000);       //every sec
           
            
            secTimer.Elapsed += new System.Timers.ElapsedEventHandler(secTimer_Elapsed);
          
            
            _sec = 0;
            _min = 0;
            _ahr = 0;
            DataContext = this;

          
        }

        

        
        void secTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
                     
            Sec++;
        }

        public int Sec
        {
            get { return _sec; }
            set { _sec = value; onpropertychanged("Sec"); }
        }

        public int Min
        {
            get { return _min; }
            set { _min = value; onpropertychanged("Min"); }
        }

    public int Hour     
    {
            get { return _ahr; }
        set { _ahr = value; onpropertychanged("Hour"); }
        }


        private void stoptbut_Click(object sender, RoutedEventArgs e)
        {
            TimersState(State.Stop);
        }

        private void startBut_Click(object sender, RoutedEventArgs e)
        {
            TimersState(State.Start);
        }

        private void resetButt_Click(object sender, RoutedEventArgs e)
        {
            TimersState(State.Reset);
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        void onpropertychanged(string probName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(probName));
        }

        void TimersState(State st)
        { 
          switch (st)
          {
              case State.Start:
                  StartTimers();
                        break;
              case State.Stop :
                        StopTimers();
            break;
              case State.Reset:

                    StopTimers();
                      
                    StartTimers();
            break;

          }
        }

        void StartTimers()
        {
            secTimer.Start();
         
        }

        void StopTimers()
        {
            secTimer.Stop();
          
            Sec = 0;
            Min = 0;
            Hour = 0;
        }

       

        private void secTxtBox_Error(object sender, ValidationerrorEventArgs e)
        {
            if (e.Action == ValidationerrorEventAction.Added)
                Sec = 1;
        }
    }
}



and i have bind the text property of a Textbox with the property "Sec" and created a validation rule as below

<TextBox x:Name="secTxtBox"   Grid.Row="1" Grid.Column="2" Margin="10,2" VerticalAlignment="Center"  Validation.Error="secTxtBox_Error" >
            <TextBox.Text>
                <Binding Path="Sec" Mode="TwoWay" ValidatesOnDataErrors="True" UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                        <local:SecTimerValidation Check="Sec"></local:SecTimerValidation>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>



and my validation class as below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;

namespace Timer
{
    public enum CheckFor { Sec, Min, Hour }
    class SecTimerValidation : ValidationRule
    {
        public CheckFor Check { get; set; }
        int _secval;
        ValidationResult valRes ;
     public  SecTimerValidation()
        {
            _secval = 0;
            valRes = null;
        }
     public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
     {
         switch(Check)
         {
             case CheckFor.Sec:
              _secval=Int32.Parse( value.ToString());
                
             if (_secval > 60)
                    valRes = new ValidationResult(false, "Seconds error");
                else
                    valRes = new ValidationResult(true,null);
                 break;
                   
        }
         return valRes;
     }
    }
}



my problem is when the counter exceeds the value 60 it continue to increment and the validation not checked and when i steped through the program i found that the validation not executed at all,
if any one tell me why i will be appreciate

Is This A Good Question/Topic? 0
  • +

Replies To: Validation Problem

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Validation Problem

Posted 15 September 2011 - 05:23 AM

Just one note.. Your properties are not setup correctly for the INotifyPropertyChanged. You have this...

public int Sec
{
     get { return _sec; }
     set { _sec = value; onpropertychanged("Sec"); }
}



So what happens if I keep setting the Sec property to 1? It will always fire the onpropertychanged event, and that's not what you want. You only want to fire the event when the value has actually changed. So add this code to each of your properties...

public int Sec
{
     get { return _sec; }
     set 
     { 
        if(value != _sec)
        {
             _sec = value; 
             onpropertychanged("Sec"); 
        }
     }
}



Now the event will only be fired when the value is set to something different from the current value.

I am getting your code now to test out your issue.
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Validation Problem

Posted 15 September 2011 - 06:01 AM

The problem is in your XAML.

You need to change this...
<local:SecTimerValidation Check="Sec"></local:SecTimerValidation>



to this...

<local:SecTimerValidation Check="Sec" ValidatesOnTargetUpdated="True" />



Notice that I added this ValidatesOnTargetUpdated="True" to the validation.
Was This Post Helpful? 1
  • +
  • -

#4 falcon eyes  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 53
  • Joined: 16-April 07

Re: Validation Problem

Posted 15 September 2011 - 07:52 AM

thanx
it it works but not fire Validation.Error event of the TextBox
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Validation Problem

Posted 15 September 2011 - 07:59 AM

I'm not sure what that means. What are you wanting to happen?
Was This Post Helpful? 1
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Validation Problem

Posted 15 September 2011 - 01:38 PM

After re-reading your post, I see what you mean now. The secTxtBox_Error event wasn't getting fired. That's because you didn't tell the binding to notify when a validation error occurs.

<TextBox.Text>
    <Binding
        Path="Sec"
        Mode="TwoWay"
        TargetNullValue="0"
        FallbackValue="0"
        ValidatesOnDataErrors="True"
        NotifyOnValidationerror="True"
        UpdateSourceTrigger="PropertyChanged">
        <Binding.ValidationRules>
            <local:SecTimerValidation
                Check="Sec" ValidatesOnTargetUpdated="True" />
        </Binding.ValidationRules>
    </Binding>
</TextBox.Text>



Notice that I added the NotifyOnValidationerror="True".
Was This Post Helpful? 2
  • +
  • -

#7 falcon eyes  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 53
  • Joined: 16-April 07

Re: Validation Problem

Posted 18 September 2011 - 04:12 AM

thanks it work fine,but i have another question?

i have added another property for mil seconds(to count in mil second 1/1000 sec) and called it MSec
when i set the time interval to 1000ms and disables MSec the timer work accurately and the seconds counter work fine
but when i set the time interval to 1ms and enable MSec property i didn't obtain an accurate result for the seconds counter
and i don't know why,
do u have an idea about it?
Was This Post Helpful? 0
  • +
  • -

#8 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 641
  • Joined: 07-October 08

Re: Validation Problem

Posted 19 September 2011 - 03:23 AM

Yes. It is because when it is set at 1000ms the program only has to handle the event firing every 1 second. When it is 1ms it has to handle it 1000 times every second, or 1000 times more than when it is set at 1000ms. This means the computer has to do 1000x more work, atleast in theory, so it could and probably will lag. Afterall, all a timer is saying is "wait at least x ms and then run the code", and not "run this code bang on every x ms".

This post has been edited by ragingben: 19 September 2011 - 03:25 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1