Solved

Creating Controls  in run-time

Posted on 1998-09-08
7
142 Views
Last Modified: 2010-04-30
How can I create a control (any control, a textbox, a picture control, any OCX control,etc..) in runtime, from scrach?. I mean, it is possible to create a control via code?
0
Comment
Question by:Guillo
7 Comments
 

Expert Comment

by:janim
ID: 1433672
you cannot create a control from scratch unless you already have at least one control (of the type you want to create) that is loaded on your form with index property set to any value.

if you don't want to see this initial control on you form, just set its visible property to false.  you create controls using the 'load' command then make its visible property to true.
0
 

Author Comment

by:Guillo
ID: 1433673
Thanks, Janim, i've alredy assumed that a base control was necesary but i need more information.
OK, I understand the fact to have a contron of the type i want. Then the question is:
Let's suppose i have a command button for 1 action(in this case hidden, because i want this only as a "base"). How can i create copies of this button for x actions (assume that there is a variable ammount of actions, so i don't know how many buttons they may be)?
And, It's possible to do the same with other controls (Activex OCX, data controls, etc)?
Thanks.
0
 

Author Comment

by:Guillo
ID: 1433674
Please help me, i need an answer as soon as possible.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:KEWL
ID: 1433675
there is a function calles 'LoadControl' in VB5, but you need an objects CLSID to use it. but look it up and try it
0
 

Expert Comment

by:janim
ID: 1433676
can you further explain your question?
by action do you mean the event corresponding to your control?
can you give me a sample algorithm of what you need to do?  

0
 
LVL 14

Accepted Solution

by:
waty earned 100 total points
ID: 1433677
Here is code to add / delete controls at runtime based on a control. This is a sample. I don't know your project :

Private Sub cmdAdd_Click ()
      If MaxId = 0 Then MaxId = 1      ' Set total option
                                                                  ' buttons.
      If MaxId > 8 Then Exit Sub      ' Only ten buttons
                                                                  ' allowed.
      MaxId = MaxId + 1                  ' Increment button count.
      Load optButton(MaxId)            ' Create new button.
      optButton(0).SetFocus            ' Reset button selection.
      ' Set new button under previous button.
      optButton(MaxId).Top = optButton(MaxId - 1)._
      Top + 400
      optButton(MaxId).Visible = True      ' Display new
                                                                  ' button.
      optButton(MaxId).Caption = "Option" & MaxId + 1
End Sub
      
Private Sub cmdDelete_Click ()
      If MaxId <= 1 Then Exit Sub      ' Keep first two
                                                                  ' buttons.
      Unload optButton(MaxId)            ' Delete last button.
      MaxId = MaxId - 1                        ' Decrement button count.
      optButton(0).SetFocus            ' Reset button selection.
End Sub

0
 

Author Comment

by:Guillo
ID: 1433678
Thank you!
I was seasrching for a general solution aplicable for all cases. And that's is!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question