teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

最後に修正

2018/01/26 05:29

投稿

suimi
suimi

スコア8

title CHANGED
File without changes
body CHANGED
@@ -4,6 +4,8 @@
4
4
  プログラムの修正に目が行き過ぎて,質問文の修正を読んですぐ忘れてました.
5
5
  修正が遅くなりましてごめんなさい.
6
6
  Zuishinさん,ayuma0913さん.ありがとうございます.
7
+ 次が途中経過です.この文では動きませんでした.
8
+ if(name.IndexOf("TextBlock")<9)の時点でTrueに入れてませんでした.
7
9
 
8
10
  ```C#
9
11
  private bool FilterByName(string name, int min, int max)

4

ニュアンスの変更

2018/01/26 05:29

投稿

suimi
suimi

スコア8

title CHANGED
File without changes
body CHANGED
@@ -138,4 +138,4 @@
138
138
  }
139
139
  }
140
140
  ```
141
- 90%以上パクリではありますができました
141
+ 90%以上パクリではありますができました

3

状況の進展に伴い,目的が完遂され,この記事を後に見る人に向けた内容に変更

2018/01/26 05:19

投稿

suimi
suimi

スコア8

title CHANGED
File without changes
body CHANGED
@@ -5,7 +5,67 @@
5
5
  修正が遅くなりましてごめんなさい.
6
6
  Zuishinさん,ayuma0913さん.ありがとうございます.
7
7
 
8
- ``````c#
8
+ ```C#
9
+ private bool FilterByName(string name, int min, int max)
10
+ {
11
+ // name が TextBlock1, TextBlock2... のような "TextBlock" + 数値でないときは false を返します。
12
+ // それ以外の場合、数値が min 以上 max 以下であれば true を、そうでなければ false を返します。
13
+ int change;
14
+
15
+ if(name.IndexOf("TextBlock")<9)
16
+ {
17
+ return false;
18
+ }
19
+ else
20
+ {
21
+ Regex num = new Regex(@"[^0-9]");
22
+ string a = num.ToString();
23
+ change = int.Parse(a);
24
+
25
+ TextBlock01.Text = change.ToString();
26
+
27
+ if (change > min && change < max)
28
+ {
29
+ return true;
30
+ }
31
+ else
32
+ {
33
+ return false;
34
+ }
35
+ }
36
+
37
+ }
38
+ ```
39
+
40
+ ayuma0913さんのような感じでMatchでもできました.
41
+ 今後のためにMatchを知らない前提で目的に合わせて調べて試してを繰り返してできたものを残します.
42
+ ありがとうございました.
43
+
44
+ ```XAML
45
+ <Window x:Class="WpfApp1.MainWindow"
46
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
47
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
48
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
49
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
50
+ xmlns:local="clr-namespace:WpfApp1"
51
+ mc:Ignorable="d"
52
+ Title="MainWindow" Height="350" Width="525">
53
+ <Grid Margin="20">
54
+ <Grid.RowDefinitions>
55
+ <RowDefinition Height="Auto"/>
56
+ <RowDefinition Height="Auto"/>
57
+ <RowDefinition Height="Auto"/>
58
+ <RowDefinition/>
59
+ <RowDefinition Height="Auto"/>
60
+ </Grid.RowDefinitions>
61
+ <TextBlock Grid.Row="0" Name="TextBlock01"/>
62
+ <TextBlock Grid.Row="1" Name="TextBlock02"/>
63
+ <TextBlock Grid.Row="2" Name="TextBlock03"/>
64
+ <Button Grid.Row="4" Width="90" Height="21" Click="Button_Click">Click me!</Button>
65
+ </Grid>
66
+ </Window>
67
+ ```
68
+ ```C#
9
69
  using System.Collections.Generic;
10
70
  using System.Linq;
11
71
  using System.Text.RegularExpressions;
@@ -14,10 +74,13 @@
14
74
 
15
75
  namespace WpfApp1
16
76
  {
77
+ // MainWindow 中には複数の TextBlock が含まれます。
78
+ // TextBlock はそれぞれ TextBlock1, TextBlock2... のように名付けられています。
17
79
  public partial class MainWindow : Window
18
80
  {
19
81
  private IList<TextBlock> textBlocks;
20
82
 
83
+ // parent の子孫要素から全ての TextBlock を抽出して返すメソッド
21
84
  private IEnumerable<TextBlock> FindTextBlocks(DependencyObject parent)
22
85
  {
23
86
  foreach (var child in LogicalTreeHelper.GetChildren(parent).OfType<DependencyObject>())
@@ -28,57 +91,33 @@
28
91
  }
29
92
  }
30
93
 
31
- private bool FilterByName(string name)
32
- {
33
- // name の数値部分が 50~80 なら true を、それ以外なら false を返す
34
- name = "8";
35
-
36
- return name;
37
- //エラー発生。stringをboolに暗黙的に変換できません。nameで数値の範囲の制限のかけ方がわかりませんでした。
38
- }
39
-
40
94
  public MainWindow()
41
95
  {
42
96
  InitializeComponent();
43
97
  textBlocks = FindTextBlocks(this)
44
- .Where(a => Match(a.Name))
98
+ .Where(a => FilterByName(a.Name, 1, 2))
45
- //ここの使い方が調べましたが分からなかったです。なんとなくの意味しか理解していません。
46
- .OrderBy(a => a.Name)
47
99
  .ToList();
48
100
  }
49
101
 
50
- private void Button_Click(object sender, RoutedEventArgs e)
102
+ private bool FilterByName(string name, int min, int max)
51
103
  {
52
- foreach (var textBlock in textBlocks)
53
- {
54
- textBlock.Text = "I am " + textBlock.Name;
55
- }
56
- }
57
-
58
- }
59
- }
60
- ```
61
- 現在FilterByNameの中身を考えて変更してみましたが,このままだとNamwの数値が存在する数値でも感知できていないみたいなので前回でおっしゃっていたMatchを使う方針でやってます.
62
- ```C#
63
- private bool FilterByName(string name, int min, int max)
64
- {
65
104
  // name が TextBlock1, TextBlock2... のような "TextBlock" + 数値でないときは false を返します。
66
105
  // それ以外の場合、数値が min 以上 max 以下であれば true を、そうでなければ false を返します。
67
106
  int change;
68
-
107
+ string s1 = name.Substring(0, 9);
108
+
109
+ string s2 = Regex.Replace(name, @"[^\d]", "");
110
+
69
- if(name.IndexOf("TextBlock")<9)
111
+ if (s1!="TextBlock")
70
112
  {
71
113
  return false;
72
114
  }
73
115
  else
74
- {
116
+ {
75
- Regex num = new Regex(@"[^0-9]");
76
- string a = num.ToString();
77
- change = int.Parse(a);
117
+ change = int.Parse(s2);
118
+
78
119
 
79
- TextBlock01.Text = change.ToString();
80
-
81
- if (change > min && change < max)
120
+ if (change >= min && change <= max)
82
121
  {
83
122
  return true;
84
123
  }
@@ -89,6 +128,14 @@
89
128
  }
90
129
 
91
130
  }
131
+ private void Button_Click(object sender, RoutedEventArgs e)
132
+ {
133
+ foreach (var textBlock in textBlocks)
134
+ {
135
+ textBlock.Text = "I am " + textBlock.Name;
136
+ }
137
+ }
138
+ }
139
+ }
92
140
  ```
93
- よろしくお願いします
141
+ 90%以上パクリではありますができました.
94
- ```

2

状況の進展により追加

2018/01/26 05:17

投稿

suimi
suimi

スコア8

title CHANGED
File without changes
body CHANGED
@@ -58,5 +58,37 @@
58
58
  }
59
59
  }
60
60
  ```
61
+ 現在FilterByNameの中身を考えて変更してみましたが,このままだとNamwの数値が存在する数値でも感知できていないみたいなので前回でおっしゃっていたMatchを使う方針でやってます.
62
+ ```C#
63
+ private bool FilterByName(string name, int min, int max)
64
+ {
65
+ // name が TextBlock1, TextBlock2... のような "TextBlock" + 数値でないときは false を返します。
66
+ // それ以外の場合、数値が min 以上 max 以下であれば true を、そうでなければ false を返します。
67
+ int change;
68
+
69
+ if(name.IndexOf("TextBlock")<9)
70
+ {
71
+ return false;
72
+ }
73
+ else
74
+ {
75
+ Regex num = new Regex(@"[^0-9]");
76
+ string a = num.ToString();
77
+ change = int.Parse(a);
78
+
79
+ TextBlock01.Text = change.ToString();
80
+
81
+ if (change > min && change < max)
82
+ {
83
+ return true;
84
+ }
85
+ else
86
+ {
87
+ return false;
88
+ }
89
+ }
90
+
91
+ }
92
+ ```
61
93
  よろしくお願いします。
62
94
  ```

1

質問部分の修正および現状と予定の明記

2018/01/25 22:48

投稿

suimi
suimi

スコア8

title CHANGED
@@ -1,1 +1,1 @@
1
- C# WPF でtextblockの配列処理がうまくできませんでした。
1
+ C# WPF でtextblockの配列処理を行い
body CHANGED
@@ -1,4 +1,9 @@
1
1
  一度回答をいただいたものの、うまく数値の範囲制限のかけ方が分からなかったので再質問しています。
2
+ C#のWPFアプリケーションでTextBlockをC言語の変数および配列のように扱って反復処理に制限を設けたり,番号を他の変数で指定したいと考えています.
3
+ 現在は勉強中なので,解決した場合,質問上に載せているプログラム変更とBAの決定を行いたいと思います.
4
+ プログラムの修正に目が行き過ぎて,質問文の修正を読んですぐ忘れてました.
5
+ 修正が遅くなりましてごめんなさい.
6
+ Zuishinさん,ayuma0913さん.ありがとうございます.
2
7
 
3
8
  ``````c#
4
9
  using System.Collections.Generic;