[RESOLVED] Creating Form Controls At Run-Time
So I'm having trouble resolving this issue...I went ot the MSDN KB 190670 which explains how to do this but it's not working the way I want...it's creating the controls (proven by running the test more than once) but they aren't displaying...can you take a look and see what's wrong?
General Declarations:
Code:
Private WithEvents cmdObject As VB.CommandButton
Private WithEvents lblObject As VB.Label
Private WithEvents txtObject As VB.TextBox
Main Bulk of Automation to make controls:
Code:
Private Sub Finish()
Dim X As Integer
Dim lLeft As Integer
Dim tLeft As Integer
Dim bLeft As Integer
Dim lbHeight As Integer
Dim tHeight As Integer
Dim lbTop As Integer
Dim tTop As Integer
Dim lWidth As Integer
Dim tWidth As Integer
Dim bWidth As Integer
lLeft = 240
tLeft = 1560
bLeft = 3120
lbHeight = 255
tHeight = 285
lbTop = 360
tTop = 330
lWidth = 855
tWidth = 1095
bWidth = 975
For X = 1 To mcnt
Set cmdObject = Controls.Add("VB.CommandButton", "cmd" & X, SystemReq)
cmdObject.Height = lbHeight
cmdObject.Left = bLeft
cmdObject.Top = lbTop
cmdObject.Width = bWidth
cmdObject.Visible = True
cmdObject.Caption = "Info"
SystemReq.Refresh
Set txtObject = Controls.Add("VB.TextBox", "text" & X, SystemReq)
txtObject.Height = tHeight
txtObject.Left = tLeft
txtObject.Top = tTop
txtObject.Width = tWidth
txtObject.Visible = True
txtObject.Alignment = 2
txtObject.Text = resArray(X)
SystemReq.Refresh
Set lblObject = Controls.Add("VB.Label", "label" & X, SystemReq)
lblObject.Height = lbHeight
lblObject.Left = lLeft
lblObject.Top = lbTop
lblObject.Width = lWidth
lblObject.Visible = True
lblObject.Caption = marray(X)
SystemReq.Refresh
lbTop = lbTop + 360
tTop = tTop + 360
SystemReq.Refresh
Next
DispWindow.Text = "The Test has completed, please view the results below."
Re: Creating Form Controls At Run-Time
What is SystemReq - Form, Frame?
Code works fine for me but I removed all references to the SystemReq variable.
You have hardcoded values which appear to be in Twips, make sure the ScaleMode property of the container control (SystemReq) is also using Twips.
Re: Creating Form Controls At Run-Time
SystemReq is the name of the Form, for some reason the controls don't appear...or I can't see them.
What are Twips? I'm unfamiliar with that word...and how does it relate to the ScaleMode property.
Sorry still learning ;-) But catching on quick. Thanks for the help!
Re: Creating Form Controls At Run-Time
ScaleMode is the measurement unit you wish to use for Forms, Pictureboxes and other container controls. Twips are a type of measurement unit. Other units are Pixel, Inch, Character. The default in VB is Twips. Unless you changed any one of the Scale properties (ScaleMode, ScaleWidth, ScaleHeight, ScaleLeft, ScaleTop) at design time it shouldn't be a problem.
The posted code works fine for me, if I change SystemReg to Me.
What is the value of mcnt? Are you sure it is >= 1.
Can you post more of the code?
It appears that you expect the code is generating a control arrays. You cannot create control arrays at runtime and unfortunately the WithEvent variables will only capture the events of the last control created.
Re: Creating Form Controls At Run-Time
Thanks for the tips...I went out browsing on the interweb and found a much smaller easier thing to use. I didn't quite understand how control arrays worked at first but then I found some code that works much much better. I'll share it below so you can get a basic idea (for those who don't know)
(Step 1 is to create a control array, the easiest way is to create a control on the form, select it, copy it and paste it and hit YES to create the array)
Code:
Private Sub Finish()
Dim X As Integer
For X = 1 To mcnt
Load MachLbl(X)
With MachLbl(X)
.Caption = marray(X)
.Enabled = True
.Visible = True
.Top = MachLbl(X - 1).Top + 360
End With
Load MachTxt(X)
With MachTxt(X)
.Alignment = 2
.Enabled = True
.Visible = True
.Text = resArray(X)
.Top = MachTxt(X - 1).Top + 360
If resArray(X) = "PASS" Then
.ForeColor = &H8000&
Else
.ForeColor = &HFF&
End If
End With
Load MachCmd(X)
With MachCmd(X)
.Enabled = True
.Visible = True
.Top = MachCmd(X - 1).Top + 360
End With
Next X
DispWindow.Text = "The Test has completed, please view the results below."
End Sub
Re: [RESOLVED] Creating Form Controls At Run-Time
Hi phimuskapsi,
I have a question for you, if you can help me.
In the code you showed in your first post, how did you code the event handlers for each of the Command Buttons.
When I do it here, only the last command button triggers the cmdObject_click() event.
Cheers,
Nap
Re: [RESOLVED] Creating Form Controls At Run-Time
Try something like this:
Code:
Private Sub MachCmd_Click(Index As Integer)
'Each time one Command Button is clicked, do something!'
Select Case Index
Case 1:
DispWindow.Text = ""
code = 2
Call ErrorTranslation(code)
Case 2:
DispWindow.Text = ""
code = 3
Call ErrorTranslation(code)
Case 3:
DispWindow.Text = ""
code = 4
Call ErrorTranslation(code)
Case 4:
DispWindow.Text = ""
code = 5
Call ErrorTranslation(code)
Case 5:
DispWindow.Text = ""
code = 6
Call ErrorTranslation(code)
Case 6:
DispWindow.Text = ""
code = 7
Call ErrorTranslation(code)
Case 7:
DispWindow.Text = ""
code = 8
Call ErrorTranslation(code)
Case 8:
DispWindow.Text = ""
code = 9
Call ErrorTranslation(code)
Case 9:
DispWindow.Text = ""
code = 10
Call ErrorTranslation(code)
Case 0:
DispWindow.Text = ""
code = 1
Call ErrorTranslation(code)
End Select
End Sub
Re: [RESOLVED] Creating Form Controls At Run-Time
Hi phimuskapsi,
Thanks for your answer but the eventhandler you just posted is to be used by your code in post #5.
My question was about the code you showed in post #1. I used the same MS example as you did except that for some reason my controls were visible. However the problem for me is that of the controls created in the FOR/NEXT loop, only the last one of each type generates events.
That is; if mcnt = 9; Only cmd9 generates events that my eventhandler ( cmdObject_click() ) is able to process. For cmd1 - cmd8, I cannot process the events.
Were you able to somehow process events from the remainder of the controls?
Cheers,
Nap
Re: [RESOLVED] Creating Form Controls At Run-Time
@Napoleon only:
I'll try to convey this message one more time for your own good:
there is no easy solution for what you're trying to accomplish - it simply does not exist.
I really hope you listen this time.
Cheers (and sorry for highjacking this thread). :wave:
Re: [RESOLVED] Creating Form Controls At Run-Time
Hey RhinoBull,
Thanks for your help, and I have taken it on board. I've already coded the stuff I needed to using 2 dozen control arrays. What a mess but it works.
In a general sense, difficult things have never dissuaded me from attempting them. Since phimuskapsi started off with the same initial method as I did, I'm wondering if he was somehow able to overcome this problem.
The thing that gets me is that the command buttons do show on the form and can be clicked. So I'm wondering what happens to the events that are raised. My windows doesn't crash, so they are not causing any problems to the OS system. So there must be some way of intercepting them, even if it means working through the API.
Cheers,
Nap
Re: [RESOLVED] Creating Form Controls At Run-Time
The way I overcame it was with my second example...it really works better.
Note:
Now that I look at the MS code I realized something...
The code is really designed for buttons that do EXACTLY the same thing. If you look at it it's basically only setup to allow that. The second way allows for multiple events based on choice.
Re: [RESOLVED] Creating Form Controls At Run-Time
@Napoleon
phimuskapi made the correct point, a control-array is made in order to share the SAME event-routines (just separated by the Index). However if you create separate controls (like you did), they have seperate event-rountines whicjh can only be called based on their own name (like cmd1_Click ,cmd2_Click ....).
Why don't you join the .Net learners (like me), No real control-arrays in there but more flexibility (what ever that's supposed to say) :wave:
Re: [RESOLVED] Creating Form Controls At Run-Time
Hi Opus,
Yea, if I would have been able to do Dim WithEvents menuoption() as VB.OptionButton it would have been a lot simpler. But I've gone with 2 dozen control arrays, which enables me to do what I want. I agree it's messy but the task was done.
In so far as migrating to .NET, I certainly will in the future. For the work I'm doing, with my limited knowledge of .NET, I can't see any compelling and clear reason to make the change at the moment.
The application I'm working on is in VB6 and is over 15K lines and since there is no way to migrate the code to .NET, I have to keep working on what I have.
The other thing is that I have a number of libraries I use, so starting .NET would mean starting from scratch. Not something I'm looking forward to at the moment.
Cheers,
Nap