0

I have written a little program. It works almost as I want, I just have one problem. I am trying to copy all functions I have found in the other program.

I have a TextBox, when the user can write a phone number. Firstly, he is only allowed to use digits and "+"and "-", so I use:

    private void textBoxPhoneNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((char.IsDigit(e.KeyChar) == false) && (e.KeyChar != '+') && (e.KeyChar != '-') && (e.KeyChar != '\b')) e.Handled = true;
        }

Then I want the phone number to be in certain format (+12-34-1234567), so I use:

    private bool IsPhoneNumberCorrect(string name)
        {
            return Regex.IsMatch(name, @"^+\+[0-9]{2}-[0-9]{2}-[0-9]{7}$", RegexOptions.None);
        }

and finally this (with TextChange):

    private void phoneNumberValidity(object sender, EventArgs e)
    {
        counter4 = Convert.ToInt32(IsPhoneNumberCorrect(textBoxPhoneNumber.Text));
        pictureBoxPhoneNumber.Image = imageList1.Images[counter4];
        checkIfOk();
        textBoxPhoneNumber.Focus();
    }

I use counter4 as a part of method (checkIfOk) that enables button. There is also an "X" icon that changes into "tick" when the number is given in proper format.

It works perfectly for me (just like in the program I am copying) - when the user writes something in the TextBox, he can only use digits and "+" and "-" and when the format is ok the icon changes and when other textboxes are also ok, the Ok buton enables.

Now, finally, the problem:
I am able to paste something from the Clipboard. In the original program, when I paste something with letters, digits and other signs, only digits and "+" and "-" remains. My program accepts everything in such situation.

I've been looking for something that might be helpful, but all I have found was very complicated. Is there a way to do it?


I tried to do something like this. It causes that when pasting only digits and "+" and "-" remains as it should, but the user can't write anything.
I am still a beginner. Maybe I am making a simple mistake?

    private void phoneNumberValidity(object sender, EventArgs e)
    {
        Regex regex = new Regex("[^0-9-+]");
        if (regex.IsMatch(Clipboard.GetText()))
        {
            counter4 = Convert.ToInt32(IsPhoneNumberCorrect(textBoxPhoneNumber.Text));                
            pictureBoxPhoneNumber.Image = imageList1.Images[counter4];
            string output = regex.Replace(Clipboard.GetText(), "");
            textBoxPhoneNumber.Text = output;
            checkIfOk();
            textBoxPhoneNumber.Focus();
        }
    }

I am trying to do something like this:

    private void phoneNumberValidity(object sender, EventArgs e)
    {
        counter4 = Convert.ToInt32(IsPhoneNumberCorrect(textBoxPhoneNumber.Text));
        pictureBoxPhoneNumber.Image = imageList1.Images[counter4];
        checkIfOk();
        textBoxPhoneNumber.Focus();

        Regex regex = new Regex("[^0-9-+]");
        if (textBoxPhoneNumber.Text.Contains("a"))
        {
            if (regex.IsMatch(Clipboard.GetText()))
            {
                string output = regex.Replace(Clipboard.GetText(), "");
                textBoxPhoneNumber.Text = output;
            }
        }
    }

I know that it's not exactly what I want, but maybe someone can give some clues... Generally i thought, that I'd like to check if the text in tb contains some unwanted elements, but I don't know how to check it. As you can see, it checks only one unwanted element.

Dyziok
  • 11
  • 1
  • 3
  • This looks like what you're looking for http://stackoverflow.com/questions/5113722/how-to-disable-copy-paste-and-delete-features-on-a-textbox-using-c-sharp – Prix Jun 08 '14 at 10:00
  • Try looking at other events that are available on the Textbox, an OnChange event for example might suit your needs – Hanno Jun 08 '14 at 10:02
  • @Prix I don't see anything that suits me there. – Dyziok Jun 08 '14 at 11:51
  • @Hanno I am already using TextChanged event, I can't any other events that might help. So I can't do the two things (checking what the user is writing and pasting [with RMB click]) in one event? – Dyziok Jun 08 '14 at 11:54

3 Answers3

1

First of all, please use TRY-Catch for the Convert.ToInt32 !

Second: Use TextChanged event, and validate the input with the actual content of the TextBox

To validate, you can do something similar:

string output = ""
string clipboardText = GetClipboardText()
for each chara in clipboardText's characters
    if chara.isNumeric or chara=='+' or chara == '-'
         output += chara
end foreach

Of course its just a simple soultion, but you can confgure it as you want. Or if you want more complex way, you can play with the regex... Start with number or + - BUT not contains alphabetical char. Based on your request.

Krekkon
  • 1,349
  • 14
  • 35
  • Thanks. I am using the TextChanged event. I am trying to validate as you wrote (please see, the bottom of my post), but it's still wrong. I can't find another way. – Dyziok Jun 08 '14 at 17:55
  • You can try this simple thing above :) – Krekkon Jun 08 '14 at 18:03
0

This is what I made, and it works :) I will only add try-catch block as Krekkon has suggested.

    private void phoneNumberValidity(object sender, EventArgs e)
    {
        counter4 = Convert.ToInt32(IsPhoneNumberCorrect(textBoxPhoneNumber.Text));
        pictureBoxPhoneNumber.Image = imageList1.Images[counter4];
        if (Regex.IsMatch(textBoxPhoneNumber.Text, "[^0-9-+]"))
        {
            Regex regex = new Regex("[^0-9-+]");
            string output = regex.Replace(Clipboard.GetText(), "");
            textBoxPhoneNumber.Text = output;
        }
        checkIfOk();
        textBoxPhoneNumber.Focus();
        }

Maybe it will help someone in the future.

Dyziok
  • 11
  • 1
  • 3
-1

Maybe you should try like this: leave the first version of phoneNumberValidity method as it is and then check if the tb Text has some unwanted elements, get rid of them.