简体   繁体   中英

How to bind multiline textbox to bindingList<string>?

How can I bind textboxt text to BindingList? I want so that when I add/change/remove string from that BindingList my TextBox will update.

Create a single string with line breaks from the List

 stringbuilder sb = new stringbuilder();   
 foreach (string s in lstring) sb.AppendLine(s); 

Use BindingSource class to bind a String to a textbox. All you need is to bind the textbox with BindingSource object, whose data source is the string you want to use.

TextBox textBox1 = new TextBox();
BindingSource stringBindingSource;
string x = "ABCDEFGHIJKLMNOP";

stringBindingSource = new System.Windows.Forms.BindingSource(this.components);
textBox1.DataBindings.Add(new Binding("Text",stringBindingSource,"Length",true));
stringBindingSource.DataSource = x;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM