using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; namespace TaskMan { public partial class ListEntry : UserControl { public ListEntry() { InitializeComponent(); flowLayoutPanel1.Width = this.Width - 35; addEntryBox(); } // We ensure that there is always one and only one empty box. private TextBox emptyControl; private List entryControls=new List(); private Dictionary m_tasks = new Dictionary(); private void textBox1_TextChanged(object sender, EventArgs e) { if (!loadingTasks) { TextBox sendingControl = (TextBox)sender; if (sendingControl == emptyControl) { // Text has been entered in the empty box, so create a new task associated with this //control and create a new empty box. m_tasks.Add(sendingControl, new Task(sendingControl.Text)); addEntryBox(); } else if (sendingControl.Text == "") { // one box has become empty. This becomes the one empty box, so the other is deleted emptyControl.Parent = null; entryControls.Remove(emptyControl); emptyControl = sendingControl; m_tasks.Remove(sendingControl); } else { m_tasks[sendingControl].desc = sendingControl.Text; } } } private void addEntryBox(){ TextBox tb = new TextBox(); emptyControl = tb; tb.Width = flowLayoutPanel1.Width-5; tb.TextChanged += textBox1_TextChanged; tb.Parent = flowLayoutPanel1; entryControls.Add(tb); } private bool loadingTasks=false; public void setTask(List S){ m_tasks.Clear(); loadingTasks = true; foreach (Task s in S) { emptyControl.Text = s.desc; m_tasks.Add(emptyControl, s); addEntryBox(); } loadingTasks = false; } public List tasks() { List t=new List(); foreach (KeyValuePair v in m_tasks) { t.Add(v.Value); } // foreach (TextBox tb in entryControls) { // // one text box is always empty, so ignore it. // if (tb != emptyControl) t.Add(new Task(tb.Text)); // } return t; } } }