C# 在 TextBox 中插入整数值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1280328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:37:43  来源:igfitidea点击:

Inserting an integer value in a TextBox

c#.netwinforms

提问by zack

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?

我需要在我的 C# Windows 窗体应用程序 (GUI) 的 TextBox 中显示一个整数值。我有一个 int32 值可用。我找不到像 TextBox 这样的接受 int 值的容器。TextBox 只接受字符串。我如何打字?

采纳答案by womp

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

.NET 中的所有内容都可以通过使用“ToString()”方法以一种或另一种方式转换为字符串。

Example

例子

int x = 5;
string y = x.ToString();

回答by David

int i = 10;
TextBox1.Text = i.ToString();

回答by Mark Redman

TextBox.Text = MyInteger.ToString();

TextBox.Text = MyInteger.ToString();

回答by Dave Carlile

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

您可以使用 ToString() 方法将整数转换为字符串。

int x = 10;

整数 x = 10;

Console.WriteLine(x.ToString())

Console.WriteLine(x.ToString())

回答by Micha? Ziober

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