How can I automatically increment numbers in C #?

I am using a C # 2008 Windows Forms application.

My project has a TextBox control, and I want to do the automatic creation of numbers for samples s00, then when I return to form it again, it should increase like s01, s02, s03 ...... like that

Please help me

+8
c # winforms auto-increment
source share
8 answers

Pretty easy. Hold the variable to save the current number.

 int incNumber = 0; 

Then, when you click the button, create a number line as follows:

 string nyNumber = "s" + incNumber.ToString("00"); incNumber++; 
+7
source share

Do as Øyvind Knobloch-Bråthen suggested, but if you want it to be done automatically when the form is Deactivated and Activated (you come back to the form and give it focus), then you can do something like this.

This only works if you are sure that the text in the field will always be in the specified 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(); } }; 
+4
source share

Just as Øyvind Knobloch-Bråthen said: Keep track of an integer with a variable. Only you should format it like this (Microsoft preferred):

 int incNumber = 0; string formattedIncNumber = String.Format("s{0:D2}", incNumber); incNumber++; 

Or if you want to do this with one line less code:

 int incNumber = 0; string formattedIncNumber = String.Format("s{0:D2}", incNumber++); 

See MSDN for a complete reference on integer formatting.

+2
source share

The slightly better change of the Owwind Knobloh Bro is higher:

 int incNumber=0; s + String.Format("{0:00}", incNumber); incNumber++; 

// s00, s01, s02. If you want, say, the range 0001-9999, just change “00” to “0000”, etc.

+2
source share

If the text component of the line is unknown (with or without a number at the end of the line), options for this function may be useful:

  private string increment_number_at_end_of_string(string text_with_number_at_the_end) { string text_without_number = text_with_number_at_the_end.TrimEnd('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); string just_the_number = text_with_number_at_the_end.Substring(text_without_number.Length); int number = -1; if (int.TryParse(just_the_number, out number)) { return text_without_number + (number + 1).ToString(); } return text_with_number_at_the_end; } 
+1
source share

Try this to automatically generate a number and automatically increase the number:

 // Stock is table name // metal id is unique number that is auto generated as well as auto incremented private void textBox9_TextChanged(object sender, EventArgs e) { string s = "select max(metalid)+1 from stock"; SqlCommand csm = new SqlCommand(s, con); con.Open(); csm.ExecuteNonQuery(); SqlDataReader dd = csm.ExecuteReader(); while (dd.Read()) { int n = dd.GetInt32(0); textBox1.Text = n.ToString(); } con.Close(); } 
0
source share

Another approach to one line:

 string sampleNum = "s" + (counter++).ToString("00"); 

Where the counter determines the following:

 int counter= 0; 
0
source share
 { try { //madhura// SqlCommand cmd1 = new SqlCommand(@"select 'Column_name'+ REPLACE(STR(MAX(CAST(Right(Column_name,5) as int)+1 ),6),SPACE(1),'0') as Column_name from TabelName ", con); SqlDataAdapter da = new SqlDataAdapter(cmd1); DataTable dt = new DataTable(); da.Fill(dt); 

if (dt.Rows [0] ["Column_name '"]. ToString () == null) {Label1.Text = "DMBP-000001"; } else {Label.Text = dt.Rows [0] ["Column_name '"]. ToString (); }} catch {}}

-2
source share

All Articles