简体   繁体   中英

Inserting an integer value in a TextBox

I need to show an integer value in a TextBox in my C# Windows Forms application (GUI). I have an int32 value available. I could not find a container like a TextBox that takes int values. The TextBox only accepts Strings. How do I type cast?

Everything in .NET can be transformed to a string in one way or another by using the "ToString()" method.

Example

int x = 5;
string y = x.ToString();
int i = 10;
TextBox1.Text = i.ToString();

TextBox.Text = MyInteger.ToString();

You can use the ToString() method to convert the integer to a string.

int x = 10;

Console.WriteLine(x.ToString())

You can do this in many ways:

        int i = 123893232;
        Console.WriteLine(i.ToString());//123893232
        Console.WriteLine(Convert.ToString(i));//123893232
        Console.WriteLine(String.Format("{0:C}", i));//123 893 232,00 zł(Polish)
        Console.WriteLine(String.Format("{0:D}", i));//123893232
        Console.WriteLine(String.Format("{0:E}", i));//1,238932E+008
        Console.WriteLine(String.Format("{0:F}", i));//123893232,00
        Console.WriteLine(String.Format("{0:G}", i));//123893232
        Console.WriteLine(String.Format("{0:N}", i));//123 893 232,00
        Console.WriteLine(String.Format("{0:P}", i));//12 389 323 200,00
        Console.WriteLine(String.Format("{0:X}", i));//76275F0 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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