views:

27

answers:

3

Hi,

I have a form with 2 tab controls. Form opens on tab 1. I have a few controls I need to validate on tab2.

If the controls I want to validate have been set to not Visible then I want to ignore validation. (i.e. The controls have been hidden from the user) Problem is all the controls on tab 2 return Visible=False, because the Tab is not selected.

Becuase Visible on an individual control returns False if any of the parent's Visible property is False, I can't find out whether a control has been set to Visible=False in code or whether Visible=False becuase the parent tab control is not visible.

Any ideas how I can find out whether a control has been set to Visible=False in code? The private "state" variable in the control class seems to hold this info but I'd prefer not to have to use reflection... this will be slow and feels a bit hacky!

My only other idea is that I'm going to have to add my own property to the control class to hide them (they are controls that I have written)
But this means lots of work on all my forms (I have many forms that have various bits of code to hide individual controls or groups of controls)

Hope that makes sense.

Thanks, Mike G

A: 

You could encode a value into the Control.Tag property if you want to place an arbitrary marker on a control.

However, this doesn't seem like the best way to manage validation...

Adam
Hi,Thanks for the suggestion.This idea however is lots of work, becuase I have this issue on many many forms and in several apps.Also Tag isn't needed becuase I have subclassed all the controls, so I could add my own peoperty anyway.
MikeG
A: 

In the end I've decided to use reflection. Done some perf testing and seems OK for my scenario.

First I check for the presence of tab controls on the form, if there aren't any then can ignore this code and just use the control.IsVisble. Also if the control.Visible reutrns True then is MUST be set to be visible in code, it's only then in the ambiguous case that I then use this...

    Private Shared GetStateMethodInfo As MethodInfo = _
GetType(Control).GetMethod("GetState", BindingFlags.Instance Or BindingFlags.NonPublic)

Then call...

Return CBool(GetStateMethodInfo.Invoke(control, New Object() {2}))
MikeG
Further addition to this is that a groupbox or panel would be made invisible rather than the controls it contains. Hence my now quite complex code also now "walks" up the parent hierarchy looking at all the Visble properies but just treats tab pages differently.
MikeG
A: 

I solved this problem by inheriting each of the basecontrols and then using my own implementation. I could then add my own isvalidatable property to the control.

Sounds way over the top I know, but it did allow we to change out the Windows Forms controls for DevExpress variants by changing only this baseclass.

JohnnyJP
Hi Johhny thanks for the feedback, I considered this becuase we are also iunheriting controls. Main issue was for me that it is the sometimes groups of parent controls we are making not visible i.e. A groupbox or panel would be made invisible rather than the controle it contains, hence my complex refelection based code that "walks" up the parent hierarchy looking at all the Visble properies.
MikeG