2

I've written the following to restrict a WPF textbox to only accept integers and also only accept a number less than or equal to 35:

In my WindowLoaded event I create a handler for 'OnPaste':

DataObject.AddPastingHandler(textBoxWorkflowCount, OnPaste);

OnPaste consists of the following:

private void OnPaste(object sender, DataObjectPastingEventArgs e)
{
    if (!IsNumeric(e.Source.ToString(), NumberStyles.Integer)) e.Handled = true;
}

and our function to force numerics only is as follows:

public bool IsNumeric(string val, NumberStyles numberStyle)
{
    double result;
    return double.TryParse(val, numberStyle, CultureInfo.CurrentCulture, out result);
}

The specific textbox that is having the error should also be limited to a number <=35. To do this I've added the following TextChanged event:

private void TextBoxWorkflowCountTextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                if (textBoxWorkflowCount == null || textBoxWorkflowCount.Text == string.Empty || Convert.ToInt32(textBoxWorkflowCount.Text) <= 35) return;
                MessageBox.Show("Number of workflow errors on one submission cannot be greater then 35.", "Workflow Count too high", MessageBoxButton.OK, MessageBoxImage.Warning);
                textBoxWorkflowCount.Text = "";
            }
            catch(Exception)
            {
                // todo: Oh the horror! SPAGHETTI! Must fix. Temporarily here to stop 'pasting of varchar' bug
                if (textBoxWorkflowCount != null) textBoxWorkflowCount.Text = "";
            }

        }

Although this does the job and works it's very nasty/hackish and I would love to know how it could be done better for sake of improving myself... Especially so without having to swallow an exception.

Dhaust
  • 5,470
  • 9
  • 54
  • 80
Michael A
  • 9,480
  • 22
  • 70
  • 114

2 Answers2

1

Does this work for you? Replace the content of TextBoxWorkflowCountTextChanged with this:

if (textBoxWorkflowCount == null || textBoxWorkflowCount.Text == string.Empty) return;
int workflowcount = 36;
if (int.TryParse(textBoxWorkflowCount.Text, out workflowcount) && workflowcount > 35) {
    MessageBox.Show("Number of workflow errors on one submission cannot be greater then 35.", "Workflow Count too high", MessageBoxButton.OK, MessageBoxImage.Warning);
    textBoxWorkflowCount.Text = "";
}
else if (workflowcount == 36) {
    textBoxWorkflowCount.Text = "";
}
Qi Chen
  • 1,648
  • 1
  • 10
  • 19
1

Based on of QtotheC's, answer I arrived at the following after a bit more refactoring. Adding here for future visitors :)

    private void TextBoxWorkflowCountTextChanged(object sender, TextChangedEventArgs e)
    {
        if (string.IsNullOrEmpty(textBoxWorkflowCount.Text))
            return;

        int workflowCount;

        if (!int.TryParse(textBoxWorkflowCount.Text, out workflowCount) || workflowCount <= 35) return;
        MessageBox.Show("Number of workflow errors on one submission cannot be greater then 35.", "Workflow Count too high",
                        MessageBoxButton.OK, MessageBoxImage.Warning);
        textBoxWorkflowCount.Text = "35";
    }
Michael A
  • 9,480
  • 22
  • 70
  • 114
  • you *could* combine the two conditions with || (that would reduce nesting). – Adam Aug 14 '12 at 04:33
  • @codesparkle Don't you mean && for the second if statement? Will edit / update with thatn ow. – Michael A Aug 14 '12 at 04:43
  • It's ok, just to be cheeky I inverted so I could use || :) – Michael A Aug 14 '12 at 04:45
  • You'd have a `NullPointerException` at `string.IsNullOrEmpty(textBoxWorkflowCount.Text)` if `textBoxWorkflowCount` is ever null. Not that it will matter much--this event should never fire if the textbox is null. – Qi Chen Aug 14 '12 at 16:04