C# 替换循环中的按钮名称字母

C# 替换循环中的按钮名称字母,c#,for-loop,C#,For Loop,我正在用C#编写一个开发WindowsPhone8应用程序的代码 我有8个名为(f1、f2、f3……f8)的按钮 现在,我想将它们的文本值添加到数组中。我知道怎么做 但是需要8行代码 例如: Numbers[0] = Convert.ToInt32(f1.Content); Numbers[1] = Convert.ToInt32(f2.Content); . . . Numbers[7] = Convert.ToInt32(f8.Content); 有没有一种方法可以写一个循环来为我完成所有

我正在用C#编写一个开发WindowsPhone8应用程序的代码

我有8个名为(f1、f2、f3……f8)的按钮

现在,我想将它们的文本值添加到数组中。我知道怎么做 但是需要8行代码

例如:

Numbers[0] = Convert.ToInt32(f1.Content);
Numbers[1] = Convert.ToInt32(f2.Content);
.
.
.
Numbers[7] = Convert.ToInt32(f8.Content);
有没有一种方法可以写一个循环来为我完成所有的过程?大概是这样的:

for(int i=0; i < 8 ; i++)
    Numbers[i] = Convert.ToInt32(fX.Content);
int[] numbers = MainGrid.Children.OfType<Button>()
            .Where(x => x.Name.Contains("F"))
            .Select(x => Convert.ToInt32(x.Content))
            .ToArray();
for(int i=0;i<8;i++)
数字[i]=转换为32(fX.Content);
其中X以值1(f1)开始,然后是2,依此类推。
在C#中有什么方法可以做到这一点吗?

我假设您在代码隐藏中,因为您“可以”使用控件名

为此,请使用FindName函数():

for(int i=0;i<8;i++)
数字[i]=转换.ToInt32(((按钮)FindName(“f”+i)).Content);

这不是一种很好的XAML/WPF/MVVM方法,所以您可能希望设置一个绑定到数字列表的items控件(然后可以调用sum),但这应该适用于发布的场景。

假设您有一个名为
MainGrid
(它不必是网格控件,任何东西都可以工作。只需相应地调整代码即可)

因为如果您不熟悉LINQ,它可能会令人困惑和不清楚,所以这里有一种更传统的方法来做同样的事情:

//Unless you have a specific reason for wanting an int array, a list is easier to work with
List<int> numbers = new List<int>();

//Use .OfType<>() So that you don't have to cast each control to a button in the loop
foreach(Button button in MainGrid.Children.OfType<Button>())
{
    //If it's not one of our named buttons, continue the loop
    if(! button.Name.StartsWith("F")) continue;

    int buttonValue = Convert.ToInt32(button.Content);
    numbers.Add(buttonValue);
}
//除非您有需要int数组的特定原因,否则列表更容易使用
列表编号=新列表();
//使用.OfType(),这样就不必将每个控件强制转换为循环中的按钮
foreach(MainGrid.Children.OfType()中的按钮)
{
//如果它不是我们命名的按钮之一,请继续循环
如果(!button.Name.StartsWith(“F”))继续;
int buttonValue=Convert.ToInt32(button.Content);
数字。添加(按钮值);
}

欢迎来到Stack Overflow!我已经编辑了您的标题。请参阅“问题是否应该在标题中包含“标记?”,其中的共识是“不,不应该”。可能是文本框转换的重复?但我处理的是按钮,而不是文本框。抱歉。修复了!虽然概念相同,但我只是做了错误的转换。请注意,这仅在网格上只有这些按钮时有效。您需要一个“where”如果还有其他人,就在名字上加上子句。这是真的。很好call@Jedediah什么是X?在本例中,
X
是一个本地匿名变量。基本上,它是“当前”按钮。为了更清楚,可以这样写:
。选择(按钮=>Convert.ToInt32(按钮内容))
x
在使用LINQ+1时只是一种典型的速记,这是一个很好的答案,尽管LINQ似乎有点过分:)
<Grid x:Name="MainGrid">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Button Grid.Row="0" Grid.Column="0" Content="1" x:Name="F1" />
    <Button Grid.Row="1" Grid.Column="0" Content="2" x:Name="F2" />
    <Button Grid.Row="2" Grid.Column="0" Content="3" x:Name="F3" />
    <Button Grid.Row="3" Grid.Column="0" Content="Do not count me" />
</Grid>
int[] numbers = MainGrid.Children.OfType<Button>()
            .Where(x => x.Name.Contains("F"))
            .Select(x => Convert.ToInt32(x.Content))
            .ToArray();
//Unless you have a specific reason for wanting an int array, a list is easier to work with
List<int> numbers = new List<int>();

//Use .OfType<>() So that you don't have to cast each control to a button in the loop
foreach(Button button in MainGrid.Children.OfType<Button>())
{
    //If it's not one of our named buttons, continue the loop
    if(! button.Name.StartsWith("F")) continue;

    int buttonValue = Convert.ToInt32(button.Content);
    numbers.Add(buttonValue);
}