I am using C# 2008 Windows Forms application.
In my project there is a TextBox
control and in that I want make an auto generate numbers for samples s00, next when I come back to form again it should be increment like s01,s02,s03......like that
Please help me
In C, ++ and -- operators are called increment and decrement operators. They are unary operators needing only one operand. Hence ++ as well as -- operator can appear before or after the operand with same effect. That means both i++ and ++i will be equivalent.
Auto increment and decrement One of the nicer shortcuts is the auto-increment and auto-decrement operators. You often use these to change loop variables, which control the number of times a loop executes. The auto-decrement operator is '--' and means “decrease by one unit.”
1. The process of increasing or decreasing a numeric value by another value. For example, incrementing 2 to 10 by the number 2 would be 2, 4, 6, 8, 10. 2.
2) Post-increment operator: A post-increment operator is used to increment the value of the variable after executing the expression completely in which post-increment is used. In the Post-Increment, value is first used in an expression and then incremented. Syntax: a = x++;
Quite easy. Keep a variable to keep the current number.
int incNumber = 0;
Then on click of button, generate the number string like this:
string nyNumber = "s" + incNumber.ToString("00");
incNumber++;
Do as suggested by Øyvind Knobloch-Bråthen but if you want it to be done automatically when form is Deactivated and Activated (You come back to the form and give it focus) then you can do somthing like this.
This only works if you are sure the text in box will always be in the mentioned format
this.Activated += (s, ev)=>{
string tmp = textbox1.Text;
int num = String.Substring(1) as int;
if(nuum != null)
{
num++;
textbox1.Text = "s" + num.Tostring();
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With