简体   繁体   English

如何在C#中的文本框末尾添加单位

[英]How to add a unit at the end of textbox in C#

I find this code from here to add a unit like minutes or kg at the end of a textbox after the number was entered. 我从此处找到此代码,以在输入数字后在文本框的末尾添加单位,例如分钟或公斤。 in C# 在C#中

private void CowAgetxtBox_Leave(object sender, EventArgs e)
    {
        {
            CowAgetxtBox.Text = CowAgetxtBox.Text + " Month";
        }
    }

    private void CowAgetxtBox_Enter(object sender, EventArgs e)
    {
        {
            CowAgetxtBox.Text = CowAgetxtBox.Text.Replace(" Month", "");
        }
    }

but I don't know how get that number, I use this Code but it doesn't work 但我不知道如何获得该数字,我使用了此代码,但它不起作用

int Age = int.Parse(CowAgetxtBox.Text);

实际上,将单元放置在文本框中不是一个好主意,这会在数据验证,本地化,转换等方面引起很多问题,最好将其放置在文本框中:)

You just need to do a process similar you when the user focused on the text box. 当用户专注于文本框时,您只需要执行与您类似的过程即可。

int Age = int.Parse(CowAgetxtBox.Text.Replace(" Month", ""));

This will get the job done. 这样就可以完成工作。

It seems that 看起来

         CowAgetxtBox.Text

this statement contains value other then number thats why this statment 该语句包含其他值,而不是数字,这就是为什么该陈述

        int Age = int.Parse(CowAgetxtBox.Text);

is not working remove " Month" from the first function 无法正常工作,请从第一个功能中删除“月”

You can add a property like the following: 您可以添加如下所示的属性:

int Age {
get { return Convert.ToInt32(CowAgetxtBox.Text.Replace(" Month", ""));     }
}

如果单位标签发生变化,这可能会在更多情况下为您服务:

int Age = int.Parse(CowAgetxtBox.Text.Split(' ')[0]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM