Form1
にListBox studentListBox
があり、Form2
が入力されたstudentList
のメンバーが表示されます。クラスStudent
があり、各学生に関するいくつかの異なるプロパティが含まれています。ListBoxは初めて呼び出されたときにのみ更新されますか? c#
またデータソースをstudentList
に設定するForm1
には、機能BindData()
があります。 studentListBox
表示メンバーをName
に設定します。これはクラスStudent
のプロパティです。
Form1
public void BindData()
{
if (studentListHome != null)
{
studentListBox.DataSource = studentListHome;
foreach(Student s in studentListHome)
{
studentListBox.DisplayMember = "Name";
}
}
}
BindDataはForm1_Load
に呼ばれ、それはまた、更新ボタンをクリックすることで呼び出すことができます。
Form2
は、各学生を作成し、送信ボタンを
Form2
private void submitButton_Click(object sender, EventArgs e)
{
if (comparator(nameInput) &
comparator(idInput) &
comparator(bdayInput)){
variable_assignment();
var tempStudent = StudentCreation();
if(tempStudent != null)
{
studentListForm2.Add(tempStudent);
}
testList.Add(tim);
testList.Add(john);
testList.Add(tempStudent);
foreach(Student s in testList)
{
homeForm.StudentListHome.Add(s);
}
homeForm.BindData();
}
}
comparator()
をクリックしたときにそれがこれを行うForm1
の内側studentList
に追加してくださいユーザー入力フィールドが上がらない」作る機能です空ではありません。 variable_assignment()
は、ユーザー入力を関連変数に割り当てます。 StudentCreation()
私はタイトルで述べたようなので、Student
をinstatiates機能である、ListBox
だけそのと呼ばれる最初の時間を更新し、その後、それはまったく同じまま。 Students
がに追加されていることがわかりました。Console.WriteLine
を送信したため、studentList
に新しいStudent
が追加されていることがわかりましたので、送信ボタンをクリックするたびにListBox
にバインドされています。
これを修正するにはどうすればよいですか?あなたがリストボックスにリストボックスではなく、各項目に対して一度だけ設定する必要があるため
なぜこの 'studentListBox.DisplayMember = "Name"; 'ループするのですか?あなたが一度それをすれば十分です。 – pitersmx
下記の回答を参照してください。ただし、コーディングの方法をそのまま継続したい場合は、これを実行してください: if(studentListHome!= null) { studentListBox.DataSource = null; studentListBox.DataSource = studentListHome; studentListBox.DisplayMember = "Name"; } – Digvijay