Przy pomocy: System.Linq i Where
using System.Linq;
Przykład
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Name="gridMain">
<TextBox Height="18" HorizontalAlignment="Left" Margin="29,20,0,0" Name="tbDaneA1" VerticalAlignment="Top" Width="69" Text="LoremA1" />
<TextBox Height="18" HorizontalAlignment="Left" Margin="29,44,0,0" Name="tbDaneB1" VerticalAlignment="Top" Width="69" Text="LoremB1" />
<TextBox Height="18" HorizontalAlignment="Left" Margin="29,68,0,0" Name="textBox3" VerticalAlignment="Top" Width="69" />
<TextBox Height="18" HorizontalAlignment="Left" Margin="116,20,0,0" Name="tbDaneC1" VerticalAlignment="Top" Width="69" Text="LoremC1" />
<TextBox Height="18" HorizontalAlignment="Left" Margin="116,44,0,0" Name="textBox5" VerticalAlignment="Top" Width="69" />
<TextBox Height="18" HorizontalAlignment="Left" Margin="116,68,0,0" Name="textBox6" VerticalAlignment="Top" Width="69" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="212,20,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>
private void button1_Click(object sender, RoutedEventArgs e)
{
var textBoxesToClear = gridMain.Children
.OfType<TextBox>()
.Where(tb => tb.Name.StartsWith("tbDane"));
foreach (var textBox in textBoxesToClear)
{
textBox.Clear();
}
}
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
var textBoxesToClear = gridMain.Children
.OfType<TextBox>()
.Where(tb => tb.Name.StartsWith("tbDane"));
foreach (var textBox in textBoxesToClear)
{
textBox.Clear();
}
}
}
}
lub z użyciem tablicy nazw kontrolek
private void button1_Click(object sender, RoutedEventArgs e)
{
//var textBoxesToClear = gridMain.Children
// .OfType<TextBox>()
// .Where(tb => tb.Name.StartsWith("tbDane"));
//foreach (var textBox in textBoxesToClear)
//{
// textBox.Clear();
//}
string[] textBoxNames = { "tbDaneA1", "tbDaneB1", "tbDaneC1" };
foreach (string textBoxName in textBoxNames)
{
var textBox = (TextBox)gridMain.FindName(textBoxName); // Znajdź kontrolkę po nazwie
if (textBox != null)
{
textBox.Clear();
}
}
}
using System;
//using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//var textBoxesToClear = gridMain.Children
// .OfType<TextBox>()
// .Where(tb => tb.Name.StartsWith("tbDane"));
//foreach (var textBox in textBoxesToClear)
//{
// textBox.Clear();
//}
string[] textBoxNames = { "tbDaneA1", "tbDaneB1", "tbDaneC1" };
foreach (string textBoxName in textBoxNames)
{
var textBox = (TextBox)gridMain.FindName(textBoxName); // Znajdź kontrolkę po nazwie
if (textBox != null)
{
textBox.Clear();
}
}
}
}
}