Hi evry1,
i have a question that how can i disallow spaces in the textbox or any character that i dont want the user to textit ,
thx
(thats my 1st time here ^_^)
Hi evry1,
i have a question that how can i disallow spaces in the textbox or any character that i dont want the user to textit ,
thx
(thats my 1st time here ^_^)
how can i disallow spaces or any or even numbers in text box?
MSatya
//u create the event for ur textbox text changed.. and send to the method validateInt a textbox control. that u want to validate
private void txtCantidad_TextChanged(object sender, EventArgs e)
{
validanumero(txtCantidad);
}
private void validateInt (TextBox sender)
{
//a flag to define if it should select the end of the text so it looks pretty :P
bool selectEnd = false;
// the string newText will be the final result
string newText = null;
//here its gets the text of sender.. sender is the textbox that u sent
string txt = sender.Text;
//creates an array of chars of ur textbox text
char[] txtArray = txt.ToCharArray();
//for each character it starts the validation
foreach (char car in txtArray)
{
//i car is > 47 and < 58 the its a valid character and add to newtext the character... if u dont know what is 47 and 48 look in google for an ASCII table and each character contains a number... thats it
if (car > 47 && car < 58)
{
newText = newText + car;
}
//heres what i said about selecend... if the character is not valid then it will select the last part of the text.. if u dont understand me,, just dont do it and check out what happen :)
else
{
selectEnd = true;
}
}
//ok,,now it ended and the text of the text is = to newText;
sender.Text = newText;
//now figures out what part of the text to select .. as i said if uj didnt get my point on here :P ,, remove it and check out ;)
if (selectEnd == true)
{
int x = 0;
try
{
x = newText.Length;
}
catch (Exception)
{
newText = "";
}
sender.SelectionStart = x;
}
}
hope it helps... if u need to validate floats just change the characters number and there u go ;)
ToroLocoMex
hi ,
actually im learning c# now and im not good enough yet by enevts if i dont bother you can u write the code please so i guess i can understand how does it works .
thx
tim_baxter
Handle the KeyPressed event and if the key passed in through the event args is not one you want, then sent e.Handled = true;
Be careful tho, if char.IsControl returns true, you want to leave e.Handled, otherrwise the arrows, etc will not work.
adimasi
hi,
you can try something like this
public partial class Form1 : Form {
public Form1()
{
InitializeComponent();
//keypress event this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{ //first character in this string is space string rejectthose = " ><,-+{}[]";
if (rejectthose.IndexOf(e.KeyChar) > -1)
{
MessageBox.Show("you can't use this character " + e.KeyChar);
e.Handled = true;
}
}
}
hope this helps