Results 1 to 8 of 8

Thread: How do I create an On_Click event for buttons created programattically? - Help!!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    New York
    Posts
    165

    How do I create an On_Click event for buttons created programattically? - Help!!

    Hi,

    I have an application which consists of an MDI Form and x amound of child forms which are created dynamically. Each of the child forms contains a button which is created on the fly, programattically:

    For i = 1 To intDocCount
    'Add new Form:
    f1 = New Form()
    f1.MdiParent = Me
    f1.Text = objWord.Documents.Item(i).FullName

    'Add 'Select Text' Button:
    myButton = New Button()
    myButton.Location = New System.Drawing.Point(120, 6)
    myButton.Visible = True
    f1.Controls.Add(myButton)
    f1.Show
    Next

    As the Buttons are created programattically and dynamically (ie, if there are 5 forms, 5 buttons are created) I am a little confused as to how to create an On_Click event for each of them.

    How do I create an On_Click event for each button created?

    Any help would be much appreciated

    Many thanks,

    Chris

  2. #2
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    ok that makes no since

    if the button is on the child why do you need to generate it from the mdi form? if you want help provide the needed details and put code in the code or vbcode tags

    here though:

    VB Code:
    1. Option Explicit On
    2. Option Strict On
    3.  
    4. Imports Microsoft.VisualBasic
    5.  
    6. Imports System
    7. Imports System.Drawing
    8. Imports System.Windows.Forms
    9. Namespace mdiExample
    10. Public Class mdi : Inherits Form
    11.  
    12.     <STAThread()> _
    13.     Public Shared Sub Main()
    14.         System.Windows.Forms.Application.Run(new mdi())
    15.     End Sub
    16.    
    17.     Public Sub New()
    18.         MyBase.New()
    19.        
    20.         Me.IsMdiContainer = True
    21.         Me.Text = "MDI Example"
    22.         Me.Show
    23.        
    24.         Dim i As Short = 0
    25.         Dim frm As mdiChild
    26.         For i = 0 To 9         
    27.             frm = new mdiChild
    28.             frm.MdiParent = Me
    29.             frm.Text = CStr(i)
    30.             frm.Show
    31.         Next i
    32.        
    33.     End Sub
    34. End Class
    35.  
    36. Public Class mdiChild : Inherits Form
    37.    
    38.     Public Sub New()
    39.         MyBase.New
    40.        
    41.         Dim btn As new Button
    42.        
    43.         btn.Text = "A Button"
    44.         btn.Location = new Point(120,6)
    45.        
    46.         Me.Controls.Add(btn)
    47.         AddHandler btn.Click, AddressOf Me.btn_Click
    48.     End Sub
    49.    
    50.     Private Sub btn_Click(sender As Object, e As EventArgs)
    51.         MsgBox("A Button")
    52.     End Sub
    53. End Class
    54. End Namespace
    Last edited by Magiaus; Apr 12th, 2002 at 06:15 PM.
    Magiaus

    If I helped give me some points.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    New York
    Posts
    165
    Thanks for the reply Magiaus. I am a little confused though.

    I want to create x amount of forms programmatically and this includes creating the button on each form programatically, which your codes seems to do fine.

    I guess I just need to get used to using .NET principles for coding now, but I am a little unsure of how to implement the 'mdiExample' Namespace you have created. Would the Namespace and Classes sit inside another module, which I would then inherit from my own code?

    Also, what does the <STAThread()> _ tag refer to?

    Any advice much appreciated

    Thanks, Chris

  4. #4
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    umm

    ok let me think i'm not the best teacher in the world but i'll post in a min
    Magiaus

    If I helped give me some points.

  5. #5
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    ok

    <STAThread()> I belive means start thread. I am unsure though because I can find no documentation on it. I do know that you only need to use it if you are placing your Main() inside a class. Maybe Cander or someone else can offer more on that.

    As far as the Namespace goes I only used the namespace to insure that there are no conflicts because i used mdi and mdi child as class names.

    If I were you I would just make two new *.vb files and adjust my code to your needs. I haven't made anything complex yet with .Net I have only been answering questions in here to help me learn it

    But I belive that you don't need to Import the namespace unless it is compiled code. i.e. if you put my file in your project you could just call my class or maybe create a namespace for your code call mdiExamle.MyCode but I'm not sure I'm getting ready to start a major project today and will know a more solid answer soon. that said i am going to make a new thread about the project i am starting cause i need people who want to work on it....
    Magiaus

    If I helped give me some points.

  6. #6
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    i was wonderng about STAThread also.. havent found any info on it yet..


    For you class, if you just add the class to your project, you dont need the imports statement..just instantiate the class

    Dim A as New Classname

    if it was in a seperate dll, the you need the namespace

    Imports mdiBlah

    Dim A as New Classname
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  7. #7
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Red face A big project....

    umm i hit the wrong button oops
    Magiaus

    If I helped give me some points.

  8. #8
    Lively Member MedevH's Avatar
    Join Date
    Aug 2001
    Location
    Where I walk, I walk alone. Where I code, I code alone.
    Posts
    91

    Hey CSF

    You say:

    I guess I just need to get used to using .NET principles for coding now
    Can i ask simply what "Priciples" you are used to.
    or more plainly what languages your familiar with.

    As Magiaus said he's not the best teacher but one helluva coder
    If I knew what language you are familiar with perhaps I could help you some with the conversion process.
    There is no good or evil only CODE! ~MedevH~

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