Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Dear All,

I have two textboxes.. I want to display the last 4 digits of 1st textbox into 2nd textbox using the textchanged event of 1st textbox...

Ex: If I enter 4566456767 in 1st textbox, then 6767 should be displayed in 2nd textbox..

Please any one suggest me how to do this..


Regards,
Raj Bangalore
Posted
Comments
Sergey Alexandrovich Kryukov 30-Mar-11 0:23am    
And why it seems to be a problem? Any effort?
--SA
Raj.rcr 30-Mar-11 0:27am    
coding is the problem SA. I can display the same text in 2nd textbox, but dnt knw how to display only the last 4 digits..
Sergey Alexandrovich Kryukov 30-Mar-11 0:37am    
Did you try any code? OK, see my comments to the Answer by Mika. You have a chance to improve it...
--SA

You wire up the TextChanged[^] event and you can use SubString[^] to get the last 4 digits. For example:

private void textBox1_TextChanged(object sender, EventArgs e) {
    if (textBox1.Text.Length >= 4) {
        textBox2.Text = textBox1.Text.Substring(textBox1.Text.Length - 4);
    }
}
 
Share this answer
 
Comments
Raj.rcr 30-Mar-11 0:32am    
Thank u Mika...
Sergey Alexandrovich Kryukov 30-Mar-11 0:35am    
This is a working solution, my 5, however:
1) this is APS.NET. Wiring every TextChanged event through postback will show poop performance; the alternative would be doing it all on client side via Javascript;
2) requirement is "4 digits", not "4 characters", so some extra code required.
3) I would add something like:
} else textBox2.Text = "What the hell are you doing? There is not 4 digits here!"
:-)

--SA
Wendelius 31-Mar-11 14:35pm    
Good points, thanks :)
iam giving you example

try this

string str = "HelloWorld";
TextBox1.Text=str.Substring(str.Length - 4);
 
Share this answer
 
Comments
Raj.rcr 30-Mar-11 0:33am    
Thank u Mahen...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900