一度回答をいただいたものの、うまく数値の範囲制限のかけ方が分からなかったので再質問しています。
C#のWPFアプリケーションでTextBlockをC言語の変数および配列のように扱って反復処理に制限を設けたり,番号を他の変数で指定したいと考えています.
現在は勉強中なので,解決した場合,質問上に載せているプログラム変更とBAの決定を行いたいと思います.
プログラムの修正に目が行き過ぎて,質問文の修正を読んですぐ忘れてました.
修正が遅くなりましてごめんなさい.
Zuishinさん,ayuma0913さん.ありがとうございます.
次が途中経過です.この文では動きませんでした.
if(name.IndexOf("TextBlock")<9)の時点でTrueに入れてませんでした.
C#
1private bool FilterByName(string name, int min, int max) 2 { 3 // name が TextBlock1, TextBlock2... のような "TextBlock" + 数値でないときは false を返します。 4 // それ以外の場合、数値が min 以上 max 以下であれば true を、そうでなければ false を返します。 5 int change; 6 7 if(name.IndexOf("TextBlock")<9) 8 { 9 return false; 10 } 11 else 12 { 13 Regex num = new Regex(@"[^0-9]"); 14 string a = num.ToString(); 15 change = int.Parse(a); 16 17 TextBlock01.Text = change.ToString(); 18 19 if (change > min && change < max) 20 { 21 return true; 22 } 23 else 24 { 25 return false; 26 } 27 } 28 29 }
ayuma0913さんのような感じでMatchでもできました.
今後のためにMatchを知らない前提で目的に合わせて調べて試してを繰り返してできたものを残します.
ありがとうございました.
XAML
1<Window x:Class="WpfApp1.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApp1" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="350" Width="525"> 9 <Grid Margin="20"> 10 <Grid.RowDefinitions> 11 <RowDefinition Height="Auto"/> 12 <RowDefinition Height="Auto"/> 13 <RowDefinition Height="Auto"/> 14 <RowDefinition/> 15 <RowDefinition Height="Auto"/> 16 </Grid.RowDefinitions> 17 <TextBlock Grid.Row="0" Name="TextBlock01"/> 18 <TextBlock Grid.Row="1" Name="TextBlock02"/> 19 <TextBlock Grid.Row="2" Name="TextBlock03"/> 20 <Button Grid.Row="4" Width="90" Height="21" Click="Button_Click">Click me!</Button> 21 </Grid> 22</Window>
C#
1using System.Collections.Generic; 2using System.Linq; 3using System.Text.RegularExpressions; 4using System.Windows; 5using System.Windows.Controls; 6 7namespace WpfApp1 8{ 9 // MainWindow 中には複数の TextBlock が含まれます。 10 // TextBlock はそれぞれ TextBlock1, TextBlock2... のように名付けられています。 11 public partial class MainWindow : Window 12 { 13 private IList<TextBlock> textBlocks; 14 15 // parent の子孫要素から全ての TextBlock を抽出して返すメソッド 16 private IEnumerable<TextBlock> FindTextBlocks(DependencyObject parent) 17 { 18 foreach (var child in LogicalTreeHelper.GetChildren(parent).OfType<DependencyObject>()) 19 { 20 var textBlock = child as TextBlock; 21 if (textBlock != null) yield return textBlock; 22 else foreach (var grandchild in FindTextBlocks(child)) yield return grandchild; 23 } 24 } 25 26 public MainWindow() 27 { 28 InitializeComponent(); 29 textBlocks = FindTextBlocks(this) 30 .Where(a => FilterByName(a.Name, 1, 2)) 31 .ToList(); 32 } 33 34 private bool FilterByName(string name, int min, int max) 35 { 36 // name が TextBlock1, TextBlock2... のような "TextBlock" + 数値でないときは false を返します。 37 // それ以外の場合、数値が min 以上 max 以下であれば true を、そうでなければ false を返します。 38 int change; 39 string s1 = name.Substring(0, 9); 40 41 string s2 = Regex.Replace(name, @"[^\d]", ""); 42 43 if (s1!="TextBlock") 44 { 45 return false; 46 } 47 else 48 { 49 change = int.Parse(s2); 50 51 52 if (change >= min && change <= max) 53 { 54 return true; 55 } 56 else 57 { 58 return false; 59 } 60 } 61 62 } 63 private void Button_Click(object sender, RoutedEventArgs e) 64 { 65 foreach (var textBlock in textBlocks) 66 { 67 textBlock.Text = "I am " + textBlock.Name; 68 } 69 } 70 } 71}
90%以上パクリではありますができました!
回答2件
あなたの回答
tips
プレビュー