从单独表单上的列表框编辑记录

时间:2019-04-05 10:50:57

标签: c# forms editing

在我的程序中,我允许用户从列表框中选择学生记录,当他们单击编辑按钮时,将打开一个新表格。该表格显示他们选择的学生的ID和标记。我需要让用户编辑该标记并更新列表框。我无法让用户进行编辑,对此不胜感激。 (当我将用户数据保存在编辑表单中时,我很难弄清楚该怎么办)谢谢。

不允许使用LINQ,因此不提供解决方案或建议,将不胜感激。

单击编辑按钮时的主要形式:

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
    }
}

编辑表单:

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 个答案:

答案 0 :(得分:0)

我只写了我更改过的代码部分。其他部分与您相同。并且您不希望使用linq。

enter image description here

首先将listBox的Modifiers属性设置为Public,以从其他表单访问它。

现在,我们使用代码。

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();
    }
}

表单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();
}

项目的屏幕截图。

enter image description here

我选择了Person3,然后单击了UPDATE按钮

enter image description here

更改的数据

enter image description here

点击了“提交”按钮

enter image description here

更新(新)数据