Detect last charecter & Do something
-
Friday, August 10, 2012 10:17 AM
I want to that.....
if my textbox focus is lost then
a "," (Comma) should be entered
tb.Leave += delegate(object s, EventArgs ev)
{
tb.Text = tb.Text.Trim() + ",";
};this working fine (Output = My Sample Text,)
if this text box focus is lost again (when text has been entered alraddy[My Sample Text,]) then output = My Sample Text,,
so i decide to set a condition like this.... But no luck........
if my text box has "," (Comma) as last charecter then should not enter ", " again
my code is......
tb.Leave += delegate(object s, EventArgs ev)
{
if (tb.Text.LastIndexOf(tb.Text) != ",")
{
tb.Text = tb.Text.Trim() + ",";
}
else
{
tb.Text = tb.Text.Trim();
}
};what is wrong with me here?
please help.
i am using DOT NET frame work 2.0
see error
http://img838.imageshack.us/img838/1843/errorzc.jpg

All Replies
-
Friday, August 10, 2012 10:19 AM
Last index of returns an int.
You can use tb.Text.EndWith(",")
http://msdn.microsoft.com/it-it/library/system.string.endswith(v=vs.80).aspx
Regards,
Bubu
http://zsvipullo.blogspot.it- Proposed As Answer by Sambath Raj.C Friday, August 10, 2012 10:32 AM
- Marked As Answer by Bob ShenMicrosoft Contingent Staff, Moderator Tuesday, August 14, 2012 8:09 AM
-
Friday, August 10, 2012 10:38 AM
If you want to get the last char of the string:
tb.Text.Substring(tb.Text.Length -1)
or
tb.Text[tb.Text.Lenght -1]
- Marked As Answer by Bob ShenMicrosoft Contingent Staff, Moderator Tuesday, August 14, 2012 8:09 AM
-
Friday, August 10, 2012 11:04 AM
Hi,
You already have the code you need; you just need to modify it a tiny little bit.
tb.Leave += delegate(object s, EventArgs ev) { if(tb.Text.EndsWith(',') { tb.Text = tb.Text.Trim(); } else { tb.Text = tb.Trim() + "," + " "; // Not sure why you are adding a space when you are using Trim() :P } }That's all you need.
Dave
Digital Forensic Software Developer
CCS LABS Digital Forensic Software
Mark as Answer or Vote up if useful thank you!- Proposed As Answer by Dave A Gordon Friday, August 10, 2012 11:04 AM
- Marked As Answer by Bob ShenMicrosoft Contingent Staff, Moderator Tuesday, August 14, 2012 8:09 AM
-
Saturday, August 11, 2012 6:16 AM
private void button1_Click(object sender, EventArgs e)
{
string LastChar = string.Empty;
LastChar = textBox1.Text;
LastChar = LastChar.Substring(LastChar.Length-1,1);
MessageBox.Show("The Last Character is " + LastChar);
}This will detect the last character is textbox, and its up to you what you want to do with last character
- Edited by amit_kumar Saturday, August 11, 2012 6:27 AM
- Marked As Answer by Bob ShenMicrosoft Contingent Staff, Moderator Tuesday, August 14, 2012 8:09 AM

