?
Solved

VB Dynamic Button

Posted on 2003-02-24
11
Medium Priority
?
208 Views
Last Modified: 2011-08-18
Hi

i create a Dynamic Button : Form1.Controls.Add "VB.CommandButton", "Button1", Frame1
Dim oButton As VB.CommandButton
Set oButton = Form1.Controls("Button1")
With oButton
.Top = 33600
.Left = 3700
.Caption = "Save"
.Width = 2000
.Height = 500
.Visible = True
End With

now i wanna make him to something like that when i click on it

For Each ctl In Form2.Controls
   If (TypeOf ctl Is OptionButton) Then
       If ctl.Value = True Then
           Print #ff, ctl.Caption
           
           ctl.Value = False
       End If
   End If
Next ctl

so basicly my questins is :
How do i do a Button1_Click() with a dynamic button

thx
Italbro
0
Comment
Question by:Italbro5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 140 total points
ID: 8009031
Private WithEvents oButton As CommandButton

Private Sub Form_Load()
Set oButton = Controls.Add("VB.CommandButton", "oButton")
With oButton
  .Top = 33600
  .Left = 3700
  .Caption = "Save"
  .Width = 2000
  .Height = 500
  .Visible = True
End With
End Sub


Private Sub oButton_Click()
 ...
End Sub
0
 

Author Comment

by:Italbro5
ID: 8009046
Dhaest :
the only thing i need to has is this :
Private WithEvents oButton As CommandButton

if i dont write this, does it mean it wont work ?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 8009082
Private WithEvents btnObj As CommandButton

Private Sub Form_Load()
Set oButton= Controls.Add("VB.CommandButton", "oButton")
With oButton
   .Top = 33600 ' are you sure of value, try 0 to start
   .Left = 3700
   .Caption = "Save"
   .Width = 2000
   .Height = 500
   .Visible = True
End With
End Sub

private oButton_click
...
end sub
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Dhaest
ID: 8009087
Oops, sorry, my browser did something weird.
Yes, that's the only rule you need to make it work
0
 

Author Comment

by:Italbro5
ID: 8009142
Dhaest dosent work
it does nothing when i click

look what u wrote :
Private WithEvents btnObj As CommandButton

private oButton_click
...
end sub

not the same name

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 8009163
Just take a look at the first example I gave. There is the name the same !
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 8009329
I've question to ask  Dhaest , no offense


If you can declare like that why not just add button to the form?
0
 
LVL 3

Expert Comment

by:QJohnson
ID: 8009801
Using the Controls.Add method for adding command buttons is not very useful for the reasons you note here: they have no code in their events.  This is fine for labels and textboxes, I guess.  But for buttons and menu items, your best bet is the "old school" method of creating a control array and loading additional members as necessary.

Example:
 You have command button named cmdReadFile and assign its Index property Zero.  Its purpose is to read the contents of a text file into some string buffer array that is a form-level string var.  We assume there are potentially many of these files named SourceFile_01, SourceFile_02, etc and that you will create an element in the string array for each file.  Maybe not very worthwhile, but an understandable design target.
 When you look in click event, you will see that there is now an Index argument.  So you can write code here in anticipation of the buttons you will add at runtime, like this:

Private Sub cmdReadFile( Index as integer )
   dim nFileHandle as integer
   dim sTemp as string
   nFileHandle = FreeFile
   Open sBaseName & Format$(Index + 1, "00") for input as #nFileHandle
   Do while not EOF(nFileHandle)
      Line Input #nFileHandle, sTemp
      msFileContents(Index + 1) = msFileContents(Index +1) & sTemp
   Loop
   Close #nFileHandle
End Sub

When you know you how many files, you can just add the command buttons like this:

   For i = 1 to intNumberNeeded - 1
      Load cmdReadFile(i)
      cmdReadFile(i).Top = cmdReadFile(i-1).Top + 630
      '  assume we will just be stacking the buttons here
      '  you would likely be changing the caption in real life, right?
      cmdReadFile(i).Visible = True  ' they're invisible by default
   Next

   ReDim msFileContents(0 to intNumberNeeded - 1) as String

... and you're ready to go
0
 

Author Comment

by:Italbro5
ID: 8010124
QJohnson are you 100% sure u can add code for a boutton create diynamicly ???

0
 
LVL 3

Expert Comment

by:QJohnson
ID: 8010368
If you'll notice, there isn't any "dynamic" code in this technique.  It's just written in such a way that it anticipates being used by any number of newly-added members of the control array.  

I am 100% sure that this works because I dynamically populate menus in many of my projects with this same technique.

I'll bet this codes can convince you:

  1) start a new Standard exe project
  2) open the code widow for the form in module view
  3) select the code there (it should only be the declaration and end sub for Form_Load
  4) paste the code below over it
  5) double click a command button onto your form and position it near the top
  6) set the button's  index property to zero
  7) hit F5 to run it and you'll see the new buttons and observe their behavior.

It's crude but it should give you ideas about how you want to do your own.

>>>>>>>> code to paste into new project follows: >>>>>>>>

Private Sub Command1_Click(Index As Integer)
   If Index = 0 Then
      MsgBox "I was here at design time"
   Else
      MsgBox "I was added, my index is: " & Index
   End If
   
End Sub

Private Sub Form_Load()
   Dim i As Integer
   
   For i = 1 To 5
      Load Command1(i)
      With Command1(i)
         .Top = Command1(i - 1).Top + 630
         .Caption = "I'm new - number: " & i
         .Visible = True
      End With
   Next
End Sub

<<<<<<<< end of code to paste  <<<<<<<<<<

For your own code, if you can't think of any way to use the index property to cause the right code to run, remember you can always be a bit less elegant and put a select case statement in the button's click event like this:

 Private Sub cmdMine_Click (Index as Integer)
    Select Case Index
       Case 0
           '  code for the one available at design time
       Case 1
           '  code for the first one you add
       Case 2
           '  code for the second one you add
 End Sub

This looks a lot more elegant if you declare some constants for these, of course, e.g.:

Private Const BA_READ_FILE     As Integer = 0
Private Const BA_WRITE_FILE    As Integer = 1
Private Const BA_DELETE_FILE   As Integer = 2
(using BA as a cue that these are ButtonArray constants)

so that the case statement becomes -->
   Select Case Index
      Case BA_READ_FILE
         ' presumably code to read a file
      Case BA_WRITE_FILE
          .... etc.,

give it a try... it's really pretty easy stuff

Q
0
 

Author Comment

by:Italbro5
ID: 8010829
I dont know why it wasnt working
but now it does
thx
Italbro
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

801 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