将字符串数组转换为文本框 [英] convert string array to textbox

查看:64
本文介绍了将字符串数组转换为文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何将字符串[]转换为文本框



我的代码是:





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);
                   }

推荐答案

也许这就像你想要的那样。



Perhaps this is something like what you want to do.

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 []转换为TextBox。 TextBox是一个Control,而String []是一个变量。隐式转换不会发生。



我想你想要做的是,你想将String []转换为String然后将它添加到TextBox的内容中。



好​​吧,我可以提供一个例子,你可以实现你的代码,开始使用任何TextBox和任何数据。



首先,我们将创建一个简单的String []数据。这将是这样的



Actually you can't convert a String[] to a TextBox. TextBox is a Control, whereas String[] is a variable. Implicit conversion won't happen.

I guess what you want to do is, that you want to convert the String[] into a String and then add it to the Content of the TextBox.

Well, I can provide an example of that thing that you can implement to your code to get started working with any TextBox and any Data.

First of all, we'll create a simple String[] of the data. Which would be like this

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.


这篇关于将字符串数组转换为文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆