Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you change the selected items of one listbox, you could temporarily disable the events on the other. For example:</p> <pre><code>// Store the event handlers in private member variables. private System.EventHandler selectedEmployeeChanged = new System.EventHandler(this.lbEmployees_SelectedIndexChanged); private void lbProjects_SelectedIndexChanged(object sender, EventArgs e) { try { // Remove your event so that updating lbEmployees doesn't cause // lbEmployees_SelectedIndexChanged to get fired. lbEmployees.SelectedIndexChanged -= selectedEmployeeChanged; // other event handler logic } finally { // Ensure that the handler on lbEmployees is re-added, // even if an exception was encountered. lbEmployees.SelectedIndexChanged += selectedEmployeeChanged; } } </code></pre> <p><strong>Note:</strong> I have renamed your listBoxes (and the associated events) to be more readable. As per your description, <code>listBox1</code> is now <code>lbEmployees</code> and <code>listBox2</code> is now <code>lbProjects</code>.</p> <p>As for programatically selecting multiple items, if you know the index for each one, you could use the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.setselected" rel="nofollow">ListBox.SetSelected Method</a>. For example:</p> <pre><code>listBox1.SetSelected(1, true); listBox1.SetSelected(3, true); </code></pre> <p>causes listBox1 to select the items at 1 and 3.</p> <p>In your case, I would recomend only using the queries to get what values to select (not clearing your listboxes and then just adding back the values that should be selected). Below is my suggestion for how you would rewrite one of your handlers:</p> <pre><code>// Store the event handlers in private member variables. private System.EventHandler selectedEmployeeChanged = new System.EventHandler(this.lbEmployees_SelectedIndexChanged); private void lbProjects_SelectedIndexChanged(object sender, EventArgs e) { // Declare this outside of try so that we can close it in finally DbDataReader reader = null; try { // Remove your event so that updating lbEmployees doesn't cause // lbEmployees_SelectedIndexChanged to get fired. lbEmployees.SelectedIndexChanged -= selectedEmployeeChanged; // Deselect all items in lbEmployees lbEmployees.ClearSelected(); cmd.CommandText = "SELECT ProjectName FROM Projects WHERE ProjectID IN(SELECT ProjectID FROM EmployeeProject WHERE EmpId=(SELECT EmpId FROM Employee WHERE EmpName= '@EmpName')) ORDER BY ProjectID"; cmd.Parameters.AddWithValue("@EmpName", lbProjects.SelectedItem.ToString()); reader = cmd.ExecuteReader(); // For each row returned, find the index of the matching value // in lbEmployees and select it. while (rdr.Read()) { int index = lbEmployees.FindStringExact(rdr.GetString(0)); if(index != ListBox.NoMatches) { lbEmployees.SetSelected(index, true); } } this.AutoScroll = true; } finally { // Ensure that the reader gets closed, even if an exception ocurred if(reader != null) { reader.Close(); } // Ensure that the handler on lbEmployees is re-added, // even if an exception was encountered. lbEmployees.SelectedIndexChanged += selectedEmployeeChanged; } } </code></pre> <p>As a side note, you may want to look into using a JOIN operator in your query string, rather than multiple nested SELECTS.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload