Move controls programmatically

G

Guest

Good Evening

Novice

I have some code that hides controls if they are empty and then moves the
last two controls up (then down) the form so there aren't blank spaces.

It works fine, but I'm sure there's a neater way.

Note: the controls move 1cm at a time (567 twips to the cm).

Private Sub Form_Current()
'hide the contols if they have no text
txtRegionA.Visible = Not IsNull(RegionA)
txtRegionB.Visible = Not IsNull(RegionB)
txtRegionC.Visible = Not IsNull(RegionC)
txtRegionD.Visible = Not IsNull(RegionD)
txtRegionE.Visible = Not IsNull(RegionE)
txtUrbanA.Visible = Not IsNull(UrbanA)
txtUrbanB.Visible = Not IsNull(UrbanB)

'move the Urban labels and controls up if the Region controls have no text
If txtAlternateRegionA.Visible = No Then
lblUrbanA.Top = 3.2 * 567
txtUrbanA.Top = 3.6 * 567
lblUrbanB.Top = 4.2 * 567
txtUrbanB.Top = 4.6 * 567
Else
If txtAlternateRegionB.Visible = No Then
lblUrbanA.Top = 4.2 * 567
txtUrbanA.Top = 4.6 * 567
lblUrbanB.Top = 5.2 * 567
txtUrbanB.Top = 5.6 * 567
Else
If txtAlternateRegionC.Visible = No Then
lblUrbanA.Top = 5.2 * 567
txtUrbanA.Top = 5.6 * 567
lblUrbanB.Top = 6.2 * 567
txtUrbanB.Top = 6.6 * 567
Else
If txtAlternateRegionD.Visible = No Then
lblUrbanA.Top = 6.2 * 567
txtUrbanA.Top = 6.6 * 567
lblUrbanB.Top = 7.2 * 567
txtUrbanB.Top = 7.6 * 567
Else
If txtAlternateRegionE.Visible = No Then
lblUrbanA.Top = 7.2 * 567
txtUrbanA.Top = 7.6 * 567
lblUrbanB.Top = 8.2 * 567
txtUrbanB.Top = 8.6 * 567
Else
lblUrbanA.Top = 8.2 * 567
txtUrbanA.Top = 8.6 * 567
lblUrbanB.Top = 9.2 * 567
txtUrbanB.Top = 9.6 * 567
End If
End If
End If
End If
End If
End Sub

Thank you

Peter
 
G

Guest

Sorry! There was a small error in the Text box names--here's the code I'm
using.

Private Sub Form_Current()
'hide the contols if they have no text
txtRegionA.Visible = Not IsNull(RegionA)
txtRegionB.Visible = Not IsNull(RegionB)
txtRegionC.Visible = Not IsNull(RegionC)
txtRegionD.Visible = Not IsNull(RegionD)
txtRegionE.Visible = Not IsNull(RegionE)
txtUrbanA.Visible = Not IsNull(UrbanA)
txtUrbanB.Visible = Not IsNull(UrbanB)

'move the Urban labels and controls up if the Region controls have no text
If txtRegionA.Visible = No Then
lblUrbanA.Top = 3.2 * 567
txtUrbanA.Top = 3.6 * 567
lblUrbanB.Top = 4.2 * 567
txtUrbanB.Top = 4.6 * 567
Else
If txtRegionB.Visible = No Then
lblUrbanA.Top = 4.2 * 567
txtUrbanA.Top = 4.6 * 567
lblUrbanB.Top = 5.2 * 567
txtUrbanB.Top = 5.6 * 567
Else
If txtRegionC.Visible = No Then
lblUrbanA.Top = 5.2 * 567
txtUrbanA.Top = 5.6 * 567
lblUrbanB.Top = 6.2 * 567
txtUrbanB.Top = 6.6 * 567
Else
If txtRegionD.Visible = No Then
lblUrbanA.Top = 6.2 * 567
txtUrbanA.Top = 6.6 * 567
lblUrbanB.Top = 7.2 * 567
txtUrbanB.Top = 7.6 * 567
Else
If txtRegionE.Visible = No Then
lblUrbanA.Top = 7.2 * 567
txtUrbanA.Top = 7.6 * 567
lblUrbanB.Top = 8.2 * 567
txtUrbanB.Top = 8.6 * 567
Else
lblUrbanA.Top = 8.2 * 567
txtUrbanA.Top = 8.6 * 567
lblUrbanB.Top = 9.2 * 567
txtUrbanB.Top = 9.6 * 567
End If
End If
End If
End If
End If
End Sub

Peter
 
D

Dirk Goldgar

Peter Stone said:
Sorry! There was a small error in the Text box names--here's the code
I'm using.

Private Sub Form_Current()
'hide the contols if they have no text
txtRegionA.Visible = Not IsNull(RegionA)
txtRegionB.Visible = Not IsNull(RegionB)
txtRegionC.Visible = Not IsNull(RegionC)
txtRegionD.Visible = Not IsNull(RegionD)
txtRegionE.Visible = Not IsNull(RegionE)
txtUrbanA.Visible = Not IsNull(UrbanA)
txtUrbanB.Visible = Not IsNull(UrbanB)

'move the Urban labels and controls up if the Region controls have no
text If txtRegionA.Visible = No Then
lblUrbanA.Top = 3.2 * 567
txtUrbanA.Top = 3.6 * 567
lblUrbanB.Top = 4.2 * 567
txtUrbanB.Top = 4.6 * 567
Else
If txtRegionB.Visible = No Then
lblUrbanA.Top = 4.2 * 567
txtUrbanA.Top = 4.6 * 567
lblUrbanB.Top = 5.2 * 567
txtUrbanB.Top = 5.6 * 567
Else
If txtRegionC.Visible = No Then
lblUrbanA.Top = 5.2 * 567
txtUrbanA.Top = 5.6 * 567
lblUrbanB.Top = 6.2 * 567
txtUrbanB.Top = 6.6 * 567
Else
If txtRegionD.Visible = No Then
lblUrbanA.Top = 6.2 * 567
txtUrbanA.Top = 6.6 * 567
lblUrbanB.Top = 7.2 * 567
txtUrbanB.Top = 7.6 * 567
Else
If txtRegionE.Visible = No Then
lblUrbanA.Top = 7.2 * 567
txtUrbanA.Top = 7.6 * 567
lblUrbanB.Top = 8.2 * 567
txtUrbanB.Top = 8.6 * 567
Else
lblUrbanA.Top = 8.2 * 567
txtUrbanA.Top = 8.6 * 567
lblUrbanB.Top = 9.2 * 567
txtUrbanB.Top = 9.6 * 567
End If
End If
End If
End If
End If
End Sub

Since the adjustments all seem to be increments of 1 cm per region over
a base value, you could tidy your code up nicely like this:

Const conLblABase = 2.2
Const conTxtABase = 2.6
Const conLblBBase = 3.2
Const conTxtBBase = 3.6

Dim intAdjustment As Integer

' ... code to determine visibility ...

If txtRegionA.Visible = False Then
intAdjustment = 1
ElseIf txtRegionB.Visible = False Then
intAdjustment = 2
ElseIf txtRegionC.Visible = False Then
intAdjustment = 3
ElseIf txtRegionD.Visible = False Then
intAdjustment = 4
End If

If intAdjustment > 0 Then
lblUrbanA.Top = (conLblABase + 1) * 567
txtUrbanA.Top = (conTxtABase + 1) * 567
lblUrbanB.Top = (conLblBBase + 1) * 567
txtUrbanB.Top = (conTxtBBase + 1) * 567
End If
 
S

Stefan Hoffmann

hi Peter,

Peter said:
I have some code that hides controls if they are empty and then moves the
last two controls up (then down) the form so there aren't blank spaces.
Normally it's not a good idea to move controls, because users memorize
the position of controls. It leads to unnecessary obfuscation.
It works fine, but I'm sure there's a neater way.
As far as i understand your code, why don't you use an endless form?


mfG
--> stefan <--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top