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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

where is the problem??

Status
Not open for further replies.

FirasSHhady

Programmer
Dec 5, 2004
14
SA
In the Form code window:
------------------------
Private Sub cmd_Click()
For Each Control In Me.Controls
If Control.Name = "Lbl" Then Call Alignlbl(Control, 30)
Next
End Sub

Private Sub Form_Load()
With Form1
.Width = 7000
.Top = 100
.Height = 8000
End With
End Sub
======================================================

In the Module Window:
---------------------

Sub Alignlbl(ByVal lbl As Label, N As Integer)
Dim I As Integer
For I = 1 To N

'Creat the control
Load lbl(I)

'Position of new controls
With lbl
.Left = lbl(I - 1)
.Top = lbl(I - 1).Top + lbl(I - 1).Height + 30
.Caption = I

'Make a different Backcolor
If I Mod 2 = 0 Then
.BackColor = &HC0FFFF
Else
.BackColor = &HC0FFC0
End If

'Width of new controls
Do While .Width < 0.5 * (Me.ScaleWidth)
.Width = .Width + 1
DoEvents
Loop

'Make them all visible
.Visible = True
End With
Next
End Sub


 
What's wrong with it - what error, what does it do?
My guess - no control array.

======================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
 
Have you dimmed the control object elsewhere ? if not

Code:
Private Sub cmd_Click()

DIM Control as control

For Each Control In Me.Controls
    If Control.Name = "Lbl" Then Call Alignlbl(Control, 30)
Next
End Sub
 
1. yes, it's a control array.
but look, I try to pass the array to General sub in the Module to use it frequently.

2. Yes, I dim the Control.
 
There are a few issues here.

1:
Private Sub cmd_Click()
For Each Control In Me.Controls
If Control.Name = "Lbl" Then Call Alignlbl(Control, 30)
Next

You should use a variable to loop through a collection:
Code:
Private Sub Command1_Click()
Dim ctrLoop As Control
For Each ctrLoop In Me.Controls
    If ctrLoop.Name = "Lbl" Then Call Alignlbl(30)
Next
End Sub

2: Don't use the name of an array variable as a standard variable:
Sub Alignlbl(ByVal lbl As Label, N As Integer)

As the array name is known you only need to pass the Int value:
Code:
Sub Alignlbl(N As Integer)
Dim I As Integer

3: You're setting an Int value (Left) to a control name:
With lbl
.Left = lbl(I - 1)

You probably need:
Code:
With Lbl(I)
    .Left = Lbl(I - 1).Left

Make sure that you set the Index property of the label called Lbl to 0 inthe property window. That should then work.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
Thank Mr.johnwm, it's really great tips to me, but when I chang the code as above the result still the same.

I wounder if I could attach the project of my program, but I don't know how in this forum.

But I think the 2nd tip need alittel more explain.

thank's alot.
Feras
 
Here is the complete code - paste it into a new form module with a Command button and a label on the form. Set the label name to Lbl and set it's index to 0.
Code:
Private Sub Command1_Click()
Dim ctrLoop As Control
For Each ctrLoop In Me.Controls
    If ctrLoop.Name = "Lbl" Then Call Alignlbl(30)
Next

End Sub

Private Sub Form_Load()
With Me
    .Width = 7000
    .Top = 100
    .Height = 8000
End With
End Sub

Sub Alignlbl(N As Integer)
Dim I As Integer
For I = 1 To N

'Creat the control
Load Lbl(I)

'Position of new controls
With Lbl(I)
    .Left = Lbl(I - 1).Left
    .Top = Lbl(I - 1).Top + Lbl(I - 1).Height + 30
    .Caption = I

    'Make a different Backcolor
    If I Mod 2 = 0 Then
        .BackColor = &HC0FFFF
    Else
        .BackColor = &HC0FFC0
    End If

    'Width of new controls
    Do While .Width < 0.5 * (Me.ScaleWidth)
            .Width = .Width + 1
            DoEvents
    Loop

    'Make them all visible
    .Visible = True
End With
Next
End Sub

That is cut&pasted from my test form, so I know it runs. If you have a problem, come back with the exact error and show the area of the code that fails.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
this is copy&past from my projrct' windows, now the vb tells me that there is an Error and the error is: Sub Or Function Not Definde, and set the curser at (Lbl), at the line marked below.


____________________________
Private Sub Command1_Click()
Dim ctrLoop As Control
For Each ctrLoop In Me.Controls
If ctrLoop.Name = "Lbl" Then Call Alignlbl(30)
Next
End Sub
Private Sub Form_Load()
With Me
.Width = 7000
.Top = 100
.Height = 8000
End With
End Sub

=============================
Sub Alignlbl(N As Integer)
Dim I As Integer
For I = 1 To N

'Creat the control
THE LINE MENTIONED ABOVE Load Lbl(I)

'Position of new controls
With Lbl(I)
.Left = Lbl(I - 1).Left
.Top = Lbl(I - 1).Top + Lbl(I - 1).Height + 30
.Caption = I

'Make a different Backcolor
If I Mod 2 = 0 Then
.BackColor = &HC0FFFF
Else
.BackColor = &HC0FFC0
End If

'Width of new controls
Do While .Width < 0.5 * (Me.ScaleWidth)
.Width = .Width + 1
DoEvents
Loop

'Make them all visible
.Visible = True
End With
Next
End Sub
 
Are you sure that your label control is called Lbl and it's Index is set to 0? To be sure of the name copy&paste from the property window for the label into the code, just in case lower case L and numeral 1 have got confused.

Your code runs fine for me

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
yeh, now I just open the properties window , and copy the name of the label I have on my form.
But the problem still as mentioned above.

sir,
I try the code in the form code window, without taknig it to a Module code and it's working (as it was).
But I don't want to do that I want to pass a control array to a general sub to use it again and agian.

Feras
 
If you want to take the Sub into a code module, you will need to pass the Form and the Control as an argument. The Sub now looks like:

Code:
Public Sub Alignlbl(F As Form, Lbl As Label, N As Integer)
Dim I As Integer
For I = 1 To N
'Creat the control
Load F.Lbl(I)
'Position of new controls
With F.Lbl(I)
    .Left = F.Lbl(I - 1).Left
    .Top = F.Lbl(I - 1).Top + F.Lbl(I - 1).Height + 30
    .Caption = I
    'Make a different Backcolor
    If I Mod 2 = 0 Then
        .BackColor = &HC0FFFF
    Else
        .BackColor = &HC0FFC0
    End If
    'Width of new controls
    Do While .Width < 0.5 * (F.ScaleWidth)
            .Width = .Width + 1
            DoEvents
    Loop
    'Make them all visible
    .Visible = True
End With
Next
End Sub
You will call it like this:
Code:
For Each ctrLoop In Me.Controls
    If ctrLoop.Name = "Lbl" Then Call Alignlbl(Me, ctrLoop, 30)
Next

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
The code work very good, thany you Mr.johnwm for you help,
and I have cople of questions please:
1. Waht about passing the controls by type, I think I sould use some thing like this:

Dim Ctrl as Control
For Each Ctrl in Me.Controls
If Ctrl.Type=Lable Then Call Alignlbl(me,Ctrl,30)
Next

2. How could I know all properties of Control. (I mean when we define Ctrl as control, so when we write "Ctrl." there isn't any menu apper as usual, so I think of all properties of the Control.


If my question need more explain please let me know.
Feras.
 
1. I believe that Typeof only works with external refs.
2. Controls all have different properties

These are both VBHelp type questions, rather than forum questions! I guess you didn't read faq222-2244 yet.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top