c# - I want to validate a radiobutton before it changes state, but it really fires when the focus leaves the control -


i have simple winform example demonstrates happens.

there groupbox 2 radiobuttons. 2 buttons share validating event handler. on form button nothing. no events connected. there checkbox controls state of passing validation. checked passes , unchecked fails.

when program starts , user clicks on either radiobutton validating event not fire. when click on control other current button validating event fires. nop button gives me click besides checkbox.

in test checkbox represents status of passing validation.

this not work because once uncheck checkbox , click radio button focus forever stuck. can't focus checkbox change state.

the reason validating event called when focus leaving , not when radiobutton clicked. sees "valdating" fails , cancels event.

this wrong approach. should doing test @ time of initial click? click event happens after state has changed.

what event should use can test validation before changing radiobutton state? can leave button , fix issue before trying again.

this example simplified test shows dead end. real world example 2 radiobuttons select 1 of 2 similar tables in datagridview. 2 tables related , want them on same tabpage. when user selects alternate table want validation/confirmation before switching away. if confirmation fails want cancel radio button.

// validate called when radio button clicked , when leaves box using system.componentmodel; using system.windows.forms;  namespace validateradiobutton {   public partial class form1 : form   {     int count;     public form1()     {       initializecomponent();     }      private void radiobutton_validating(object sender, canceleventargs e)     {       if (sender radiobutton) {         // if box not checked, cancel radiobutton change         // becomes hotel california test, once unchecked          // can never leave control         e.cancel = !chkallowchange.checked;       }       count++;       //display number of times validating called in title bar       //demonstrates when event called       text = count.tostring();      }    } } 

i have reproduced form in test project , think see going on.

setting checked property of canceleventargs true prevent whatever control set losing focus until input "corrected". aware, validating event triggered whenever control loses focus. user becomes "stuck" because way can "correct" input modify check box, cannot because of validating event fires on radio button.

a solution came moving validated event groupbox, instead of radio buttons:

private void groupbox_validating(object sender, canceleventargs e) {     if (sender radiobutton)     {         e.cancel = !checkbox1.checked;     }     count++;      //display number of times validating called in title bar     //demonstrates when event called     text = count.tostring(); } 

be sure remove validating event handler radio buttons.


for clarity, have set form in manner:

screenshot of layout of form controls, within group box.

the result cannot click 'ok' until have ticked check box. have preserved text = count.tostring() assignment can see form calls validating should.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -