Hi experts, I would like to add a staff from the "SearchLB" to "ListBox1". So for example, if I already have Staff A under "ListBox1" and I accidentally add similiar Staff A into "ListBox1", the system should prompt me with an error message, "Staff already existed.". However, my If Else statement does not seem to work.
Kindly please help me on it.
Thank you!
Below is the codes for my Confirm Button to add staff into "ListBox1".
C#
//confirm selection to be included in stafflistprotectedvoidConfirmBtn_Click(object sender, EventArgs e)
{
int sel = 0;
for (int i = 0; i if (SearchLB.Items[i].Selected == true)
{
sel++;
//if (!ListBox1.Items.Contains(SearchLB.Items[i])) //&& ListBox1.DataValueField != ConfigurationSettings.AppSettings["STAFFID"]
{
SQL dbAdapter = new SQL();
dbAdapter.insertStafflist(SearchLB.SelectedValue, (string)Session["userName"]);
}
else
{
MessageBox.Show("Staff already existed", "Error");
}
//if (ListBox1.DataValueField == ConfigurationSettings.AppSettings["STAFFID"])//else//{// MessageBox.Show("Staff already existed", "Error!");//}
}
}
if (sel == 0)
{
MessageBox.Show("There is no selected staff to be added.", "Error");
}
//generate listbox items
populateStafflist();
}
5 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Ashley Jackson44 reputationPosted Feb 16, 2011, 11:07 PM EST
0
Hi, The issue is caused by you checkig if a list item from List A is in List B. The list item will not be in both list, but the value of that item may be. Try something along the linex of:
if (ListBox1.SelectedIndex > -1) // Something Selected { if (ListBox2.Items.FindByValue(ListBox1.SelectedValue) != null) { // Output Error Message } else { // Process selected entry }
Ashley Jackson44 reputationPosted Feb 17, 2011, 1:16 AM EST
0
Just realised that you are doing a windows froms app. The listbox for that uses a list of object. Try the following slightly tweaked version of the code: if(listBox1.SelectedIndex > -1) { if (listBox2.FindString(listBox1.SelectedItem.ToString()) > -1) { MessageBox.Show("Duplicate Found"); } else { MessageBox.Show("OK Add It!"); } } Let mek now how that is. Ash...
Ashley Jackson44 reputationPosted Feb 17, 2011, 1:43 AM EST
0
Just an extra thought. If your requirements are a little more complex -
ie you have staff with a name and staff id and you want to check
against the staff Id not the name then we have to do a little extra
work:
Then populate the list (for this example I am just manually adding items)
listBox1.Items.Add(new person {Name="ash", StaffId = 01});
listBox1.Items.Add(new person { Name = "John", StaffId = 02 });
listBox1.Items.Add(new person { Name = "Jane", StaffId = 03 });
listBox1.Items.Add(new person { Name = "Mary", StaffId = 05 });
listBox2.Items.Add(new person { Name = "Jane", StaffId = 03 });
listBox2.Items.Add(new person { Name = "Mary", StaffId = 04 });
NOTE: In listbox 2 Mary has a different Staff Id so is ot a dupicate.
Then to check for duplicates do this:
if(listBox1.SelectedIndex > -1)
{
bool found=false;
foreach (person per in listBox2.Items)
{
if (per.StaffId == ((person)listBox1.SelectedItem).StaffId)
{
found = true;
}
}
Ashley Jackson44 reputationPosted Feb 16, 2011, 11:07 PM EST
The issue is caused by you checkig if a list item from List A is in List B. The list item will not be in both list, but the value of that item may be.
Try something along the linex of:
if (ListBox1.SelectedIndex > -1) // Something Selected
{
if (ListBox2.Items.FindByValue(ListBox1.SelectedValue) != null)
{
// Output Error Message
}
else
{
// Process selected entry
}
}
Let me know how you get on withthis.
Ash....
lauren brown33 reputationPosted Feb 17, 2011, 12:16 AM EST
Syed ShakeerRank #777 · 3K reputationPosted Feb 17, 2011, 12:37 AM EST
Check the this link.http://stackoverflow.com/questions/1137083/c-how-do-i-select-a-list-box-item-when-i-have-the-value-name-in-a-string
Ashley Jackson44 reputationPosted Feb 17, 2011, 1:16 AM EST
if(listBox1.SelectedIndex > -1)
{
if (listBox2.FindString(listBox1.SelectedItem.ToString()) > -1)
{
MessageBox.Show("Duplicate Found");
}
else
{
MessageBox.Show("OK Add It!");
}
}
Let mek now how that is.
Ash...
Ashley Jackson44 reputationPosted Feb 17, 2011, 1:43 AM EST
First set the display member of the List box:
listBox1.Items.Add(new person {Name="ash", StaffId = 01});
listBox1.Items.Add(new person { Name = "John", StaffId = 02 });
listBox1.Items.Add(new person { Name = "Jane", StaffId = 03 });
listBox1.Items.Add(new person { Name = "Mary", StaffId = 05 });
listBox2.Items.Add(new person { Name = "Jane", StaffId = 03 });
listBox2.Items.Add(new person { Name = "Mary", StaffId = 04 });
NOTE: In listbox 2 Mary has a different Staff Id so is ot a dupicate.
Then to check for duplicates do this:
if(listBox1.SelectedIndex > -1)
{
bool found=false;
foreach (person per in listBox2.Items)
{
if (per.StaffId == ((person)listBox1.SelectedItem).StaffId)
{
found = true;
}
}
if (found)
{
MessageBox.Show("Duplicate Found");
}
else
{
MessageBox.Show("Add Ok");
}
}
Let me know if this resolves everything. I have a dummy vs2010 project I can upload or email to you if you want it.
Ash...