Home > DeveloperSection > Forums > Hot to get value in label of Multi Selected Listbox?
Samuel Fernandes
Samuel Fernandes

Total Post:156

Points:1096
Posted on    October-21-2014 12:14 AM

 C# C#  .NET 
Ratings:


 2 Reply(s)
 646  View(s)
Rate this:
Hi

How to get listbox multiple selected values in label or string.
I have bind data from table in listbox then when i select multi values then this display in label or string
when i try get values in label then display System.Data.Dataview multi time
my code

  for (i = 0; i < lstcsuspectcode.SelectedItems.Count; i++)
  {

  label18.Text = label18.Text + " " + lstcsuspectcode.SelectedValue;
  
  }


Royce Roy
Royce Roy

Total Post:134

Points:938
Posted on    October-21-2014 12:15 AM

You can try this code.

 string s = "";
        foreach (ListItem li in ListBox1.Items) {

            if (li.Selected == true) {
                s = s +" "+ li.Value;
            }

        }

Mark Devid
Mark Devid

Total Post:102

Points:714
Posted on    October-21-2014 12:16 AM

HI Guyst try this...


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listBox1.Items.Add("a");
            listBox1.Items.Add("b");
            listBox1.Items.Add("c");
            listBox1.Items.Add("d");
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            int i;
            for (i = 0; i < listBox1.SelectedItems.Count; i++)
            {

                label1.Text = label1.Text + " " + listBox1.SelectedItem.ToString();

            }
        }
    }
}


Modified On Jun-26-2016 11:11:15 PM

Don't want to miss updates? Please click the below button!

Follow MindStick