|
-
Apr 12th, 2002, 03:51 PM
#1
Thread Starter
Addicted Member
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
-
Apr 12th, 2002, 06:11 PM
#2
Frenzied Member
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:
Option Explicit On
Option Strict On
Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Namespace mdiExample
Public Class mdi : Inherits Form
<STAThread()> _
Public Shared Sub Main()
System.Windows.Forms.Application.Run(new mdi())
End Sub
Public Sub New()
MyBase.New()
Me.IsMdiContainer = True
Me.Text = "MDI Example"
Me.Show
Dim i As Short = 0
Dim frm As mdiChild
For i = 0 To 9
frm = new mdiChild
frm.MdiParent = Me
frm.Text = CStr(i)
frm.Show
Next i
End Sub
End Class
Public Class mdiChild : Inherits Form
Public Sub New()
MyBase.New
Dim btn As new Button
btn.Text = "A Button"
btn.Location = new Point(120,6)
Me.Controls.Add(btn)
AddHandler btn.Click, AddressOf Me.btn_Click
End Sub
Private Sub btn_Click(sender As Object, e As EventArgs)
MsgBox("A Button")
End Sub
End Class
End Namespace
Last edited by Magiaus; Apr 12th, 2002 at 06:15 PM.
Magiaus
If I helped give me some points.
-
Apr 15th, 2002, 10:34 AM
#3
Thread Starter
Addicted Member
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
-
Apr 15th, 2002, 11:00 AM
#4
Frenzied Member
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.
-
Apr 15th, 2002, 11:11 AM
#5
Frenzied Member
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.
-
Apr 15th, 2002, 11:18 AM
#6
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
-
Apr 15th, 2002, 11:29 AM
#7
Frenzied Member
A big project....
umm i hit the wrong button oops
Magiaus
If I helped give me some points.
-
Apr 15th, 2002, 12:05 PM
#8
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|