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?