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

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

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

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

WPF

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

Q&A

1回答

1118閲覧

C# のfor文の中のtextboxが表示できない.

kaien

総合スコア6

C#

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

WPF

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

0グッド

0クリップ

投稿2018/02/01 14:39

前提・実現したいこと

C#のWPFで計算した数値を表示したいだけなんですが,表示したい数字は3ケタもあるので配列などを使いたいです.
ボタンと別途textboxを配置してボタンクリックで一部のtextboxから数値を取りだして,計算し,他のtextboxへ配列で一気に表示したいです

発生している問題・エラーメッセージ

teratail上に似たものを発見してそれを試している最中で,クリックで対応するための書き方がわからなかったです.
GridContext.Text = text[ii];の部分が動作するにはどうすれば良いんでしょうか.

エラーメッセージ

該当のソースコード

XAML

1<Window x:Class="Musen.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:Musen" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="700" Width="800"> 9 <Grid Margin="20" x:Name="maingrid"> 10 <Grid.ColumnDefinitions> 11 <ColumnDefinition Width="*"/> 12 <ColumnDefinition Width="*"/> 13 <ColumnDefinition Width="*"/> 14 <ColumnDefinition Width="*"/> 15 <ColumnDefinition Width="*"/> 16 <ColumnDefinition Width="*"/> 17 <ColumnDefinition Width="*"/> 18 <ColumnDefinition Width="*"/> 19 <ColumnDefinition Width="*"/> 20 <ColumnDefinition Width="*"/> 21 <ColumnDefinition Width="*"/> 22 <ColumnDefinition Width="*"/> 23 </Grid.ColumnDefinitions> 24 <Grid.RowDefinitions> 25 <RowDefinition Height="*"/> 26 <RowDefinition Height="2*"/> 27 <RowDefinition Height="*"/> 28 <RowDefinition Height="*"/> 29 <RowDefinition Height="*"/> 30 <RowDefinition Height="*"/> 31 <RowDefinition Height="*"/> 32 <RowDefinition Height="*"/> 33 <RowDefinition Height="*"/> 34 <RowDefinition Height="*"/> 35 <RowDefinition Height="*"/> 36 <RowDefinition Height="*"/> 37 <RowDefinition Height="*"/> 38 <RowDefinition Height="*"/> 39 </Grid.RowDefinitions> 40 <TextBox Text="{Binding Texts[0], Mode=OneWay}" Grid.Column="0" Grid.Row="2" Name="TextBox1" Height="30" Width="50" /> 41 <TextBox Text="{Binding Texts[1], Mode=OneWay}" Grid.Column="0" Grid.Row="3" Name="TextBox2" Height="30" Width="50"/> 42 <TextBox Text="{Binding Texts[2], Mode=OneWay}" Grid.Column="0" Grid.Row="4" Name="TextBox3" Height="30" Width="50"/> 43 <TextBox Text="{Binding Texts[3], Mode=OneWay}" Grid.Column="0" Grid.Row="5" Name="TextBox4" Height="30" Width="50"/> 44454647計150近く格子状に配置.(上から2行は入力用のtextboxのために空けてます.) 4849 50 <TextBox Height="40" Width="50" Name="TextBox0" Text="0" 51 HorizontalAlignment="Left" Grid.Column="1" Grid.Row="0" /> 52 <TextBox Name="Hirogari" Width="50" HorizontalAlignment="Left" 53 Text="0" Grid.Column="0" Grid.Row="0" /> 54 <Button Content="Click" Height="23" Width="70" Name="Button1" 55 HorizontalAlignment="Left" Grid.Row="0" Grid.Column="4" Click="Button_Click_1" /> 56 </Grid> 57</Window> 58

C#

1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Windows; 5using System.Windows.Controls; 6 7namespace Musen 8{ 9 /// <summary> 10 /// MainWindow.xaml の相互作用ロジック 11 /// </summary> 12 13 public class GridContext 14 { 15 public string[] Texts { get; private set; } 16 17 public GridContext() 18 { 19 20 } 21 } 22 23 24 public partial class MainWindow : Window 25 { 26 static int hComm; 27 static string wDATA; 28 static Int32 dLen, wLen; 29 static Boolean bRet; 30 static double deltaL; 31 static double deltaT; 32 static int deltaS; 33 static double dx; 34 static double dy; 35 static double dr; 36 static double dl; 37 static double dl0; 38 static int ii; 39 static int[] deltaSS = new int[144]; 40 static int[] deltaSSS = new int[144]; 41 static int deltaSS_min; 42 static int Pulsefreq0; 43 static int gain0; 44 static double lx; 45 static double ly; 46 47 static string[] text = new string[145]; 48 49 public MainWindow() 50 { 51 InitializeComponent(); 52 this.maingrid.DataContext = new GridContext(); 53 } 54 55 private void Button_Click_1(object sender, RoutedEventArgs e) 56 { 57 58 // 開始 59 60 for (int i4 = 1; i4 <= 2; i4++) 61 { 62 for (int i3 = 1; i3 <= 2; i3++) 63 { 64 for (int i1 = 1; i1 <= 6; i1++) 65 { 66 for (int i2 = 1; i2 <= 6; i2++) 67 { 68 //計算した結果をdeltaSに入れる部分 69 70 text[ii] = deltaS.ToString(); 71               //こんな以下のTextに入れられずに表示できないので解決策がほしいです. 72 GridContext.Text = text[ii]; 73 74 75 } 76 } 77 } 78 } 79 //deltaSS_min =0; 80 //終了 81 82 } 83 } 84} 85

試したこと

teratail上にあったものの二つ目のbindを回答欄に見つけたのでそれをコピペしましたが,クリックで毎回動作する今回のパターンに対応させる方法が分からなかった.
C#のコード中のGridContext.Text = text[ii];でそれぞれのTextに表示させたい.

補足情報(FW/ツールのバージョンなど)

Free版のVisalStudio2015を使用中

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

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

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

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

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

Zuishin

2018/02/01 15:20

要求仕様を満たさないであろう上に間違ったソースを出されてもわかりません。結局どうしたいのか日本語で説明するのが難しければ図に描いて説明してください。
guest

回答1

0

ループ中に「GridContext.Text = text[ii];」では代入を繰り返してしまうので、GridContext.Textの値をガンガン書き換えるだけで、ループ終了時に最後の結果が残っているだけになります。

配列textの中身を全て表示したいなら、ループ終了後に配列をつなげた文字列を作ってGridContext.Textに渡してやるのがいいと思います。

C#

1private void Button_Click_1(object sender, RoutedEventArgs e) 2{ 3 // 開始 4 for (int i4 = 1; i4 <= 2; i4++) 5 { 6 for (int i3 = 1; i3 <= 2; i3++) 7 { 8 for (int i1 = 1; i1 <= 6; i1++) 9 { 10 for (int i2 = 1; i2 <= 6; i2++) 11 { 12 //計算した結果をdeltaSに入れる部分 13 14 text[ii] = deltaS.ToString(); 15 } 16 } 17 } 18 } 19 20 // 改行区切りで連結してテキストボックスに渡す 21 GridContext.Text = string.Join("\r\n", text); 22 //終了 23}

投稿2018/02/02 02:04

YamakawaJunichi

総合スコア630

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問