-
Add new control in form at run-time
[Originally posted by Elias Gregory]
For my application, I need to add new picture in a form (for example with a Commandbutton click) during run-time. My first idea, was to use "as new" and a pointer to stack the new picture. It's ok for a form but it seems not correct for a control...
Thanks for your help (and sorry for my poor english)
-
Re:Add new control in form at run-time
[Originally posted by Greg DeBacker]
You can use a control array and add new controls to the array at runtime. Here is an example.
Add a PictureBox and a CommandButton to a new form. Set the PictureBox.Index = 0 in the properties window. This will initialize the array.
Paste the code below in the forms declarations section. Press F5 to run the program. When you click the command button you will create new pictureboxes.
Option Explicit
Dim iNewIndex As Integer
Private Sub Command1_Click()
iNewIndex = iNewIndex + 1
ScaleMode = vbTwips
Load Picture1(iNewIndex)
Picture1(iNewIndex).Visible = True
Picture1(iNewIndex).Left = Picture1(iNewIndex - 1).Left + 500
Picture1(iNewIndex).Top = Picture1(iNewIndex - 1).Top + 500
End Sub
Greg
-
Re:Add new control in form at run-time
[Originally posted by Lee Seddon]
You could use a control array and then dynamically add a new control at run time, below adds 5 new command buttons at run time.ÿ First add a command button placing it at the top of the form and name it "cmdButton", then change it's index property to 0, insert ‘Show’ in the Tag property of the command button and finally paste the code and run.
'***********************************************
Private Sub cmdbutton_Click(Index As Integer)
ÿ ÿ Dim Counter As Integer
ÿ ÿ
ÿ ÿ Select Case cmdButton(0).Tag
ÿ ÿ ÿ ÿ
ÿ ÿ ÿ ÿ Case "Show"
ÿ ÿ ÿ ÿ ÿ ÿ
ÿ ÿ ÿ ÿ ÿ ÿ For Counter = 1 To 5
ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ
ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ cmdButton(0).Tag = "Hide"
ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ Load cmdButton(Counter)
ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ
ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ With cmdButton(Counter)
ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ .Top = cmdButton(Counter - 1).Top + cmdButton(Counter - 1).Height
ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ .Caption = "Button " & Counter
ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ .Tag = ""
ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ .Visible = True
ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ End With
ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ
ÿ ÿ ÿ ÿ ÿ ÿ Next
ÿ ÿ ÿ ÿ
ÿ ÿ ÿ ÿ Case "Hide"
ÿ ÿ ÿ ÿ ÿ ÿ
ÿ ÿ ÿ ÿ ÿ ÿ If Index > 0 Then
ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ MsgBox "You pressed button " & Index
ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ Exit Sub
ÿ ÿ ÿ ÿ ÿ ÿ End If
ÿ ÿ ÿ ÿ ÿ ÿ
ÿ ÿ ÿ ÿ ÿ ÿ cmdButton(0).Tag = "Show"
ÿ ÿ ÿ ÿ ÿ ÿ
ÿ ÿ ÿ ÿ ÿ ÿ For Counter = 1 To 5
ÿ ÿ ÿ ÿ ÿ ÿ ÿ ÿ Unload cmdButton(Counter)
ÿ ÿ ÿ ÿ ÿ ÿ Next
ÿ ÿ ÿ ÿ ÿ ÿ
ÿ ÿ End Select
End Sub
'************************************************
-
Re:Re:Add new control in form at run-time
[Originally posted by Elias Gregory]
Thank you very much for your help. It was very easy. I have to be in a brainstorming period.
Gregory
-
Re:Re:Add new control in form at run-time
[Originally posted by Elias Gregory]
Thanks for your help. It was very easy.
Gregory
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks