Reputation: 27
I'm creating a matrix of textboxes, then I want to input some values in these textboxes. After that by clicking a button below my matrix, program should get values (This is the main problem!) I suppose that I can make it using foreach UIElement
but it doesn't work... I'm attaching a screenshot and a code, please correct it!
private void vsematrici_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int selectedIndex = vsematrici.SelectedIndex + 2;
StackPanel[] v = new StackPanel[selectedIndex];
for (int i = 0; i < selectedIndex; i++)
{
v[i] = new StackPanel();
v[i].Name = "matrixpanel" + i;
v[i].Orientation = Orientation.Horizontal;
TextBox[] t = new TextBox[selectedIndex];
for (int j = 0; j < selectedIndex; j++)
{
t[j] = new TextBox();
t[j].Name = "a" + (i + 1) + (j + 1);
t[j].Text = "a" + (i + 1) + (j + 1);
v[i].Children.Add(t[j]);
Thickness m = t[j].Margin;
m.Left = 1;
m.Bottom = 1;
t[j].Margin = m;
InputScope scope = new InputScope();
InputScopeName name = new InputScopeName();
name.NameValue = InputScopeNameValue.TelephoneNumber;
scope.Names.Add(name);
t[j].InputScope = scope;
}
mainpanel.Children.Add(v[i]);
}
Button button1 = new Button();
button1.Content = "Найти определитель";
button1.Click += Button_Click;
mainpanel.Children.Add(button1);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
myresult.Text = "After button clicking there should be shown a matrix of texboxes values";
foreach (UIElement ctrl in mainpanel.Children)
{
if (ctrl.GetType() == typeof(TextBox))
{
//There should be a a two-dimensional array that I want to fill with textboxes' values
//But even this "if" doen't work!!! I don't know why...
}
}
}
Upvotes: 1
Views: 1067
Reputation: 26846
You are adding some StackPanel
to your mainPanel
and then you're adding textboxes to that stackPanels.
But here:
foreach (UIElement ctrl in mainpanel.Children)
{
if (ctrl.GetType() == typeof(TextBox))
{
you're trying to find these textboxes as they were children of mainPanel
- of course you can't find them this way.
So you could change your code according to your logic like this:
foreach (UIElement pnl in mainpanel.Children)
{
if (pnl is StackPanel)
{
foreach (UIElement ctrl in (pnl as StackPanel).Children)
{
if (ctrl is TextBox)
{
// your logic here
}
}
}
}
Upvotes: 1