问题描述
如何将字符串[]转换为文本框
我的代码是:
how to convert string[] to textbox
my code is:
for (int i = 0; i < n; i++)
{
TextBox MyTextBox = new TextBox();
MyTextBox.ID = "txtsno" + "" + ViewState["num"] + i;
MyTextBox.Text = Dtable_sno.Rows[i]["serialnumber"].ToString();
string s = MyTextBox.Text;
string[] s1 = s.Split(',');
MyTextBox.Width = 200;
MyTextBox.Height = 15;
MyTextBox.TextMode = TextBoxMode.SingleLine;
MyTextBox = s1; //Error Msg to converting
panelserial.Controls.Add(MyTextBox);
}
推荐答案
for (int i = 0; i < n; i++)
{
string s = Dtable_sno.Rows[i]["serialnumber"].ToString();
// If necessary, remove an extra comma at the end of the string
if (s.EndsWith (","))
{
s=s.Substring (0,s.Length - 1);
}
string[] s1 = s.Split(','); // Create array of strings
for (int j = 0; j < s1.Length; j++) // For each string in the array
{
// Create new TextBox
TextBox MyTextBox = new TextBox();
// Uniquely name the new TextBox
MyTextBox.ID = "txtsno" + ViewState["num"] + i + "_" + j;
MyTextBox.Width = 200;
MyTextBox.Height = 15;
MyTextBox.TextMode = TextBoxMode.SingleLine;
// Assign one string from the array to the new TextBox
MyTextBox.Text = s1[j];
panelserial.Controls.Add(MyTextBox);
}
}
string[] stringArray = {"Hi", "! my name is ", "Afzaal Ahmad Zeeshan"};\
// Hi! my name is Afzaal Ahmad Zeeshan
在此之后,如果您的控件中已有TextBox,则跳过此代码部分并转到下一部分,否则你可以阅读下面的代码块,我将在其中创建一个新的TextBox,然后在下一个块中我将数据添加到它。
After this, if you already have a TextBox in your Controls then skip this code part and move to the next part, otherwise you can read the following code block where I will create a new TextBox and after that, in the next block I would add the data to it.
// create new
TextBox box = new TextBox();
// add name, adding name is a very basic thing to do with Controls
// this way you don't have to call the hierarchy, and you just
// call the controls by there name in future coding.
box.Name = "myTextBox";
上面的代码足以创建一个新的TextBox,剩下的就是处理String []并将其转换为单个String。因为允许我们添加数据的TextBox的属性是 Text
,它只允许字符串数据。这就是为什么你不能添加String []。
Above code was enough to create a new TextBox, the remaining thing is to handle the String[] and convert it to single String. Because the property of the TextBox that allows us to add the data is Text
and it allows only string data. That is why you cannot add String[].
string stringData = "";
foreach (string str in stringArray) {
// foreach string, there are currently 3 strings,
// read the first code block
stringData += str;
}
足以将其转换为String。现在,当你将这个值添加到TextBox时,它将被传递,因为允许参数(这是字符串)。
Enough to convert it to String. Now when you'll add this value to the TextBox it would get passed since the parameter is allowed (which is string).
// add to the textbox, variable name was box. See code block 2
box.Text = stringData;
我希望你明白每种方法都有一些参数和数据类型。必须满足哪些才能完美地执行代码。如果不满足参数数据类型,它们将不会执行,并且您将从编译器获得错误,该变量无法转换为所需类型。
I hope you understood that each method has some parameter and the data type. Which must be met, in order to perfectly execute the code. If parameter data types are not met, they won't execute and you'll get error from Compiler, that the variable was not convertible to the type required.
这篇关于将字符串数组转换为文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!