テキストボックス1にID テキストボックス2に名前を入力してボタンを押下すると構造体にしてDictionaryで保存できるように作成致しました。
テキストボックス1に特定のIDを入れ検索ボタンを押下すると、それと同じIDと名前を出力するものも作成致しました。
今回、躓いているものが、テキストボックス1に特定のIDを入れ、前ボタンと次ボタンを用意し、それぞれ押下すると前のID名前、次のID名前がテキストボックスに種強くするように作成中で、
前ボタンは正常動作をしておりますが、次をクリックすると一番最後のIDの値が出力するようになり困っております。
ご教授お願いいたします。
例 1.2.3.4.5 を事前に入力したとしてテキストボックス1に3を入れて前を押下すると2の値が出力
テキストボックス1に3を入れて次を押下すると、一番最後の5が出てきます。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//構造体を宣言
struct abc { public int ID; public string Name; } Dictionary<int, string> test = new Dictionary<int, string>(); private void zikkou_Click(object sender, EventArgs e) { //宣言 abc A = new abc(); //AにIDと名前を格納 A.ID = int.Parse(textBox1.Text); A.Name = textBox2.Text; test.Add(A.ID, A.Name); //Aに入力した内容をlistに追加 //list.Add(A); } private void kuria_Click(object sender, EventArgs e) { //入力内容をクリア textBox1.ResetText(); textBox2.ResetText(); } private void kensaku_Click(object sender, EventArgs e) { // 宣言 int x = int.Parse(textBox1.Text); //検索 foreach (KeyValuePair<int, string> item in test) { if (item.Key == int.Parse(textBox1.Text)) { MessageBox.Show("ID:" + item.Key.ToString() + "\n" + "NAME:" + item.Value); } } } private void mae_Click(object sender, EventArgs e) { // 宣言 int x = int.Parse(textBox1.Text); //検索 foreach (KeyValuePair<int, string> item in test) { if (item.Key + 1 == int.Parse(textBox1.Text)) { textBox1.Text = (item.Key.ToString()); textBox2.Text = (item.Value); } } } private void tugi_Click(object sender, EventArgs e) { // 宣言 int x = int.Parse(textBox1.Text); //検索 foreach (KeyValuePair<int, string> item in test) { if (item.Key - 1 == int.Parse(textBox1.Text)) { textBox1.Text = (item.Key.ToString()); textBox2.Text = (item.Value); } } } }
}
<code>ボタンを押してからコードを書いてください。インデントが崩れてせっかくはっていただいたコードが読みにくいので
回答1件
あなたの回答
tips
プレビュー