[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

assigning property to multiple controls

I setup a list box on my form, and if my query returns values, I want it to be visible, otherwise not.
This is working fine.
So I thought, I'll add my buttons into the logic, but instead of duplicating code and setting them true/false within my if/then logic, I figured I would wait until the end of the routine and set their Visible property to the value of the listbox's visible property.
Lo and behold, they did not appear.  Stepping through the code, even at the point the list box should be made visible, it's still showing that that value is False (yet the list box show - and the buttons do not).
Seemed like a logical approach to me...what am I missing?
0
sirbounty
Asked:
sirbounty
6 Solutions
 
Fernando SotoCommented:
It is hard to say without seeing the code that implements the functionality. Can you post the pertinent  code please.
0
 
sirbountyAuthor Commented:
Eh, I've left the office, but from memory...

lstResults.Items.Clear
If results.count > 0 then
  For each sr as searchresult in results
    lstResults.Items.Add sr.properties("displayName")(0).tostring()
  Next
  lstResults.Visible=true
Else
  lstResults.Visible=false
End If

btnDelete.Visible=lstResults.Visible
btnEdit.Visible=lstResults.Visible

Open in new window

0
 
Jacques Bourgeois (James Burger)Commented:
Post the real code when you will be back at the office. Code from memory is almost always different than the real code, and usually miss the simple error that makes the original stick.
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
sirbountyAuthor Commented:
Essentially the same as I posted from memory... ;^)

lstResults.Items.Clear()
Using adEntry As New DirectoryEntry(strADPath)
  Using ds As New DirectorySearcher(adEntry, adFilter)
    Dim src As SearchResultCollection = ds.FindAll
    If src.Count > 0 Then
      For Each sr As SearchResult In src
        'irrelevant code
      Next
      lstResults.Visible = True 'placing a break here and examining the value results in False, tho it IS shown
    Else
      lstResults.Visible = False
    End If
    btnEdit.Visible = lstResults.Visible
    btnDelete.Visible = lstResults.Visible
  End Using
End Using

Open in new window

0
 
sirbountyAuthor Commented:
Even tried placing a watch on it - never hits, but the listbox is displayed...really weird.
0
 
Jacques Bourgeois (James Burger)Commented:
Strange one indeed. But code that seems irrelevant sometimes hides the problem.

Have you tried setting the buttons with btnEdit.Visible=(src.Count > 0)
0
 
sirbountyAuthor Commented:
I've tried doubling the code anyway and setting the buttons to explicitly True - no joy. :(
0
 
sirbountyAuthor Commented:
I ended up creating a boolean variable, and just set the controls to that value at the end.
Even though stepping through it, the value still shows False, the controls are visible.
Would still like to figure this out if anyone has any ideas...
0
 
Jacques Bourgeois (James Burger)Commented:
Is it possible that you have something in the VisibleChanged event of the controls.
0
 
sirbountyAuthor Commented:
No, I don't. :(
0
 
Jacques Bourgeois (James Burger)Commented:
You might try setting the Enabled property instead of the Visible property.

If it works with Enabled, or if Visible is changed when you deal only with Enabled in the code that you present, then something somewhere is dealing with Visible.

If Enabled ends up showing the same behaviour as Visible, then the problem is not with the property itself. It could be another empty control, with no content or no border, that happens to hide the ListBox. Strange things like that happens you know, and often, when you have such a curious problem with code that seems so simple, it comes from one of these strange things.

Other than that, I will have to set my own Visible property to False. These sort of thing are almost impossible to diagnose at a distance, because we never have the complete picture. The problem is hidden somewhere in that whole picture.
0
 
sirbountyAuthor Commented:
The controls are on a tab page...not sure that would be a culprit or not, but thought I'd mention it
0
 
Jacques Bourgeois (James Burger)Commented:
Could be the problem.

You could react to events in the TabPage that do stuff over what you expect.

Also, in design view it's not because a Control shows over a TabPage that it is part of that tab. It can simply be a standalone control that shows over the tab without being included in it. This can play tricks sometimes.

You can easily check this by using the Document Outline (Ctrl-Alt-T) by default. It shows you the hierarchy of containers and contained controls in the form.
0
 
ArkCommented:
Just about looping through controls - you can use
Private ControlsAffected As Control() = {lstResults, btnDelete, btnEdit, moreControls}
'.....
Dim bVisible = src.Count > 0
For each c as control in ControlsAffected
   c.Visible = bVisible
Next

Open in new window

0
 
CodeCruiserCommented:
Controls on tab control do weird things when it comes to visibility and rendering as tab pages do not render themselves (and the controls on them) until needed.

One option is to move this code to tabcontrol.SelectedIndexChanged event and populate the controls there (but only if selected tab is the one containing this listbox and it is not populated already, use a boolean to track that).
0
 
sirbountyAuthor Commented:
I'll post more details about it in a bit (when I get to work), but regardless, it's very strange that even though those values indicate they are false, the code works now, at least with the boolean controlling it and not the state of the listbox...
0
 
sirbountyAuthor Commented:
Haven't found anything that explains why it doesn't work as it should, but at least I found a workaround.
Thanks for the help!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now