如何在标签中传递列表框值 [英] How to pass listbox values in label

查看:57
本文介绍了如何在标签中传递列表框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在列表框中有很多价值我想传递所有这些而没有选择其他形式的标签



我尝试过:



i试过这个,但它只显示我选择的价值



form1

i have many value in listbox i want to pass all of them without selection in label that is in other form

What I have tried:

i have tried this but its shows only the value i chose

form1

private void btn_voit_Click(object sender, EventArgs e)
        {
            string textboxchoix = listBox2.Text;           
            Form2 frm = new Form2(textboxchoix);
            frm.Show();
        }



标签中的form2




form2 in label

public Form2(string textboxchoix)
        {
            InitializeComponent();
           label_choix.Text = textboxchoix;
            
        }

推荐答案

列表框维护一个字符串集合,而标签只包含一个字符串。

如果您希望使用列表框项目字符串串联的结果设置标签文本,那么:

  • 通过迭代构建'连接字符串'在列表框项目上。
  • 将连接的字符串分配给标签文本。
The listbox maintains a collection of strings, while the label contains just one string.
If you wish to set the label text with the result of the concatenation of the listbox item strings then:
  • Build the 'concatenated string' by iterating over the listbox items.
  • Assign the concatenated string to your label text.


在第一种形式中使用Session

ArrayList al = new ArrayList();

for(int i = 0; i< ListBox1.Items.Count; i ++)

{

// if(ListBox1.Items [i] .Selected == true)

// {

al.Add(ListBox1.Items [i]。价值);

//}

}

会话[ListBoxItem] = al;



和第二张表格



lblResult.Text =;



string [] products =(string [])Session [ListBoxItem];



for(int i = 0;我< products.length; ++ i)

{



lblResult.Text + = products [i] +,;



}
In first form use Session
ArrayList al = new ArrayList();
for (int i = 0; i < ListBox1.Items.Count; i++)
{
// if (ListBox1.Items[i].Selected == true)
// {
al.Add(ListBox1.Items[i].Value);
// }
}
Session["ListBoxItem"] = al;

and second form

lblResult.Text = "";

string[] products = (string[])Session["ListBoxItem"];

for (int i = 0; i < products.length; ++i)
{

lblResult.Text += products[i] + ",";

}


这篇关于如何在标签中传递列表框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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