Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Hi All
I am working on C# Windows form where I have data in String Builder below I give the pattern
String Builder sb = val1,val2,val3,val4,val5,val6,val7;

I have also four Text Box
Now my question is how can I put the sb data to Text Box like following
TextBox1.text = val2;
TextBox2.text = val3;
TextBox3.text = val5;
TextBox4.text = val7;

Thanks & Regards
Indrajit Dasgupta
Posted
Updated 24-Oct-12 19:15pm
v2
Comments
Sergey Alexandrovich Kryukov 25-Oct-12 2:04am    
Aha, and if this is not Form, you think that StringBuilder will be used in a different way? My vote of 1 just for the title of the question. Did you ever heard of separation of concerns? Forget about the topic of the question. You move in wrong direction. And first line will not compile. It's not nice to submit code without checking it up on a compiler.
--SA

Hi,
You already have a string. StringBuilder is recommended only in case where you need to append the string. Otherwise it is better to use string object directly. Because here you are allocating extra memory to StringBuilder object. And on top of that you need a method ToString() to convert that to a string object. Instead its better to use string object directly (it is better for the current scenario).
C#
string s = "val1,val2,val3,val4,val5,val6,val7";
var sSplit = s.Split(',');
txtBox1.Text = sSplit[1];
txtBox2.Text = sSplit[2];
txtBox3.Text = sSplit[4];
txtBox4.Text = sSplit[6];
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 25-Oct-12 2:08am    
So far, the only answer which makes sense, a 5.
--SA
hi... try below code:
C#
string[] splitted = sb.ToString().Split(',');

Now array splitted will have elements val1 at index 0, val2 at index 1, and so on. You can then assign to Textboxes:
C#
txtBox1.Text = splitted[1];
txtBox2.Text = splitted[2];
txtBox3.Text = splitted[3];
txtBox4.Text = splitted[4];
 
Share this answer
 
Try Something Like this,

C#
StringBuilder sb = new StringBuilder();
            sb.Append("val1,val2,val3,val4,val5,val6,val7");    

            string[] Splits = (sb.ToString()).Split(',');
            textBox1.Text = Splits[1];
            textBox2.Text = Splits[2];
            textBox3.Text = Splits[4];
            textBox4.Text = Splits[6];



regards
sarva
 
Share this answer
 
Hi,

Try this if could help...

C#
StringBuilder sb = new StringBuilder();
sb.Append("val1,val2,val3,val4,val5,val6,val7");
var myAraary = sb.ToString().Split(',');

Text Box1 = myAraary[1];
Text Box2 = myAraary[2];
Text Box3 = myAraary[4];
Text Box4 = myAraary[6];


Note that the myAraary start index position at 0.

Regards,
 
Share this answer
 
first you need to convert to string and use string split by ','. now you have array of values and you can directly assign the value to textbox.

StringBuilder strbuilder = new StringBuilder();
strbuilder .Append("val1,val2,val3,val4,val5,val6,val7");

string[] str = (strbuilder .ToString()).Split(',');
textBox1.Text = str [1];
textBox2.Text = str [2];
.......................
 
Share this answer
 
v2
Comments
IndrajitDasgupat 25-Oct-12 0:48am    
can u show me little code about that if u don't mind
Hi friend ,code below may b the solution of your problem.

C#
StringBuilder sb = val1,val2,val3,val4,val5,val6,val7;
           string[] arr = sb.ToString().Split(',');
           textBox1.Text = arr[1].ToString();
           textBox2.Text = arr[3].ToString();
           textBox3.Text = arr[5].ToString();
 
Share this answer
 
v5

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