如何在 C# 中自动递增数字?

发布于 2024-10-16 06:19:57 字数 142 浏览 6 评论 0原文

我正在使用 C# 2008 Windows 窗体应用程序。

在我的项目中,有一个 TextBox 控件,我想为样本 s00 自动生成数字,接下来当我再次返回表单时,它应该像 s01、s02、s03 这样递增... ...就像这样

请帮助我

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

梦醒灬来后我 2024-10-23 06:19:57

很容易。保留一个变量来保留当前数字。

int incNumber = 0;

然后单击按钮,生成数字字符串,如下所示:

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

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++;
酒绊 2024-10-23 06:19:57

按照Øyvind Knobloch-Bråthen的建议进行操作,但如果您希望在表单停用激活时自动完成>(你回到表单并给予它焦点)然后你可以做这样的事情。

只有当您确定框中的文本始终采用上述格式时,此方法才有效

this.Activated += (s, ev)=>{ 
         string tmp = textbox1.Text; 
         int num = String.Substring(1) as int;              
         if(nuum != null) 
         {
             num++;
             textbox1.Text = "s" + num.Tostring();  
         }
      };

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();  
         }
      };
杀手六號 2024-10-23 06:19:57

正如 Øyvind Knobloch-Bråthen 所说:使用变量跟踪整数。只有您应该像这样格式化它(Microsoft 首选):

int incNumber = 0;

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

或者如果您想用一行少的代码来完成它:

int incNumber = 0;

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

请参阅 MSDN 有关格式化整数的完整参考。

Just as Øyvind Knobloch-Bråthen said: Keep track of the integer using 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 it with one line less code:

int incNumber = 0;

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

See MSDN for a complete reference for formatting integers.

兔小萌 2024-10-23 06:19:57

上面的 oyvind-knobloch-brathen 的稍微好一点的变体:

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

//s00、s01、s02。如果您想要范围 0001-9999,只需将“00”更改为“0000”等即可。

A slightly better variation of oyvind-knobloch-brathen's above:

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.

我不是你的备胎 2024-10-23 06:19:57

如果字符串的文本部分未知(字符串末尾有或没有数字),此函数的变体可能会有所帮助:

        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;
        }

If the text component of the string is unknown (with or without a number at the end of the string), variations of this function may be helpful:

        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;
        }
小忆控 2024-10-23 06:19:57

试试这个对于数字的自动生成和数字的自动增量:

// 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();
}

Try This For auto generation of number and auto incrementation of 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();
}
紅太極 2024-10-23 06:19:57

另一种单行方法是:

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

其中 counter 定义如下:

int counter= 0;

Another single line approach would be:

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

Where counter defines like this:

int counter= 0;
初雪 2024-10-23 06:19:57

这是 C# 中生成自动递增 id 的解决方案,不需要增加或执行任何操作。它会起作用的。每当一个新对象创建时,它的值就会增加 1。

public class Employee
    {
        static long AutoId = 0;
        public long Id { get; private set; } = ++AutoId;
        public string EmployeeName { get; set; }
        public string Address { get; set; }
    }

This is the solution to generator auto incremented id in C# which don't need to increase or do anything. It will just work. whenever a new object created its value will increase by 1.

public class Employee
    {
        static long AutoId = 0;
        public long Id { get; private set; } = ++AutoId;
        public string EmployeeName { get; set; }
        public string Address { get; set; }
    }
静若繁花 2024-10-23 06:19:57
{  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(); } } 抓住 { } }

{  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 { } }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文