Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

how to convert string[] to textbox

my code is:

C#
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);
                   }
Posted
Comments
CPallini 4-Aug-14 8:54am    
What's the purpose of splitting the original string?
Mike Meinz 4-Aug-14 8:54am    
Please explain what you are trying to do. I see that you create a string array by parsing the string s using a comma. Then you want to put the string array all on one line back into the same TextBox. I don't understand what you actually want to occur and why.

Arrays cannot be assigned to a TextBox.
Also, when assigning a string to a TextBox, you would use MyTextBox.Text.
[no name] 4-Aug-14 8:54am    
MyTextBox.Text
prasanna.raj 4-Aug-14 9:03am    
Here i am using Dynamic textbox when i save the textvalues with ',' like "val1,val2" in database and when i try to retrieve from DB its came originally like "val1,val2" but i want to split in two textbox...
CPallini 4-Aug-14 9:10am    
But there are't two text boxes in your code.
Possibly you have to dynamically create text boxes, one for each item of the splitted array? Wouldn't be better a listbox then?

Perhaps this is something like what you want to do.

C#
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);
    }
}
 
Share this answer
 
v10
Comments
prasanna.raj 4-Aug-14 9:34am    
oh this also Great just wait i am checking...
prasanna.raj 4-Aug-14 9:53am    
Here i have one more problem

string[] keys = Request.Form.AllKeys;
string value = string.Empty;
foreach (string key in keys)
{
if (key.IndexOf(ControlID) >= 0)
{
value += Request.Form[key] + ",";
}
}

return value;

this the code i am using to adding but when it added like "value1,value2," so S1 take one more value for ","(comma)
like [0]=value1,[1]=value2,[3]=""(emplty), how to clear this.,,,,
Mike Meinz 4-Aug-14 10:01am    
string[] keys = Request.Form.AllKeys;
string value = string.Empty;
foreach (string key in keys)
{
if (key.IndexOf(ControlID) >= 0)
{
value += Request.Form[key] + ",";
}
}
// Remove extraneous comma
if (value.EndsWith(","))
{
value = value.Substring(0, value.Length - 1);
}

return value;
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

C#
string[] stringArray = {"Hi", "! my name is ", "Afzaal Ahmad Zeeshan"};\
// Hi! my name is Afzaal Ahmad Zeeshan


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.

C#
// 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";


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[].

C#
string stringData = "";
foreach (string str in stringArray) {
   // foreach string, there are currently 3 strings, 
   // read the first code block
   stringData += str;
}


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).

C#
// 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.
 
Share this answer
 
Comments
prasanna.raj 5-Aug-14 1:17am    
Great thank you...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900