从单独表单上的列表框编辑记录 [英] editing record from a list box on a separate form

查看:55
本文介绍了从单独表单上的列表框编辑记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我不允许使用LINQ,因此,不使用LINQ的解决方案或建议将不胜感激.

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

编辑表单:

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

推荐答案

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

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

项目的屏幕截图.

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

更改的数据

点击了提交"按钮

更新(新)数据

这篇关于从单独表单上的列表框编辑记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆