In order to do that we 1st need to Know about the substring.
Substring in C# string Class returns a new string that is a substring of this string. The substring begins at the specified given index and extended up to the given length.
string string.substring(int startIndex,int length)
Parameters
- startIndex: The index of the start of the substring.
- length: The number of characters in the substring.
Returns:
The specified substring.
Program and its code:
- Create a new windows form.
- Drag an Drop the Textbox and Button field on to the windows form.
- Fill the Textbox Data as "CP000".
- Now we will write code for the button click event. In this code we will try to increment the textbox data.
In the following manner CP000,CP001,CP002,.....CP011,CP012,......CP998,CP999.
- Declare a global variable in the form and name it as:
- public string str;
-
- and assign textboxt1.text = str;
- Button Click Event code.
- private void btnIncrement_Click(object sender, EventArgs e)
- {
- int Num1 = Convert.ToInt32(textBox1.Text.Substring(4, 1));
- int Num2 = Convert.ToInt32(textBox1.Text.Substring(3, 2));
- int Num3 = Convert.ToInt32(textBox1.Text.Substring(2, 3));
- if (Num1 < 9)
- {
- int Num = Num1 + 1;
- str = str.Substring(0, 4) + Num;
- textBox1.Text = str;
- }
- else if (Num2 < 99)
- {
- int Num = Num2 + 1;
- str = str.Substring(0, 3) + Num;
- textBox1.Text = str;
- }
- else if (Num3 < 999)
- {
- int Num = Num3 + 1;
- str = str.Substring(0, 2) + Num;
- textBox1.Text = str;
- }
- }
- Enjoy the programming.