Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / May 2008

Tip: Looking for answers? Try searching our database.

ListBox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rogersteph@googlemail.com - 30 Apr 2008 23:51 GMT
I would like to write a small program where a user can input several
data. The data is then displayed in a listBox control and the user can
"jump" to several entries (and edit them there) by just selecting
items in the listBox.

My problem is that once I've added items to the listbox I cannot
retrieve those items anymore.
Probably I completely misunderstand how the listBox is intended to
work, but I've done something like:

First create a class that stores all the information and override the
toString so that each Item displays correctly in the listBox after
adding:

class Entry
   {
       public string[] Data;

       public Entry()
       {
           Data = new string[6];
       }

       public override string ToString()
       {
               if(Data[0]!= null)
               {
                   return Data[0];
               }
               else {
                   return "New";
               }

       }

Now in the main program I've added several of such instances of those
classes to the listBox:

listBoxKanji.Items.Add(currentEntry);

however when later trying to access these data I get (sometimes, not
always) a Null Pointer Exception:

Entry currentEntry = listBoxKanji.SelectedItem;

What I want to do is: When a user selects a certain item of the
listbox, I'd like to access the data of that selection (i.e. the items
stored in the listbox) and populate some textboxes with that
information.

How would I do that?
Morten Wennevik [C# MVP] - 02 May 2008 07:41 GMT
Hi,

I've attached a code sample demonstrating how you do this using
DataBinding/BindingSource and business objects.  Using the databinding you
can display various part of information.  The ListBox display all items
inside a BindingSource and reports back to the binding source information
about the selected item.  The BindingSource will in turn update the TextBoxes
with the currently selected item and any editing in the TextBoxes will be
reported back to the BindingSource -> ListBox as well.  

To achieve all this we need a business object (Entry) as well as a
collection of Entry objects, which could be inside another business object,
but in the code sample is located inside Form1.

The Entries collection is a BindingList rather than List since BindingList
will automatically report to the BindingSource when the list is changed
(which will then update the ListBox etc).

To run the code, create a new Windows application and change the Form1 class
in the code file with the code below.  You will also need to add using
System.ComponentModel; to the using statements for the BindingList.

public partial class Form1 : Form
{
   private ListBox lbEntry = new ListBox();
   private TextBox tbName = new TextBox();
   private TextBox tbValue = new TextBox();
   private Button btAddEntry = new Button();

   private BindingList<Entry> _entries = new BindingList<Entry>();

   public BindingList<Entry> Entries
   {
       get { return _entries; }
       set { _entries = value; }
   }

   public Form1()
   {
       InitializeComponent();
   }

   protected override void OnLoad(EventArgs e)
   {
       Controls.Add(lbEntry);
       Controls.Add(tbName);
       Controls.Add(tbValue);
       Controls.Add(btAddEntry);

       tbName.Location = new Point(lbEntry.Right + 5, lbEntry.Top);
       tbValue.Location = new Point(lbEntry.Right + 5, tbName.Bottom + 5);
       btAddEntry.Location = new Point(lbEntry.Left, lbEntry.Bottom + 5);

       btAddEntry.Text = "Add Entry";
       btAddEntry.Click += new EventHandler(btAddEntry_Click);

       BindingSource bs = new BindingSource(this, "Entries");

       lbEntry.DataSource = bs;
       lbEntry.DisplayMember = "Name";

       tbName.DataBindings.Add("Text", bs, "Name", false,
DataSourceUpdateMode.OnPropertyChanged);
       tbValue.DataBindings.Add("Text", bs, "Value", false,
DataSourceUpdateMode.OnPropertyChanged);            
   }

   void btAddEntry_Click(object sender, EventArgs e)
   {
       Entries.Add(new Entry());
   }
}

public partial class Form1 : Form
{
   private ListBox lbEntry = new ListBox();
   private TextBox tbName = new TextBox();
   private TextBox tbValue = new TextBox();
   private Button btAddEntry = new Button();

   private BindingList<Entry> _entries = new BindingList<Entry>();

   public BindingList<Entry> Entries
   {
       get { return _entries; }
       set { _entries = value; }
   }

   public Form1()
   {
       InitializeComponent();
   }

   protected override void OnLoad(EventArgs e)
   {
       Controls.Add(lbEntry);
       Controls.Add(tbName);
       Controls.Add(tbValue);
       Controls.Add(btAddEntry);

       tbName.Location = new Point(lbEntry.Right + 5, lbEntry.Top);
       tbValue.Location = new Point(lbEntry.Right + 5, tbName.Bottom + 5);
       btAddEntry.Location = new Point(lbEntry.Left, lbEntry.Bottom + 5);

       btAddEntry.Text = "Add Entry";
       btAddEntry.Click += new EventHandler(btAddEntry_Click);

       BindingSource bs = new BindingSource(this, "Entries");

       lbEntry.DataSource = bs;
       lbEntry.DisplayMember = "Name";

       tbName.DataBindings.Add("Text", bs, "Name", false,
DataSourceUpdateMode.OnPropertyChanged);
       tbValue.DataBindings.Add("Text", bs, "Value", false,
DataSourceUpdateMode.OnPropertyChanged);            
   }

   void btAddEntry_Click(object sender, EventArgs e)
   {
       Entries.Add(new Entry());
   }
}

Signature

Happy Coding!
Morten Wennevik [C# MVP]

> I would like to write a small program where a user can input several
> data. The data is then displayed in a listBox control and the user can
[quoted text clipped - 47 lines]
>
> How would I do that?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.