开发者

Using control.Visible returns False if it's on a tab page that is not selected

开发者 https://www.devze.com 2023-01-09 09:09 出处:网络
I have a form with 2 tab controls. Form opens on tab 1. I have a few controls I need to va开发者_如何学JAVAlidate on tab2.

I have a form with 2 tab controls. Form opens on tab 1. I have a few controls I need to va开发者_如何学JAVAlidate 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


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}))


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...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号