質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

2回答

5514閲覧

C# WPF でtextblockの配列処理を行いたい。

suimi

総合スコア8

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

0クリップ

投稿2018/01/24 11:28

編集2018/01/26 05:29

一度回答をいただいたものの、うまく数値の範囲制限のかけ方が分からなかったので再質問しています。
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%以上パクリではありますができました!

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Zuishin

2018/01/26 05:25

できましたね。おめでとうございます。自己解決してください。それでこの質問は他の人の参考になる、価値ある質問になります。
guest

回答2

0

ベストアンサー

コメント欄では書きにくいので回答欄に書きます。

「うまくできませんでした」ではいけません。
teratail の質問と回答は、単なる個人間のサポートではなく、ノウハウの蓄積の役目もあります。
困った問題を検索したら Stack Overflow や teratail の回答が出てきて助けられることもよくあります。
ですから、そのような他人に役立つ質問にしましょう。

ここで問題となっているのは二カ所。
「LINQ の使い方がわからない」ということと「正規表現の使い方がわからない」ということです。

どちらも非常に便利で極めて頻出する重要な技術ですから、調べて基礎から学習することを強く強くお勧めします。

それと、質問する時には、前の質問を読んだ前提はやめましょう。
初めて見る人にとっては全く意味不明な質問です。

また、正しく動くソースならそれだけで説明になりますが、間違ったソースは何の説明にもなりません。
説明が決定的に不足しています。

ここで推奨されるタイトルは「うまくできませんでした」ではなく「文字列の中に一定範囲内の数値が含まれるかどうか判定したい」です。
初めての人にもどのような質問かわかるよう書き直し、次のソースを使ってください。

C#

1// MainWindow 中には複数の TextBlock が含まれます。 2// TextBlock はそれぞれ TextBlock1, TextBlock2... のように名付けられています。 3class MainWindow : Window 4{ 5 private IList<TextBlock> textBlocks; 6 7 // parent の子孫要素から全ての TextBlock を抽出して返すメソッド 8 private IEnumerable<TextBlock> FindTextBlocks(DependencyObject parent) 9 { 10 foreach (var child in LogicalTreeHelper.GetChildren(parent).OfType<DependencyObject>()) 11 { 12 var textBlock = child as TextBlock; 13 if (textBlock != null) yield return textBlock; 14 else foreach (var grandchild in FindTextBlocks(child)) yield return grandchild; 15 } 16 } 17 18 public MainWindow() 19 { 20 InitializeComponent(); 21 textBlocks = FindTextBlocks(this) 22 .Where(a => FilterByName(a.Name, 50, 80)) 23 .ToList(); 24 } 25 26 private bool FilterByName(string name, int min, int max) 27 { 28 // name が TextBlock1, TextBlock2... のような "TextBlock" + 数値でないときは false を返します。 29 // それ以外の場合、数値が min 以上 max 以下であれば true を、そうでなければ false を返します。 30 } 31}

あとは FilterByName の中身を書けば完成するわけですが、質問に挙げられた中身がひどすぎます。
これでは丸投げと判断されても文句が言えません。
C# で正規表現を使って数値を取り出す方法など探せばすぐに見つかるのですから、FilterByName の中身を埋められるよう少し頑張ってみてください。

投稿2018/01/24 12:50

Zuishin

総合スコア28660

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

suimi

2018/01/26 05:34

前の質問からお世話になりました.ありがとうございました. 細かい目的別に調べていく中でMatchがあまり出てこなかったので,今後の訓練も兼ねて見たもので構成したものを残すことにしました.
guest

0

C#

1bool FilterByName(string name) 2{ 3 // name の数値部分が 50~80 なら true を、それ以外なら false を返す 4 5 // TextBlock(数字)のパターンを示す正規表現 6 Regex reg = new Regex(@"TextBlock(?<No>\d+$)"); 7 Match mat = reg.Match(name); 8 9 if (mat.Success) 10 { 11 // 数字部分を抜き出す 12 string no = mat.Result("${No}"); 13 int numberValue = -1; 14 15 // 整数に変換 16 if (int.TryParse(no, out numberValue)) 17 { 18 // 50以上80以下ならtrue 19 return numberValue >= 50 && numberValue <= 80; 20 } 21 22 } 23 24 return false; 25} 26 27public MainWindow() 28{ 29 InitializeComponent(); 30 textBlocks = FindTextBlocks(this) 31 .Where(a => FilterByName(a.Name)) 32 .OrderBy(a => a.Name) 33 .ToList(); 34}

投稿2018/01/24 13:08

ayuma0913

総合スコア8

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問