Reputation: 87
I have about 100 textboxes on a page, and each textbox has its own corresponding value in an array, each textbox has a method which is invoked when the value in the textbox is changed, and updates the corresponding elements of the array, to reflect that value it has been changed to. (In theory)
However, is there a way that you can adjust the below method, so that rather than writing it out 100 times, with a changing name "_8_8_TextChanged", and changing the values that it changes manually, and doing it so that 1 method is called by all textboxes, and the method recognises which textbox called it, and updates the relevant elements in the array?
The Method is defined below and features on the "Solver.xaml.cs" page.
private void _8_8_TextChanged(object sender, TextChangedEventArgs e)
{
int number = int.Parse(_8_8.Text);
if ((number >= 1) && (number <= 9))
{
for (int i = 0; i <= 8; i++)
{
if (i == (number - 1))
{
content[8, 8, i] = true;
}
else
{
content[8, 8, i] = false;
}
}
}
}
The XAML textbox itself is defined below and features on the "Solver.xaml" page, with its styling elements removed for simplicity.
<TextBox x:Name="_8_8" TextChanged="_8_8_TextChanged"/>
Upvotes: 0
Views: 681
Reputation: 18803
I really hope you have a good reason for using that many text boxes. In any case you can use the same event handler for all of your TextChange events, as follows.
All textboxes would be set up to use the same handler:
<TextBox x:Name="_8_8" TextChanged="_x_y_TextChanged"/>
<TextBox x:Name="_8_9" TextChanged="_x_y_TextChanged"/>
You can then update your array based on the sending text box:
private void _x_y_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox tb = (TextBox)sender;
// use the Name of the textbox to determine x, y value
string[] tmp_x_y = tb.Name.Split("_");
// you may have to adjust these indices based on how Split actually
// does its work.
int x = int.Parse(tmp_x_y[0]);
int y = int.Parse(tmp_x_y[1]);
int number = int.Parse(tb.Text);
if ((number >= 1) && (number <= 9))
{
for (int i = 0; i <= 8; i++)
{
if (i == (number - 1))
{
content[x, y, i] = true;
}
else
{
content[x, y, i] = false;
}
}
}
}
I didn't actually compile the code above, but it should give you a good starting point.
Upvotes: 3