Dynamically set TextBox text

13,967

Solution 1

Use FindName(string) to find the text box by name as follows (where container is a control that contains all of the text boxes):

private void UpdateTextBox(int row, int column, string text)
{
    TextBox textBox = container.FindName("_" + row + "_" + column) as TextBox;
    if(textbox != null)
    {
        textbox.Text = text;
    }
}

Solution 2

There are two ways you might go:

If you have a lot of data to manage, or if you can't predict the length of the array, it would be better to bind to a collection instead of manually poking data into and out of an array. If you create a class derived from ObservableCollection instead of using an array the data <> ui relationship is pretty trivial.

if you really need to do this manually, maybe it would be better to stick the index into the 'tag' field of your text boxes. You could (a) see it clearly in your xaml, (b) parse it easily and (c) if you used a variation on the formula here:

Find all controls in WPF Window by type

you could iterate over the textboxes in window and find the right one by looking at its tag index:

    foreach (TextBox t in FindVisualChildren<TextBox>(this))
    {
        if ((int) t.Tag)  == my_index )
        {
            t.Text = "my_text_goes_here";
         }
    }

Solution 3

I would go in the direction of the answer I gave on this question: form anchor/dock In short, I would create a class that holds the actual values and then create a collection that holds information classes.

Then I would not use the event "TextChanged" on the TextBoxes, rather "sniff" for changes on the Dependency Property used to hold the text. This can easily be done in the Dependency Property.

Last, I would use an ItemsControl or ItemsPresenter to show the controls. Number of controls will follow number of items in the collection.

Share:
13,967
GArchitech
Author by

GArchitech

A Computer Science student at a London based university.

Updated on June 25, 2022

Comments

  • GArchitech
    GArchitech almost 2 years

    I have multiple XAML TextBoxes, each of which manipulate a corresponding value in an array, when the value in the TextBox is changed, using a C# method which dynamically checks which TextBox has called the method.

        <TextBox x:Name="_0_0" TextChanged="_x_y_TextChanged"/>
        <TextBox x:Name="_0_1" TextChanged="_x_y_TextChanged"/>
        <TextBox x:Name="_0_2" TextChanged="_x_y_TextChanged"/>
        // And so on.....
    

    each of which manipulate a corresponding value in an array, when the value in the TextBox is changed, using a C# method which dynamically checks which TextBox has called the method.

        private void _x_y_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox current = (TextBox)sender;
            string currentname = current.Name;
            string rowstring = currentname.Substring(1, 1);
    
            string columnstring = currentname.Substring(3, 1);
    
            int row = Convert.ToInt32(rowstring);
            int column = Convert.ToInt32(columnstring);
    
            // I've then detected the name of the textbox which has called it...
    

    So this information can be used to dynamically store information from a TextBox in a corresponding array index - or whatever you want to do with it...

    My question however, is:

    How can I create a method which uses index locations in my array, to call the relevant TextBox and update its text?