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

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

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

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

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

.NET Framework

.NET Framework は、Microsoft Windowsのオペレーティングシステムのために開発されたソフトウェア開発環境/実行環境です。多くのプログラミング言語をサポートしています。

Q&A

解決済

4回答

1898閲覧

ボタンクリックとテキストボックスの切り替えについて

pj7577bm60v

総合スコア13

C#

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

Visual Studio

Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。

.NET Framework

.NET Framework は、Microsoft Windowsのオペレーティングシステムのために開発されたソフトウェア開発環境/実行環境です。多くのプログラミング言語をサポートしています。

0グッド

0クリップ

投稿2020/03/25 06:11

前提・実現したいこと

数字ボタンをクリックしたときにテキストボックス1,2,3、に数字が表示させたい。
イメージ説明

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

数字ボタンをクリックしたときにテキストボックス1,2,3にそれぞれに表示させれるように切り替えの ボタンを用意したのですが、メソッドが上手く作れず切り替えボタンが上手く作動しません。

C#

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { string num = "0"; string num1 = "1"; string num2 = "2"; string num3 = "3"; string num4 = "4"; string num5 = "5"; string num6 = "6"; string num7 = "7"; string num8 = "8"; string num9 = "9"; string num0 = "0"; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { num = num += num1; textBox1.Text = num; } private void button2_Click(object sender, EventArgs e) { num = num += num2; textBox1.Text = num; } private void button3_Click(object sender, EventArgs e) { num = num += num3; textBox1.Text = num; } private void button4_Click(object sender, EventArgs e) { num = num += num4; textBox1.Text = num; } private void button5_Click(object sender, EventArgs e) { num = num += num5; textBox1.Text = num; } private void button6_Click(object sender, EventArgs e) { num = num += num6; textBox1.Text = num; } private void button7_Click(object sender, EventArgs e) { num = num += num7; textBox1.Text = num; } private void button8_Click(object sender, EventArgs e) { num = num += num8; textBox1.Text = num; } private void button9_Click(object sender, EventArgs e) { num = num += num9; textBox1.Text = num; } private void button10_Click(object sender, EventArgs e) { num = num += num0; textBox1.Text = num; } private void button11_Click(object sender, EventArgs e) { } private void button12_Click(object sender, EventArgs e) { } private void button13_Click(object sender, EventArgs e) { } } }

試したこと

ボタンをそれぞれ増やしてテキストボックス1,2,3に割り振りすれば実現できるのですが、切り替えることはできるのか、
プロのみなさんはどういう風にプログラムを作っているのかという質問です。
実現する方法はいろんな方法があるのでしょうか。
初歩的な質問かと思いますがよろしくお願いします。

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

Microsoft Visual Studio Community 2019
Version 16.5.0
.NET Framework version 4.8.03752

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

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

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

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

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

guest

回答4

0

ベストアンサー

(1) 切り替えボタンを、RadioButton にします。

見た目がボタンが良いなら Appearance プロパティを Button にするとトグルボタンになります。

「【C#】Windowsフォームでトグルボタンを実装する」
https://kuroeveryday.blogspot.com/2014/05/ToggleButton.html

すると Checked プロパティで、どれを選択したかがわかるのでプロパティを作っておきます。

C#

1private int SelectedIndex { 2 get { 3 if (radioButton1.Checked) return 1; 4 if (radioButton2.Checked) return 2; 5 if (radioButton3.Checked) return 3; 6 return 0; 7 } 8}

(2) TextBox は List<TextBox> に積み上げておきます。

SelectedIndex が、インデックスとして使用できるよう、先頭に null を入れます。

C#

1 textBoxes = new List<TextBox> { 2 null, 3 textBox1, 4 textBox2, 5 textBox3 6 };

(3) Button のイベントハンドラはひとつにまとめます。

C#

1button1.Click += Button_Click; 2button2.Click += Button_Click; 3button3.Click += Button_Click; 456

でもいいのですが、foreach でコントロールを列挙してひとまとめにします。

C#

1 foreach (var con in Controls) { 2 if (con is Button button) { 3 button.Click += Button_Click; 4 } 5 }

(4) Button_Click では、sender にクリックしたボタンが入ってくるので、それを利用します。

C#

1private void Button_Click(object sender, EventArgs e) { 2 var btn = (Button)sender; 3456}

(5) というわけでこうなりました。

C#

1using System; 2using System.Collections.Generic; 3using System.Windows.Forms; 4 5namespace QA249299 6{ 7 public partial class Form1 : Form 8 { 9 readonly List<TextBox> textBoxes; 10 11 public Form1() { 12 InitializeComponent(); 13 14 foreach (var con in Controls) { 15 if (con is Button button) { 16 button.Click += Button_Click; 17 } 18 } 19 20 textBoxes = new List<TextBox> { 21 null, 22 textBox1, 23 textBox2, 24 textBox3 25 }; 26 } 27 28 private void Button_Click(object sender, EventArgs e) { 29 var textBox = textBoxes[SelectedIndex]; 30 if (textBox != null) { 31 var btn = (Button)sender; 32 textBox.Text += btn.Text; 33 } 34 } 35 36 private int SelectedIndex { 37 get { 38 if (radioButton1.Checked) return 1; 39 if (radioButton2.Checked) return 2; 40 if (radioButton3.Checked) return 3; 41 return 0; 42 } 43 } 44 } 45}

投稿2020/03/25 07:43

KOZ6.0

総合スコア2639

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

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

pj7577bm60v

2020/03/25 13:45

こんなにもコードがすっきりするとはほんとにすごいです。 すごく勉強させていただきました。 ありがとうございました。
guest

0

とりあえずソースを書いてみたけど、こんな感じでしょうか?

private TextBox outputTextBox = textBox1; // ボタン1 private void button1_Click(object sender, EventArgs e) { outputTextBox.Text += "1"; } // テキストボックス1に切り替え private void button11_Click(object sender, EventArgs e) { outputTextBox = textBox1; } // テキストボックス2に切り替え private void button12_Click(object sender, EventArgs e) { outputTextBox = textBox2; }

投稿2020/03/25 06:18

izmktr

総合スコア2856

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

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

0

提示コードでは各数字ボタン押下時の処理が,
textBox1.Text = num;
とされているから,当然,常にtextBox1.Textの内容が更新される.

更新対象を動的に切り替えたいなら,例えば,

  • 「今現在の更新対象はどれなのか?」という情報を保持するようにして
  • 更新対象切替ボタン押下時には,その情報を適切に変更するようにして
  • 各数字ボタン押下時の処理には,「今の現在の更新対象」のTextを更新するようにする

という感じでどうでしょうか.

投稿2020/03/25 06:27

fana

総合スコア11675

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

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

fana

2020/03/25 06:28

(日本語を書いている間に,それそのままのコードが投下された)
pj7577bm60v

2020/03/25 13:47

回答ありがとうございます。 現在の情報の保持も大切な要素ですね。 参考にさせていただきます。 ありがとうございました。
guest

0

num += num1;

ってすればどうでしょう


切り替えるってはなしなら、切り替えボタンでTextboxの番号入れるようにして、
数字ボタンのイベントでその番号に応じたTextbox似代入すればよろしいかと。

投稿2020/03/25 06:15

編集2020/03/25 06:19
y_waiwai

総合スコア87784

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問