Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am having trouble with the edit and delete button on my program. What I am trying to do is when you click a name

I am having trouble with the edit and delete button on my program. What I am trying to do is when you click a name on the list box you should be able to edit the name or delete it, one of the errors that I am getting is " System.ArgumentException: 'An item with the same key has already been added." among other ones. when I click the save button.

I have provided the code in the lines below

Thank you.

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;

namespace Lab_9_3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private Dictionary dStudent = new Dictionary();

// private bool check = false; private void Validate1(object sender, KeyPressEventArgs e) { if (e.KeyChar < 65 || e.KeyChar > 90) { if (e.KeyChar < 97 || e.KeyChar > 122) {

if (e.KeyChar != 8) { e.Handled = true; } } } }

private void Validate2(object sender, KeyPressEventArgs e) { { if (e.KeyChar < 48 || e.KeyChar > 57) { if (e.KeyChar != 8) { e.Handled = true; } } } } private void Form1_Load(object sender, EventArgs e) { dStudent.Add(100, "Don"); dStudent.Add(101, "Mary"); dStudent.Add(102, "Sam"); dStudent.Add(103, "Tom"); dStudent.Add(104, "Samatha");

lstNames.DisplayMember = "Value"; lstNames.ValueMember = "key"; lstNames.DataSource = new BindingSource(dStudent, null);

btnSave.Enabled = false; btnCancel.Enabled = false; txtEmpID.ReadOnly = true; txtEmpName.ReadOnly = true; }

private void lstNames_SelectedIndexChanged(object sender, EventArgs e) { txtEmpName.Text = lstNames.Text; txtEmpID.Text = lstNames.SelectedValue.ToString(); }

private void btnAdd_Click(object sender, EventArgs e) { lstNames.Enabled = false; btnSave.Enabled = true; btnAdd.Enabled = false; btnEdit.Enabled = false; btnDelete.Enabled = false; txtEmpID.Text = ""; txtEmpName.Text = ""; txtEmpName.ReadOnly = false; txtEmpID.ReadOnly = false; btnCancel.Enabled = true;

}

private void btnSave_Click(object sender, EventArgs e) { if (txtEmpID.Text !="" && txtEmpName.Text !="") { btnSave.Enabled = true; btnAdd.Enabled = true; btnCancel.Enabled = true; btnEdit.Enabled = true; btnDelete.Enabled = true; lstNames.Enabled = true; dStudent.Add(int.Parse(txtEmpID.Text), txtEmpName.Text); lstNames.DataSource = new BindingSource(dStudent, null);

txtEmpID.ReadOnly = true; txtEmpName.ReadOnly = true;

} }

private void txtEmpName_KeyPress(object sender, KeyPressEventArgs e) { Validate1(sender, e); }

private void txtEmpID_KeyPress(object sender, KeyPressEventArgs e) { Validate2(sender, e); }

private void btnEdit_Click(object sender, EventArgs e) { // check = true;

lstNames.Enabled = false; btnAdd.Enabled = false; btnEdit.Enabled = false;

txtEmpID.ReadOnly = true; txtEmpName.ReadOnly = false;

btnDelete.Enabled = false; btnCancel.Enabled = true; btnSave.Enabled = true; }

private void btnCancel_Click(object sender, EventArgs e) { txtEmpName.Text = ""; txtEmpID.Text = "";

btnAdd.Enabled = true; btnDelete.Enabled = true; btnEdit.Enabled = true;

btnSave.Enabled = true;

lstNames.Enabled = true; txtEmpID.ReadOnly = true; txtEmpName.ReadOnly = true;

}

private void btnDelete_Click(object sender, EventArgs e) { string message = "Are you sure you want to delete the employee? "; string title = ""; MessageBoxButtons buttons = MessageBoxButtons.YesNo; DialogResult result = MessageBox.Show(message, title,buttons);

if (result == DialogResult.Yes) { //for (int i = 0; i < dStudent.Count; i++) //var element = dStudent.ElementAt(); dStudent.Remove(lstNames.SelectedIndex);

btnAdd.Enabled = true; btnDelete.Enabled = true; btnEdit.Enabled = true;

} } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Iv Special Issue On Database Systems For Biomedical Applications Lncs 6990

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Christian Bohm ,Johann Eder ,Claudia Plant

2011th Edition

3642237398, 978-3642237393

More Books

Students also viewed these Databases questions