Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Checking if a Control has a particular Property


Reply
 
Thread Tools Display Modes
  #1  
Old 06-26-2012, 01:59 AM
bobcory's Avatar
bobcory bobcory is offline
Contributor
 
Join Date: Jun 2003
Location: NW UK
Posts: 405
Default Checking if a Control has a particular Property


I need to know if a control has a particular property. In this instance, the property is actually whether it has a container. The only way I have been able to do it so far is via an error routine like this:

Code:
Public Sub FrameContentsEnabledLFH(MyForm As Form, MyFrame As Frame, blnEnabled As Boolean)
  
  Dim blnError   As Boolean
  Dim cntControl As Control
  
  On Error GoTo FrameContentsEnabledLFH_Error

  'Enable Form and Frame if contents to be enabled (otherwise can be confusing)
  If blnEnabled = True Then
    MyForm.Enabled = True
    MyFrame.Enabled = True
  End If
  
  For Each cntControl In MyForm
    If cntControl.Container Is MyFrame Then
      If blnError = False Then
          If blnEnabled = True Then
              cntControl.Enabled = True
            Else
              cntControl.Enabled = False
          End If
        Else
          blnError = False
      End If
    End If
  Next

  On Error GoTo 0
  Exit Sub

FrameContentsEnabledLFH_Error:

  blnError = True
  Resume Next
  
End Sub
Is there a better way? All help gratefully received
Bob
.
Reply With Quote
  #2  
Old 06-26-2012, 03:21 AM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,882
Default

Not sure what your chasing Bob. Some sort of global enabling with options?

in any case you can use typename to check for specific types of objects.

Code:
Private Function IsContainer(obj As Object) As Boolean Dim sType As String sType = LCase(TypeName(obj)) & "," If InStr("form,frame,picturebox,sstab,", sType) > 0 Then IsContainer = True Else IsContainer = False End If End Function
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #3  
Old 06-27-2012, 02:54 AM
bobcory's Avatar
bobcory bobcory is offline
Contributor
 
Join Date: Jun 2003
Location: NW UK
Posts: 405
Default

Quote:
Originally Posted by Gruff View Post
Not sure what your chasing Bob. Some sort of global enabling with options?
[/vb]
Basically, yes! I have a whole library of routines which do things like "Enable all controls within this frame" OR "Enable all controls within this form"

The problem is that with some controls, the following line causes an error

Code:
    If cntControl.Container Is MyFrame Then
so I used to just have it looking like
Code:
    on error resume next
    If cntControl.Container Is MyFrame Then
...
   on error got 0
This is messy though and I found a subtle problem that in one situation a control outside the frame was being enabled (specifically a Timer which was not supposed to be on but when it fired every few seconds diverted focus from a form in a mysterious way)

So, it would be so much simpler if I could just say something like

Code:
  if ControlHasProperty(cntControl, vbContainer) = True then
    if cntControl.Container is MyFrame then
      ...
Your solution (for which as always, many thanks) if I understand it correctly would only cope with a known list of controls so a new control added in the future could cause a problem, I think
Bob
.
Reply With Quote
  #4  
Old 06-27-2012, 04:23 PM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,882
Default

Yes Bob it is only meant to deal with standard VB controls. Think about it. Any ActiveX control you load could have any type of properties in the world. You cannot second guess what they are going to do.

Another approach to enabling/disabling is to pass a string of control names that you want to affect and the enable state. We search for items bracketed by commas so the a search for "amp" does not return true for "stamp"

Something Like:
Code:
Public Sub EnableByName(byval State as boolean, ControlNames as string) 'ControlNames is a comma separated list of Control Names. Dim UpdateAll as boolean Dim Ctl as Control ControlNames = lcase("," & ControlNames & ",") 'Normalize the names if ControlNames= "" then UpdateAll = True on Error resume next for each Ctl in Me.Controls if UpdateAll then Ctl.Enabled = State Else sName = lcase("," & Ctl.Name & ",") 'Normalize the current control name if Instr$(1,ControlNames,sName) > 0 then Ctl.Enabled = State ' Control is in the string list. end if end if Next Ctl if Err.Number <> 0 then Err.Clear End Sub


So to disable all controls :
EnableByName "", False

Then to enable certain controls:
EnableByName "txtGroup,PicFace,cboUserNames, cmdOK", True

Finally to enable all controls again
EnableByName "", True
----

Yet another method would be to put group numbers into control.tag properties.

Write a sub routine that changes the enabled state by group.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 06-27-2012 at 04:39 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->