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

Want to Create Dynamic Form and label

Status
Not open for further replies.

Cap2010

Programmer
Mar 29, 2000
196
CA
1) Want to create form dynamically and also label
or text during runtime. want to know how can i go about
and below is one of the e.g. for creating the text through
runtime has an error want to solve the same also


2) Learning COM how do I go about and want to know good site plus a book to refer


'HI don't want to add textbox in the form
' actually want to create through coding
' tried all kind of combination none works
' All have the hint is the use of
' property get or set is there
' then on form load event create the label
' and add value to the label
' below is the same


Private Sub Form_Click()

Dim anytext As TextBox

'error occurs on below command
'here error occurs as it requires
'already created text box
'is required in the form (which is
'what don't want to create)

'Set anytext = New text1


'Error is object variable not set

anytext.Text = "Hello"
End Sub


Thanks

Lad
 
You must always set the variable to a new type and not just any word you use. Remember that forms have control containers and any control created at runtime must be inserted into the form's control container and then loaded so you may see it in your form.

Private sub Form_Click()

Dim anytext as TextBox

Set anytext = New TextBox - Missing/Incorrect for you

'From here you have to insert anytext into your form's
'control container and then load anytext. I'm sorry
'I can't show you now, because my office workstation
'does not have visual basic installed. But VB's
'intellisense will let you know of the control container
'(control array) when you use "."

anytext.text = "Hello World"
End Sub
 
Hi JeffMaller,

Have seen your code, it shows you have added manually the label and textbox in the form.
Purpose what I am looking for is creating programmatically.

Secondly, TerenceOu has mentioned about control containers
though it is right, need the code which would help me
to complete the work.

Thanks for your help.

Regard,

Lad

 
Lad,
With the method that I posted, you do need to have a control array to act as the "seed". You can programmatically add/remove more controls at runtime using that control array and then adjust the properties as needed.
Otherwise, you will have to use APIs to create controls based on MFC at runtime and then monitor the process' message queue to respond to events. This is the more complicated method and I do not know if the MFC apis will work in VB.
Also, to my knowledge, the "Set {obj}= new {control} method listed by TerenceOu will not work. - Jeff Marler
(please note, that the page is under construction)
 
For your information jmarler, if you noticed, what I did was

Set {control} = New {control}

I did not do this:

Set {obj} = New {control}

These are 2 totally different statements. So, lets not confuse our friend here
 
TerenceOu,
Yes, you are correct in pointing out that I did not type EXACTLY what you typed . . . this is because I was in a hurry and for quick examples I will frequently use obj as a generic pointer. This DOES NOT mean that obj was declared as an object . . .
With that being said, I think that if you look at the statements in question, they are both doing the same thing! A variable (either a variable of type object or a specific control class such as TextBox) is being set to a new instance of the control. What is wrong with your example is that VB controls (such as TextBox, Label, and ListBoxes) are Public but not Createable. This means that no matter how you declare your variable (either an object pointer or a control pointer) you are not going to be able to instantiate the control class using the New command. See the following code for an example . . .



'** This line declares the variable. Works Fine!

Dim ControlTextBox as TextBox <- This line is fine.


'** This line will fail at compile . . . it won't
'** even begin to run. You will get an &quot;Invalid Use
'** of New Keyword error&quot;. This is because VB controls
'** are public not creatable.

Set ControlTextBox = New TextBox <- This line will fail.



As I said before, you can not simply create new controls with the New command as you would regular classes . . . it just doesn't work. If you have been able to make this work (with standard VB controls such as TextBox, Label, etc) I would love to see your code . . . why don't you post it? Until then, as you put it, lets not confuse our friend here!
- Jeff Marler
(please note, that the page is under construction)
 
ALad,
Yes, TerenceOu IS correct when he mentions control containers. Specifically, it is accessed with frmSomeForm.Controls. He is also correct when he stated that all forms have control containers and that all controls on the form have a reference maintained within that form's control container. What you are not going to be able to do is to programmatically create new controls (using standard VB code and standard VB controls) at run time. This is because you CAN NOT USE THE New keyword with a VB control in order to create a new, unique instance of it. You need to do that before you can add the newly created control to any control container. It is because of this very fact that the small demo I did used control arrays.
There is a technique using ControlExtenders (which I need to look up again . . . I can't remember them off of the top of my head), but they are limited in their application. If you'd like (and assuming I find the info) I can post it in here or write a FAQ on it. The only other option that I can think of is to use the CreateWindowEX API (remember that deep down, all controls are really just individual windows) and pass it &quot;EDIT&quot;, &quot;BUTTON&quot;, etc. for the class of window to create. You would then need to monitor the message queue for the newly created window for any messages (WM_MOUSEDOWN, WM_MOUSEUP, etc.) I have a smaple program that does that, but I can't quite get the button to look like a standard VB button. Needs tweaking I guess . . .
As for TerenceOu technique . . . if he has been able to make the New keyword work or has found some way to dynamically add controls to the container's control collection at run time (in VB6), I would love to see it. I Was working on a project about a year ago where I could've really used it. - Jeff Marler
(please note, that the page is under construction)
 
I'm so sorry I couldn't post this code earlier so you could have used it jmarler.

'Copy this code and paste it into your form. Run it to
'see the result
Private Sub Form_Load()
'This variable need not be created
Dim t As Control

'A textbox control is added to the form's control
'container/array, and &quot;t&quot; points to the newly created
'textbox.
Set t = Form1.Controls.Add(&quot;VB.TextBox&quot;, &quot;Text1&quot;, Form1)

'As the textbox is created at run-time, you won't get
'to see it. So, you have to set the textbox to be
'visible and also the position on the form which
'you want it to be created
t.Visible = True
t.Top = 34
t.Left = 34
't.Text = &quot;Hello&quot;

'This example code demonstrates the use of the form
'controls array/container. But do remember that only
'you know the name of the specific control you want
'or you can use an index instead of the control's name
Form1.Controls(&quot;Text1&quot;).Text = &quot;Hello&quot;

'You can comment the previous line and try pasting
'this line of code in a command button's click event
'to see the result. But if a command button was
'created it is the first in the array which means
'that the newly created control's index is = 1
'Form1.Controls(1).Text = &quot;Hello&quot;

End Sub
 
I know that that my recent posting will be quite different from what I have previously said. But if anyone noticed earlier, I did say that my office workstation does not have Visual Basic installed, which means I only have the weekend to actually test code before I post it and not because I haven't got the solution.

Sorry for the mistakes, but well....here's the solution
 
TerenceOu ,
Hmmm . . . interesting solution. I know that the controls themselves are public not createable (that is why just declaring it as a New control would not work), but I have never tried to use the actual controls collection as a conrol generation engine. That is a very interesting approach and one that I will be able to use myself in the future.
If I can add one more piece to you code however . . . With you demo, you will not be able to respond to events fired from your control.
There is a very simple solution to this. When you declare your control object, declare at the module level and use the WithEvents keyword. For example . . .


Private WithEvents[/U[ txtTextBox As TextBox


Now the problem with this is that you would still need to declare every single varible in code at design time that you intend to add programatically. Of course, you don't have to declare the variables . . . you could just do this . . .


Call Form1.Controls.Add(&quot;VB.TextBox&quot;, &quot;Text1&quot;, Form1)

Form1.Controls(1).Top = 500
Form1.Controls(1).Left = 500
Form1.Controls(1).Visible = True


but then you have no way to monitor the events fired by the control. This is fine if you only want to add input fields that the program will read from, but it will not work if you expect the standard control events to fire (just try adding a Button this way). That is the reason why my example (albeit over simplified) used a control array. The advantages are . . .

1) I can add more controls programatically at runtime.

2) I don't need to declare avariable withevents at design time for every control that I might add programtically.

3) I can still respond to control events.

The only cost is that I have to add the single control at design time to produce the control array.

As I said, I like your approach here, but I do not think that it is entirely complete. Using your method, is there a way to add controls whose events can be monitored WITHOUT having to declare a variable WITHEVENTS for every control that you may need to add?
- Jeff Marler
(please note, that the page is under construction)
 
OK . . . the underline never got turned off . . . sorry about that. Here is a thought for the monitoring of events. Add the conrols using the Add method of the forms control collection, but DO NOT declare your variables. Then using the newly created controls window Handle (hWnd) hook into its message queue and monitor the events there. I checked using SPY and controls added this way do produce all of the event messages. All you would need is a standard routine that would monitor and dispatch all event messages generated by the dynamically added controls. - Jeff Marler
(please note, that the page is under construction)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top