carlAlex
8/22/2016 - 10:26 AM

C# listbox - DataSource

C# listbox - DataSource

        /// <summary> 
         /// Updates the running process list view and 
         /// returns the number of processes found. 
         /// </summary> 
         /// <param name="lv"></param> 
         /// <returns></returns> 
         public int UpdateAppList(ListView lv) 
         { 
             try 
             { 
                 var lvItems = from proc in Process.GetProcesses() 
                               where proc.MainWindowTitle.Length > 0 
                               select new ListViewItem(new string[] { proc.ProcessName, proc.MainWindowTitle, proc.Id.ToString() }); 

                 lv.Items.AddRange(lvItems.ToArray()); 
                 return lvItems.ToArray().Length; 
             } 
             catch { return 0; } 
         }
         
        public Form1() 
         { 
             InitializeComponent(); 

             listView1.View = View.Details; 
             listView1.Columns.Add("Process Name", 175); 
             listView1.Columns.Add("Process Window", 175); 
             listView1.Columns.Add("Process Id", 75); 

             int nCount = UpdateAppList(this.listView1); 
             this.Text = "Processes found: " + nCount.ToString(); 
         }
*the sortedList already exists, and we need a templist..

List<SearchTerm> sortedList = searchTerms.OrderBy(x => x.Description).ToList();

                    searchTerms.Clear();

                    foreach (var item in sortedList)
                    {
                        searchTerms.Add(item);
                    }
          private int GetWindowsRunning() 
         { 
             try 
             { 
                 var processes = from proc in Process.GetProcesses() 
                                 where proc.MainWindowTitle.Length > 0 
                                 select proc.MainWindowTitle; 

                 foreach (string title in processes) 
                 { 
                     listBox1.Items.Add(title); 
                 } 

                 return processes.ToArray().Length; 
             } 
             catch { return 0; } 
         } 
         
         
         //OR
         
        private int GetWindowsRunning() 
         { 
             try 
             { 
                 listBox1.DataSource = (from proc in Process.GetProcesses() 
                                        where proc.MainWindowTitle.Length > 0 
                                        select proc.MainWindowTitle).ToArray(); 
                 
                 return listBox1.Items.Count; 
             } 
             catch { return 0; } 
         }
//public List<Transaction> Transactions;

listBoxTransactions.DataSource = tMgr.Transactions;
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;

namespace MyListControlSample
{
    public class ListBoxSample3 : Form
    {
        private ListBox ListBox1 = new ListBox();
        private Label label1 = new Label();
        private TextBox textBox1 = new TextBox();

        [STAThread]
        static void Main()
        {
            Application.Run(new ListBoxSample3());
        }

        public ListBoxSample3()
        {
            this.ClientSize = new Size(307, 206);
            this.Text = "ListBox Sample3";

            ListBox1.Location = new Point(54, 16);
            ListBox1.Name = "ListBox1";
            ListBox1.Size = new Size(240, 130);

            label1.Location = new Point(14, 150);
            label1.Name = "label1";
            label1.Size = new Size(40, 24);
            label1.Text = "Value";

            textBox1.Location = new Point(54, 150);
            textBox1.Name = "textBox1";
            textBox1.Size = new Size(240, 24);

            this.Controls.AddRange(new Control[] { ListBox1, label1, textBox1 });

            // Populate the list box using an array as DataSource.
            ArrayList USStates = new ArrayList();
            USStates.Add(new USState("Alabama", "AL"));
            USStates.Add(new USState("Washington", "WA"));
            USStates.Add(new USState("West Virginia", "WV"));
            USStates.Add(new USState("Wisconsin", "WI"));
            USStates.Add(new USState("Wyoming", "WY"));
            ListBox1.DataSource = USStates;

            // Set the long name as the property to be displayed and the short
            // name as the value to be returned when a row is selected.  Here
            // these are properties; if we were binding to a database table or
            // query these could be column names.
            ListBox1.DisplayMember = "LongName";
            ListBox1.ValueMember = "ShortName";

            // Bind the SelectedValueChanged event to our handler for it.
            ListBox1.SelectedValueChanged += 
                new EventHandler(ListBox1_SelectedValueChanged);

            // Ensure the form opens with no rows selected.
            ListBox1.ClearSelected();
        }

        private void InitializeComponent()
        {
        }

        private void ListBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (ListBox1.SelectedIndex != -1)
            {
                textBox1.Text = ListBox1.SelectedValue.ToString();
                // If we also wanted to get the displayed text we could use
                // the SelectedItem item property:
                // string s = ((USState)ListBox1.SelectedItem).LongName;
            }
        }
    }

    public class USState
    {
        private string myShortName;
        private string myLongName;

        public USState(string strLongName, string strShortName)
        {

            this.myShortName = strShortName;
            this.myLongName = strLongName;
        }

        public string ShortName
        {
            get
            {
                return myShortName;
            }
        }

        public string LongName
        {

            get
            {
                return myLongName;
            }
        }

    }
}
public partial class MyForm:Form
{

    public MyForm(){
        InitializeComponent();
        ShowData();
    }

    BindingList<MyData> data = new BindingList<MyData>();

    private void ShowData()
    {
       listBox1.DataSource = data;
       listBox1.DisplayMember = "Name";
       listBox1.ValueMember = "Id";
    }

    private void buttonAddData_Click(object sender, EventArgs e)
    {
       var selection = (MyData)comboBox1.SelectedItem;
       data.Add(selection);
    }
}