I need to bind an array of dynamically created textboxes to a string[] or List<string>. This was the closest WinForm Controls binding to a List<T> problem but no cigar.
Typically for single textboxes I bind the Textboxes' Text property:
Engine engine = new Engine();
public frmMain()
{
InitializeComponent();
txtQuery.DataBindings.Add("Text",engine,"Query");
}
To a class property:
public class Engine : IEngine, INotifyPropertyChanged
{
private string query;
public string Query
{
get { return query; }
set
{
query = value;
InvokePropertyChanged(new PropertyChangedEventArgs("Query"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void InvokePropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, e);
}
}
I've given it a go with an array of textboxes and strings without luck:
Front End:
TextBox[] txtBoxArr = new TextBox[numberOfDimensions];
for (int i = 0; i < numberOfDimensions;i++)
{
string tabName = "Dataset" + (i + 1);
tabCtrlDatasets.TabPages.Add(tabName,tabName);
txtBoxArr[i] = new TextBox();
txtBoxArr[i].Name = "txtDataset" + i ;
txtBoxArr[i].DataBindings.Add("Text",engine,"Dataset");
tabCtrlDatasets.TabPages[i].Controls.Add(txtBoxArr[i]);
}
Back End:
private string[] dataset;
public string[] Dataset
{
get { return dataset; }
set
{
dataset = value;
InvokePropertyChanged(new PropertyChangedEventArgs("Dataset"));
}
}
To get it working I need to know the index of the item in the array. I cant recall doing this before, does anyone know how to identify the index of the textbox to bind it to the correct item in the string array?
I'm a bit tired today and having a memory block.