从C#文本框中的位置获取字符

时间:2011-04-06 09:39:39

标签: c# textbox

我正试图从文本框中获取3个字符 - >例如:“test”并从本文中获得3。

我正在尝试类似这样的地方,其中printer1_logo.text是另一个文本框,我想从文本框RefrenceNumber中获得4个字符。

Printer1_Logo.Text=RefrenceNumber.GetCharFromPosition(4);

错误:

Argument 1: cannot convert from 'int' to 'System.Drawing.Point' (CS1503) - D:\App\MainForm.cs:249,66

The best overloaded method match for 'System.Windows.Forms.TextBoxBase.GetCharFromPosition(System.Drawing.Point)' has some invalid arguments (CS1502) - D:\App\MainForm.cs:249,31

3 个答案:

答案 0 :(得分:5)

在C#中 textBox1.Text = textBox2.Text[2].ToString()

在VB中 textBox1.Text = textBox2.Text(2).ToString

如果您需要超过1个字符

textBox1.Text = textBox2.Text.SubString(2,2)

答案 1 :(得分:4)

GetCharFomPosition(位置表示坐标。不是char索引) 你也可以使用string作为char数组,例如:

Char  theChar = Printer1_Logo.Text[2]; // index is start at 0

答案 2 :(得分:1)

您只需使用 SubString 剪切字符串:

string subString1 = textbox1.Text.Substring(0,3)