hi im again,,,,,,
thanx for daniweb i get the solution to my 1st problum.
now the second problum;

i have a textbox on my windows form and i need to validate it.
rule is user must not able to press any A-Z && a-z && !-* keys on the key bord.
if he is tring to pres any that kind of key the system must genarate a mesage box.
MessageBox.Show("bla bla bla"); :)

only the keys the user can pres on that text box must be 1,2,3,4....0.

i hop you guys will help me on this little thing.

:icon_wink:

thanxxxxx

Try the following code in the textbox_Keypress Event!

if(((e.KeyChar < '0' || e.KeyChar >'9'))&&(e.KeyChar!='\b'))
        {
        MessageBox.Show("Please Enter Numbers only");
            e.Handled = true;
        }
        else
        {
        e.Handled=false;
        }

The above coding will enable the user to enter values from 0 -9 and will allow the user to erase the characters back(BackSpace).

Hope this helps you!

Edited 3 Years Ago by mike_2000_17: Fixed formatting

My solution Danny is more scalable
Don't depends on current requirements and you should have some flexibility
I argue using RegularExpression
pattern = .....
Then if this text matches the pattern return true to accept the value otherwise false.
I'll do code snippet for it soon.

You might also consider using an ErrorProvider instead of a message box. I hate it when applications pop up a dialog while typing. You could set the error message on an invalid key press and wait until the control loses focus or a valid key is pressed then clear the error.

Comments
Nice said!

@Scott : I hate it when applications pop up a dialog while typing.
I could not agree more:)
ErrorProvider is a great way to go, but only if you have a few textboxes on your form. It tends to be buggy. : look at this example

Comments
good information

@Scott : I hate it when applications pop up a dialog while typing.
I could not agree more:)
ErrorProvider is a great way to go, but only if you have a few textboxes on your form. It tends to be buggy. : look at this example

Ah, I did not realize that. I use DeveloperExpress controls where those had been fixed already -- plus they added a nice property .HasErrors which the stock error provider doesn't have or I haven't been able to find.

I use XtraForm, XtraButton, XtraPanel, XtraEverything. Developer express has awesome controls.

Developer Express seems a nice thing to have. I am reading more about it on their site. HasErrors may be simulated with GetError I guess. Look at the code example on this MSDN page

You're most welcome, Danny :)
I hope you like it, when I had facebook account I had 700 pics there, I may schedule changing my avatar periodically :D

This article has been dead for over six months. Start a new discussion instead.