Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the best way to mask an UI Event?
    primarykey
    data
    text
    <p>I am working with a DataGridView, and I use the <code>CellValueChanged</code> event.</p> <p>I dont want this event to be triggered when I change a cell value by the code. However, I want it to be triggered when the user edits it.</p> <p>That's why I enclose my cell value change operations with the following code :</p> <pre><code>void changeCellOperation() { dgv.CellValueChanged -= new DataGridViewCellEventHandler(dgv_CellValueChanged); ... cell.Value = myNewCellValue ... dgv.CellValueChanged += new DataGridViewCellEventHandler(dgv_CellValueChanged); } </code></pre> <p>I ended to have several differents functions where my DataGridView cells are updated this way.</p> <p>Because these functions are called from different places and can be nested, I cannot afford to keep this code as is to avoid event unwanted event reactivation.</p> <p>So I ended up this way :</p> <pre><code>int valueChangedEventMask = 0; void changeCellOperation() { valueChangedEventMask++; ... cell.Value = myNewCellValue ... valueChangedEventMask--; } void dgv_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (valueChangedEventMask &gt; 0) return ... } </code></pre> <p>This works fine. Also when the calls are nested, including inside the event itself.</p> <p>But the <code>CellValueChanged</code> event is now <strong>fired too many times for no reasons</strong>.</p> <p>Because I often have to cope with this pattern, I am looking for a solution that can be applicable generally for Events in UIs, not only the DataGridView.</p> <p>So my question is:</p> <p>What is the best tip to mask UI Events correctly and avoid unnecessary Events fires ?</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.
 

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