如何在标签中显示文本框的所有值 [英] How to display all values of text box in label
问题描述
当我选择它时我可以显示一个项目
但是我不能一次显示所有项目。
我尝试了什么:
i试过这个
form1
private void btn_voit_Click( object sender,EventArgs e)
{
string textboxchoix = listBox2.Text;
Form2 frm = new Form2(textboxchoix);
frm.Show();
}
form2
public Form2( string textboxchoix)
{
InitializeComponent();
label_choix.Text = textboxchoix;
}
向Form2添加方法
public void AddString( string s)
{
lable_choix.Text + = s;
}并为每个额外项目调用。
引用:我知道如何在标签中显示文本框但如果我选择它将显示它只显示一个项目为exmaple我有10个元素n我的文本框但是当我显示它只显示一个我希望显示所有元素
这不是TextBox - 它是一个ListBox。
试试这个:
StringBuilder sb = new StringBuilder();
foreach(listBox1.Items中的对象x)
{
sb.AppendLine(x.ToString());
}
Form2 frm = new Form2(sb.ToString());
frm.Show();
已修改,
如果我理解正确,你试图将项目从表单上的列表框移动到另一个表单上的另一个列表框。
如果这是正确的,那么你需要定义一个公共方法在接收表格上并传递要添加到方法中的项目。
在发送方面,您收集项目并调用方法。但请注意,您需要知道表单的实例,仅仅知道该类是不够的
请考虑以下示例
在接收表格中定义如下方法
public bool AddListboxItems( string [] items){
此跨度> .listBox1.Items.AddRange(项目);
return true ;
}
请记住使用正确的列表框名称
如果接收表单的类是例如Form1,然后以下应该将项目发送到类的新实例
ListBox lb1 = new ListBox();
lb1.Items.Add( first);
lb1.Items.Add( second);
lb1.Items.Add( third);
// 选择项目0和2
lb1.SelectionMode = SelectionMode.MultiSimple;
lb1.SelectedItems.Add(lb1.Items [ 0 ]);
lb1.SelectedItems.Add(lb1.Items [ 2 ]);
Form1 frm1 = new Form1(); // 接收实例应以另一种方式计算
string [] items = new string [lb1。 SelectedItems.Count];
for ( int counter = 0 ; counter < lb1.SelectedItems.Count; counter ++){
items [counter] = lb1.SelectedItems [counter] .ToString ();
}
frm1.AddListboxItems(items);
请注意,接收表单是在代码中创建的,因此在实际情况下,您可能需要引用已存在的表单。
i can display one item when i select it
but i can't display all items at once.
What I have tried:
i tried this
form1
private void btn_voit_Click(object sender, EventArgs e)
{
string textboxchoix = listBox2.Text;
Form2 frm = new Form2(textboxchoix);
frm.Show();
}
form2
public Form2(string textboxchoix)
{
InitializeComponent();
label_choix.Text = textboxchoix;
}
Add a method to Form2
public void AddString(string s) { lable_choix.Text += s; }And call that for each additional item.
Quote:i know how to display textbox in label but if i select it will display and it only display one item for exmaple i have 10 element n my textbox but when i display it it shows only one i want display all element
That';s not a TextBox - it's a ListBox.
Try this:
StringBuilder sb = new StringBuilder(); foreach (object x in listBox1.Items) { sb.AppendLine(x.ToString()); } Form2 frm = new Form2(sb.ToString()); frm.Show();
Modified,
If I understand correctly, you're trying to move items from a listbox on a form to another listbox on a different form.
If that is correct then you need to define a public method on the receiving form and pass the items to be added to the method.
On the "sending" side you gather the items and call the method. However note that you need to know the instance for the form, it's not sufficient to only know the class
Consider the following example
On the "receiving" form define a method like the following
public bool AddListboxItems(string[] items) { this.listBox1.Items.AddRange(items); return true; }
Remember to use the correct list box name
And if the class of the receiving form is for example Form1 then the following should send the items to a new instance for the class
ListBox lb1 = new ListBox(); lb1.Items.Add("first"); lb1.Items.Add("second"); lb1.Items.Add("third"); // select items 0 and 2 lb1.SelectionMode = SelectionMode.MultiSimple; lb1.SelectedItems.Add(lb1.Items[0]); lb1.SelectedItems.Add(lb1.Items[2]); Form1 frm1 = new Form1(); // The receiving instance should be figured out in another way string[] items = new string[lb1.SelectedItems.Count]; for (int counter = 0; counter < lb1.SelectedItems.Count; counter++) { items[counter] = lb1.SelectedItems[counter].ToString(); } frm1.AddListboxItems(items);
Note that the receiving form is created in the code so in a real situation you probably need to have a reference to an already existing form.
这篇关于如何在标签中显示文本框的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!