CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Join Date
    Mar 2011
    Posts
    153

    [RESOLVED] *Ironic angry face* Dynamically creating methods for dynamically created buttons

    Okay, I have this code.

    Code:
    Public Class Form1
    
        Dim NPB As Button
        Dim x As Integer
    
        Private Sub NewProgramToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewProgramToolStripMenuItem.Click
            Dim OpePro As String
            OpenFileDialog1.ShowDialog()
            OpePro = OpenFileDialog1.FileName
            Process.Start(OpePro)
            x = x + 25
            NPB = New Button
            NPB.Text = OpePro
            NPB.Left = 5
            NPB.Top = x
            Controls.Add(NPB)
        End Sub
    End Class
    As you can see, when I open a program from my menu, a new button will be created with text similar to this, "C:\Program ". The button it self will not do anything. How can I create the method at the same time the button it is for is created? Also how can I change the text do that it gives me, for example, CCleaner.exe instead of the whole target path? Also, I scream like a gerbil being hit on by a donkey in Tijuana.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons

    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Mar 2011
    Posts
    153

    Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons

    The link does not mention how to dynamically add a new function. By the way, I think I forgot to thank you in the other thread.

  4. #4
    Join Date
    Mar 2011
    Posts
    153

    Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons

    Okay, I got this far

    Code:
    Public Class Form1
    
        Dim NPB As Button
        Dim x As Integer
        Dim OpePro As String
    
        Private Sub NewProgramToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewProgramToolStripMenuItem.Click
            OpenFileDialog1.ShowDialog()
            OpePro = OpenFileDialog1.FileName
            Process.Start(OpePro)
            x = x + 75
            NPB = New Button
            NPB.Text = OpePro
            NPB.Width = 75
            NPB.Height = 62
            NPB.Left = x
            NPB.Top = 0
            Controls.Add(NPB)
            AddHandler NPB.Click, AddressOf Button1_Click
    
        End Sub
    
        Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Process.Start(OpePro)
        End Sub
    
    End Class
    When I create a second button this way to open a different program, the first button is overwritten

  5. #5
    Join Date
    Feb 2005
    Location
    Denmark
    Posts
    742

    Arrow Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons

    I've never done something similar to that, but my guess is that the reason your button is overwritten is that you use the same button variable
    Code:
    Dim NPB As Button
    Therefore by assigning a new object to the variable, you loose the reference to the old one.

    If you need a class variable for the button, try adding the button to a button list instead.
    Something like

    Code:
    Dim myButtonList As List(Of Button) 
    ......
     Private Sub NewProgramToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewProgramToolStripMenuItem.Click
           ....
            x = x + 75
            dim NPB = New Button
            NPB.Text = OpePro
            NPB.Width = 75
            NPB.Height = 62
            NPB.Left = x
            NPB.Top = 0
            Controls.Add(NPB)
            AddHandler NPB.Click, AddressOf Button1_Click
            myButtonList.Add(NPB) '      (remember to instantiate the list variable at one point)
        End Sub
    Whether it'll work I don't know because as I say I've not tried it myself, but off the top of my head - it might.
    Also you can use other collection types if you like and they serve your purpose.

  6. #6
    Join Date
    Mar 2011
    Posts
    153

    Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons

    That seems like I would have a redundant amount of code just to make other buttons. I was thinking more along the lines of a an array
    Last edited by 957; March 16th, 2011 at 01:40 AM. Reason: typo

  7. #7
    Join Date
    Mar 2011
    Posts
    153

    Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons

    In other words, I want to click on only one thing to create multiple buttons with separate functions.

  8. #8
    Join Date
    Mar 2011
    Posts
    153

    Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons

    no progress with this

    Code:
    Public Class Form1
    
        Dim OpePro(7) As String
        Dim posl As Integer
        Dim NPB As Button
    
        Private Sub Task_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Task.Click
            System.Diagnostics.Process.Start("taskmgr.exe")
        End Sub
    
        Private Sub Calc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc.Click
            System.Diagnostics.Process.Start("calc.exe")
        End Sub
    
        Private Sub Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add.Click
            OpenFileDialog1.ShowDialog()
            OpePro(0) = OpenFileDialog1.FileName
            OpePro(1) = OpenFileDialog1.FileName
            OpePro(2) = OpenFileDialog1.FileName
            OpePro(3) = OpenFileDialog1.FileName
            OpePro(4) = OpenFileDialog1.FileName
            OpePro(5) = OpenFileDialog1.FileName
            OpePro(6) = OpenFileDialog1.FileName
            OpePro(7) = OpenFileDialog1.FileName
    
            posl = posl + 75
    
            NPB = New Button
            NPB.Text = OpePro(0)
            NPB.Width = 75
            NPB.Height = 62
            NPB.Left = posl
            NPB.Top = 0
            Controls.Add(NPB)
            AddHandler NPB.Click, AddressOf Button1_Click
    
            NPB = New Button
            NPB.Text = OpePro(1)
            NPB.Width = 75
            NPB.Height = 62
            NPB.Left = posl
            NPB.Top = 0
            Controls.Add(NPB)
            AddHandler NPB.Click, AddressOf Button2_Click
    
    
    
            For i = 0 To OpePro.Length - 1
    
            Next
    
        End Sub
    
        Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Process.Start(OpePro(0))
        End Sub
    
        Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Process.Start(OpePro(1))
        End Sub
    End Class

  9. #9
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons

    Something like this?

    Code:
            For i = 0 To OpePro.Length - 1
              NPB = New Button
              NPB.Text = OpePro(i)
              NPB.Width = 75 i
              NPB.Height = 62 
              NPB.Left = posl + (posl * i)  ' some calcs
              NPB.Top = 0 +  (posl * i)     ' some more calcs
              Controls.Add(NPB)
              AddHandler NPB.Click, AddressOf Button2_Click
           Next
    They could share the event handler
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  10. #10
    Join Date
    Mar 2011
    Posts
    153

    Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons

    for this line

    Code:
    For i = 0 To OpePro.Length - 1
    it says "object reference not set to an instance of an object"

  11. #11
    Join Date
    Mar 2011
    Posts
    153

    Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons

    how about an if statement?

  12. #12
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons

    Make it PUBLIC instead of DIM
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  13. #13
    Join Date
    Mar 2011
    Posts
    153

    Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons

    is this right?

    Code:
    Public Class Form1
    
        Dim OpePro As String
        Dim posl As Integer
        Dim NPB As Button
        Public i As Integer
    
        Private Sub Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add.Click
            OpenFileDialog1.ShowDialog()
            OpePro = OpenFileDialog1.FileName
            posl = posl + 75
    
            For Me.i = 0 To OpePro.Length - 1
                NPB = New Button
                NPB.Text = OpePro(i)
                NPB.Width = 75
                NPB.Height = 62
                NPB.Left = posl
                NPB.Top = 0
                Controls.Add(NPB)
                AddHandler NPB.Click, AddressOf Button1_Click
            Next
    
        End Sub
    
        Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Process.Start(OpePro)
        End Sub
    
        Private Sub Task_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Task.Click
            System.Diagnostics.Process.Start("taskmgr.exe")
        End Sub
    
        Private Sub Calc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc.Click
            System.Diagnostics.Process.Start("calc.exe")
        End Sub
    End Class

  14. #14
    Join Date
    Mar 2011
    Posts
    153

    Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons

    the second button overwrote the first one so I don't think that it's what you meant

  15. #15
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons

    You need an ARRAY of buttons...

    Code:
      Dim NPB(7) As Button
    and a few (i)'s
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured