Offer
Offer

Reputation: 630

restricting alphabetic charecters typed into textbox from class

Iv'e got a code that restricts textbox input to numbers, dashes and spaces.It works fine in the window that contains the textbox.

    public static bool IsTextAllowed(string text)
    {
        //regex that matches disallowed text
        Regex regex = new Regex("[^0-9,-]+");
        return !regex.IsMatch(text);
    }

    private void textbox1_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        e.Handled = !IsTextAllowed(e.Text);
    }

However what I wish to do is put the code into a class so I can access it from multiple windows and pages. That's the part that isn't going so well.

I created a class called 'rules' and copied the bool method into it. I couldn't figure out a way to get the event handler to work in the class though so instead i tried to pass the string value produced by the method to another string, and bind it to 'textbox1'. Here's the code.

public class rules : INotifyPropertyChanged
{

    // String to contain passed on value from the regrex code
    string integers;

    //Method to pass on value to the string 'integers'
    public bool this[string Digits]
    {
        get
        {
            //regex that matches disallowed text
            Regex regex = new Regex("[^0-9,-]+");
            return !regex.IsMatch(integers);
        }
    }

    //Binding
    public string Number
    {
        get
        {
            return integers;
        }
        set
        {
            if (integers != value)
            {
                integers = value;
                RaisePropertyChanged("Number");
            }
        }
    }

    private void RaisePropertyChanged(string prop)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged (this, new PropertyChangedEventArgs(prop));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

In the code behind the window I have 'textbox1' in I set the datacontext.

    public signup()
    {
        InitializeComponent();
        Rules = new rules();
        this.DataContext = Rules;
    }

Then I bind 'textbox1' to it in the xaml:

    <TextBox x:Name="textbox1" Text="{Binding Number, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" />

Evidently I'm doing something wrong as it accepts any character punched in.

Is there anyway around this?

Upvotes: 0

Views: 65

Answers (1)

paparazzo
paparazzo

Reputation: 45096

In the preview you are cancelling the input with the e.Handled

In the setter you are allowing the input
The TextBox still has value - you have done nothing to reset value

This should work
By calling RaisePropertyChanged("Number"); it should reset back if !IsTextAllowed(value)

if (integers == value) return;
if (IsTextAllowed(value)) { integers = value; }
RaisePropertyChanged("Number");

Upvotes: 1

Related Questions