如何与另一种方法中的变量(仅在程序中创建)进行交互?

时间:2015-01-30 09:15:00

标签: c# windows-phone-8.1

在第一种方法中,我创建了一个文本框矩阵和另一个(Button_click)方法。 我需要从Button_Click方法中获取文本框的值,然后对它们执行某些操作。我不知道如何与刚刚创建的值进行交互(名称也是新的)。但我知道我无法使用t[j]

 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)
    {
       // Double sa11v = Convert.ToDouble(t[j].Text);

    }

对不起我的英语,我来自俄罗斯:)

4 个答案:

答案 0 :(得分:1)

您可以创建一个在您创建TextBox Array的事件之外使用的成员变量 -

    TextBox[] _t = null;
    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;

            _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)
    {
        if (_t != null)
        {
            //Do your work here
            // Double sa11v = Convert.ToDouble(t[j].Text);
        }

    }

但是从更广泛的方面来说,你可以使用字典,

Dictionary<string, TextBox> _dicTextBoxes;

    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;

            _dicTextBoxes = new Dictionary<string, TextBox>();
            for (int j = 0; j < selectedIndex; j++)
            {
                TextBox txtBox = new TextBox();

                txtBox = new TextBox();
                txtBox.Name = "a" + (i + 1) + (j + 1);
                txtBox.Text = "a" + (i + 1) + (j + 1);
                v[i].Children.Add(txtBox);

                Thickness m = txtBox.Margin;
                m.Left = 1;
                m.Bottom = 1;
                txtBox.Margin = m;

                InputScope scope = new InputScope();
                InputScopeName name = new InputScopeName();
                name.NameValue = InputScopeNameValue.TelephoneNumber;
                scope.Names.Add(name);
                txtBox.InputScope = scope;

                _dicTextBoxes.Add(txtBox.Name, txtBox);
            }
            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)
    {
        if (_dicTextBoxes != null)
        {
            // a23 is the name of textbox, 'a' is prefixe, '2' is the 2nd stackpanel you have added
            // '3' is the 3rd textbox you have added in stackpanel
            if (_dicTextBoxes.ContainsKey("a23"))
            {
                //Do your work here
                Double sa11v = Convert.ToDouble(_dicTextBoxes["a23"].Text);
            }
        }

    }

答案 1 :(得分:1)

如果是Children,则循环遍历mainpanelcheck each controlTextBox。类似的东西:

foreach (UIElement ctrl in mainpanel.Children)
{
    if (ctrl.GetType() == typeof(TextBox))
    {
        TextBox oneOfYourTextBoxes = ((TextBox)ctrl);
        // do your thing
    }
}

答案 2 :(得分:1)

创建数组后,无法更改数组大小。因此,在触发事件后,您的大小仅在运行时才知道,您无法在声明中设置其大小。

class MyClass 
{
    Textbox[] myTextBoxes;


    private void vsematrici_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    {
        // ...
        myTextBoxes = new TextBoxes[selectedIndex];
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        int myIndex = // your selected item here
        if (this.myTextBoxes != null && myIndex < this.myTextBoxes.Length)
        {
            Console.WriteLine(this.myTextBoxes[myIndex].Text);
        }
    }
}

答案 3 :(得分:1)

这也有效:

button1.Click += (sender, args) => DoSomethingToTextboxes(_t);