3

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.

2
  • If someone see's this can they please give me a hint as to why my questions end up destined for a "Tumbleweed" badge? Commented Feb 26, 2013 at 8:33
  • 1
    I am working on it :) Commented Feb 26, 2013 at 8:36

1 Answer 1

1

Instead of a string array for holding the TextBox values, you could use a DataTable with a single row. In this way, you don't have to increase the complexity of your Engine class (as it will still have a single property) and you can bind the textboxes to the engine.DataTable's DataColumnCollection (mapping them by the column's index).

Try this in the form:

Engine engine = new Engine();

public Form1()
{
    InitializeComponent();

    var dt = new DataTable();
    dt.Columns.Add("column0");
    dt.Columns.Add("column1");
    dt.Columns.Add("column2");
    dt.Rows.Add("abc", DateTime.Now, 123456, 789.0123F);
    engine.DataTable = dt;

    int numberOfDimensions = engine.DataTable.Columns.Count;
    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 = engine.DataTable.Columns[i].ColumnName;
        txtBoxArr[i].DataBindings.Add("Text", 
            engine.DataTable, txtBoxArr[i].Name);
        tabCtrlDatasets.TabPages[i].Controls.Add(txtBoxArr[i]);
    }
}

and modify the Engine class like this:

private DataTable dataTable;
public DataTable DataTable
{
    get
    {
        return dataTable;
    }
    set
    {
        dataTable = value;
        InvokePropertyChanged(new PropertyChangedEventArgs("DataTable"));
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I'm open to this approach, I'll give it go tomorrow. +1 Thanks. If you're awake could you please elaborate on your findings with binding a control[], what made you resort to a DataTable?
Thanks for your help on this Alex, am still keen to hear alternatives but marking your answer as correct in the mean time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.