问题
In my program I allow the user to select a student record from the list box, when they hit the edit button a new form opens. This form displays the id and mark of the student they have selected. I am required to let the user edit that mark and update the list box. I am having trouble letting the user edit, and would appreciate any advise on this. (I am having trouble figuring out what to do when i have the users data inside the edit form) Thanks.
I am not allowed to use LINQ, so a solution or advice on how to do it without it would be greatly appreciated.
Main form when the edit button is clicked:
private void btnEditMark_Click(object sender, EventArgs e)
{
string[] s_rec_arr;
if (lstMarks.SelectedIndex == -1)
{
MessageBox.Show("please select a student");
}
else
{
ModuleData.s_rec = lstMarks.SelectedItem.ToString();
s_rec_arr = ModuleData.s_rec.Split(':');
ModuleData.s_id = s_rec_arr[0];
ModuleData.s_mark = s_rec_arr[1];
editMark myEditRecordForm = new editMark(); // Opens a form called editMark
this.Hide(); // Hides the previous form
myEditRecordForm.ShowDialog(); // Shows the form
}
}
Edit form:
public partial class editMark : Form
{
public editMark()
{
InitializeComponent();
StartPosition = FormStartPosition.CenterScreen;
txtStudentID.Focus();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Hide();
Form1 myForm = new Form1();
myForm.ShowDialog();
}
private void editMark_Load(object sender, EventArgs e)
{
txtStudentID.Text = ModuleData.s_id;
txtMark.Text = ModuleData.s_mark;
}
}
回答1:
I only wrote the parts of codes which i changed.Other parts are same with yours.And as you wish no linq.
Firstly set Modifiers property of your listBox to Public for access it from another form.
Now here we go with codes.
Form1
public Form1()
{
InitializeComponent();
lstMarks.Items.Add("1:Bulutay"); //I don't know your list.This is my guess.
lstMarks.Items.Add("2:Person2"); //
lstMarks.Items.Add("3:Person3"); //
lstMarks.Items.Add("4:Person4"); //
}
private void btnUpdate_Click(object sender, EventArgs e)
{
string[] s_rec_arr;
if (lstMarks.SelectedIndex == -1)
{
MessageBox.Show("please select a student");
}
else
{
ModuleData.s_rec = lstMarks.SelectedItem.ToString();
s_rec_arr = lstMarks.SelectedItem.ToString().Split(':');
ModuleData.s_id = s_rec_arr[0];
ModuleData.s_mark = s_rec_arr[1];
this.Hide(); //We hide our Main Form, it's still running at background and waiting to be shown again.We will use it.
editMark myEditRecordForm = new editMark(); //Edit Form
myEditRecordForm.Owner = this; //We set New Edit Form's owner as this mainForm to access its lstMarks(listBox).
myEditRecordForm.ShowDialog();
}
}
Form editMark
private void btnSubmit_Click(object sender, EventArgs e)
{
string data = txtStudentID.Text + ":" + txtMark.Text;
string[] parts = data.Split(':');
Form1 mainForm = (Form1)this.Owner; //We get our hidden owner's REFERENCE to mainForm object.
for (int i = 0; i < mainForm.lstMarks.Items.Count; i++) //loops mainForm.lstMarks.Items.Count
{
string[] item = mainForm.lstMarks.Items[i].ToString().Split(':'); //We test all of items one by one.
if (item[0] == ModuleData.s_id) //if listbox's current item's ID part equals to our static ModuleData.s_id
mainForm.lstMarks.Items[i] = data; //Set new data.
}
mainForm.Show(); //We show our old Main Form which we hided before.
this.Close();
}
Screenshots from project.
I selected Person3 and clicked to UPDATE button
Changed data
Clicked to SUBMIT button
Updated (New) data
来源:https://stackoverflow.com/questions/55533790/editing-record-from-a-list-box-on-a-separate-form