Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

change rectangle border colors selectively 1

Status
Not open for further replies.

Lhuffst

Programmer
Jun 23, 2003
503
0
0
US
I have a form that has 8 rectangles with fields inside the rectangles.
When the user selects a category (combobox), I change the rectangle border color to blue.
I want all the other rectangle borders to be white.

When the user selects a new category, then I want the newly selected rectangle to be blue and the original one to go back to white.
The rectangle that has select a category is to remain the same at all times.
What happens is that when I select a category (bxselections), the appropriate rectangle does change to blue but when I select another category, the original rectangle and the new selected rectangle both remain blue.
Here is the code that I'm using ([[highlight #E9B96E]highlight #CC0000]the line I'm checking is CtlBorderChange)[/highlight][/highlight]
Code:
[b]combobox after update code[/b]
Select Case CmbCategory.Column(1)
  Case 1    'Data Entry/CSIS
             Call LockSections("DataEntryLock", True)
             Call ctrlBorderChange("BxDataEntry", True)
    
  Case 2    'Scrapped Meters
         Call LockSections("ScrappedLock", True)
         Call ctrlBorderChange("BxScrappedMeters", True)
...


[b]private Sub ctrlBorderChange(ctrlName As String, bchange As Boolean)[/b]
  ' ctrlName - string - name of control
  ' bChange - boolean - True if the control properties should  be changed, False - no change
  
   Call ctlBorderWhite(ctrlName, True)
   
  If Len(globalvar) > 0 Then
    Me(globalvar).BorderColor = 52479
  End If
  If bchange Then
    Me(ctrlName).BorderColor = vbBlue
    globalvar = ctrlName
    g_BoxName = ctrlName
  
  Else
    globalvar = vbNullString
  End If
 ' Call ctlBorderWhite(ctrlName, True)
End Sub

[b]Private Sub ctlBorderWhite(ctrlName As String, aChange As Boolean)[/b]'15658705        'light blue
'16777215        'white
 ' Debug.Print g_BoxName
    
For Each ctl In Me.Controls
        Select Case ctl.ControlType
            'Case acCheckBox, acTextBox, acComboBox, acCheckBox, acRectangle
             Case acRectangle
                'If ctl.Name <> g_BoxName Or ctl.Name = "bxselections" Then
                 '
                'End If
             
                Debug.Print ctl.Name
                If aChange Then
                If ctl.Name <> ctrlName Or ctl.Name <> "bxselections" Then
                   ctl.BackColor = 16777215 'white
                Else
                    ctl.BackColor = 15658705 'cyan
                End If
               End If
        End Select
    Next ctl
End Sub
Can anyone see what I'm doing wrong??
 
Did you mean to set the border color or backcolor?
ctl.BackColor = 16777215 'white
 
Here's a really simplified example that works with 8 rectangles named Box0-Box7, the names of which are in an unbound combobox. Should help you to see where you are going wrong.

Code:
[blue]Option Compare Database
Option Explicit

Public LastHighlight As Object

Private Sub CmbCategory_Click()
    Highlight GetControlByName(CmbCategory.value)
End Sub

Public Sub Highlight(ctl As Object)
    If Not LastHighlight Is Nothing Then
        LastHighlight.BorderColor = vbWhite
    End If
    ctl.BorderColor = vbBlue
    Set LastHighlight = ctl
End Sub

Private Function GetControlByName(strCtl As String) As Control
    Dim ctl As Object
    For Each ctl In Me.Controls
        If strCtl = ctl.Name Then
            Set GetControlByName = ctl
            Exit For
        End If
    Next
End Function[/blue]
 
Majp. That was it, I had used backcolor instead of bordercolor. Missed that when I changed my mind to only highlight the border instead of the who rectangle. Thanks
StrongM Thank you as well. I'll work with your code as well. Always trying to learn better ways !
 
One trick that can simplify things like this is to just reset all controls first. Then modify certain ones. This works well when you may be modifying multiple controls and doing multiple modifications. Not really needed here, but may be a helpful approach with more complicated forms. For example I have a calendar form that formats the holidays and certain other dates. When loading a different month I just reset everything instead of trying to figure out the individual controls to reset.
simple example.
Code:
Private Sub CmbCategory_Click()
    hightLight_UnHighlight (CmbCategory.value)
End Sub

Public Sub Highlight_UnHighlight(ctlName as string)
    dim i as integer
    'turn them all white 
    for I = 0 to 7
     me.controls("Box" & i).bordercolor = vbwhite
    next i
   'turn the one/s you want blue
    me.controls(ctlName).BorderColor = vbBlue
End Sub
 
I keep forgetting VBA can directly return a control by its name from the Controls collection - so my example could be shortened to:

Code:
[blue]Option Explicit

Public LastHighlight As Object

Private Sub CmbCategory_Click()
    Highlight Me.Controls(CmbCategory.value)
End Sub

Public Sub Highlight(ctl As Object)
    If Not LastHighlight Is Nothing Then
        LastHighlight.BorderColor = vbWhite
    End If
    ctl.BorderColor = vbBlue
    Set LastHighlight = ctl
End Sub[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top