Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
See more:
I want to increase a number in a textbox for example
In the textbox when i write 10 the next time the page load the number should be 11
So i want to increase by +1

this is my code. this make a stackoverflow. the hole computer freezes.

C#
private void TextBoxKundnr_TextChanged(object sender, EventArgs e)
        {

            int serialNo = Convert.ToInt32(TextBoxKundnr.Text);
            serialNo++;
            TextBoxKundnr.Text = serialNo.ToString(CultureInfo.InvariantCulture);
            
            
            }
            
        }
Posted
Comments
agent_kruger 22-Feb-14 22:45pm    
sir,i don't understand, you write the question and you answer it. Where is the problem then?

Well...it's pretty obvious, isn't it?
When do you get a TextChanged event?
Answer: when your change the Text Property.
So...when you change the Text Property of a TextBox from with that TextBox TextChanged event, what would you expect to happen next?
Correct: another TextChange Event. Which will also change the Text, causing another TextChanged event, which will....until your whole computer freezes trying to keep up with finally getting a TextChanged event to end, or it runs out of stack space...

Don't do it.
Ignoring this problem, changing text while your user is editing it is not a friendly thing to do, and will seriously annoy some people.
 
Share this answer
 
Looks like a wrong concept behind a common problem. That serialNo is an identifier, isn't it? And further data entered in other textboxes on the form describe the item, don't they? And eventually the data is tored in a database.
So the correct solution is: have the database generating the identifier (e.g. autoincrement column). Only show an ID value for data which were retrieved from the database (e.g. viewing or editing data), if you show it all (what's the meaning of such an identifier for the user of the software?).
 
Share this answer
 

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