Module Syntax help

G

Gary Kahrau

I am trying to add some components dynamically.
It works if the code is within form1.
When I try to move it to a module, I don't get any errors, but the
components don't display.


also the addhandler syntac is giving me trouble.
AddHandler cb2.Click, AddressOf fx.cbDesignTime_Click(fx, ee)


Below is the sample module code.

Thanks,
Gary...


Public Module module1

' Inherits System.Windows.Forms.Form

Public Sub Create_Objects()

Dim x As Integer

Dim cbCount As Integer

Dim fx As New Form1

fx.Panel1.Visible = True

fx.Refresh()

Dim ee As New System.EventArgs

'create controls for the amount in the textbox

Try

For x = 0 To 4

' For x = 0 To CType(tbButtonAmount.Text, Integer) - 1

Dim cb2 As New System.Windows.Forms.Button

cb2.Size = New System.Drawing.Size(120, 30)

cb2.Location = New System.Drawing.Point(250, 40 + x * 40)

cb2.Name = "RunTime" & CStr(cbCount)

cb2.Text = "RunTime" & CStr(cbCount)

fx.Controls.Add(cb2)

cbCount += 1

'add the click event and point to existing click event

' AddHandler cb2.Click, AddressOf fx.cbDesignTime_Click(fx, ee)

Next

Catch

MsgBox("Please ensure you have entered a number of controls to create in the
texbox!")

fx.tbButtonAmount.Select()

End Try



End Sub





End Module
 
H

Herfried K. Wagner [MVP]

Gary Kahrau said:
I am trying to add some components dynamically.
It works if the code is within form1.
When I try to move it to a module, I don't get any errors, but the
components don't display.


also the addhandler syntac is giving me trouble.
AddHandler cb2.Click, AddressOf fx.cbDesignTime_Click(fx, ee)


Below is the sample module code.

Thanks,
Gary...


Public Module module1

' Inherits System.Windows.Forms.Form

Public Sub Create_Objects()

Dim x As Integer

Dim cbCount As Integer

Dim fx As New Form1

fx.Panel1.Visible = True

fx.Refresh()

The two lines above are useless because the form is not yet visible.

You'll either have to call 'fx.Show' after the controls/handlers were added
or add a parameter to the procedure that accepts an (already existing)
instance of 'Form1'.
 
G

Gary Kahrau

OK,
I got the buttons to appear in a panel on form1.

what is the correct syntax for the event handler.
AddHandler cb2.Click, AddressOf fy.cbDesignTime_Click(fy, ee) '
<======== Wrong =======
I can't seem to figure this out. See sample code from form1 and module1.

Thanks,
Gary...

----------------- Code in Module1 ---------------------------------

Public Sub Create_Objects(ByRef fy As Form, ByRef pnl As Panel)

Dim ee As New System.EventArgs



Dim cb2 As New System.Windows.Forms.Button
cb2.Size = New System.Drawing.Size(120, 30)
cb2.Location = New System.Drawing.Point(10, 40 + x * 40)
cb2.Name = "RunTime1"
cb2.Text = "RunTime1"

pnl.Controls.Add(cb2)

AddHandler cb2.Click, AddressOf fy.cbDesignTime_Click(fy, ee) '
<===================


End Sub

----------------------------------- Form1 button
click -----------------------------------------

Public Sub cbDesignTime_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cbDesignTime.Click
'get a reference to the sender onjects name
Dim cName As String = CType(sender, System.Windows.Forms.Button).Name
'check to see which control was clicked by name
If cName = "cbDesignTime" Then
MsgBox("You clicked the Button placed here in design time")
Else
Dim x As Integer
For x = 0 To cbCount
If cName = "RunTime" & x Then
MsgBox("You clicked RunTime" & x & " button")
End If
Next
End If
End Sub
 
H

Herfried K. Wagner [MVP]

Gary Kahrau said:
what is the correct syntax for the event handler.
AddHandler cb2.Click, AddressOf fy.cbDesignTime_Click(fy, ee) '
<======== Wrong =======

Remove the '(fy, ee') part and make sure the modifier of
'cbDesignTime_Click' is <> 'Private' or 'Protected'.
 
G

Gary Kahrau

Herfried,

On the sample that I sent you,
'cbDesignTime_Click' is a Public sub in the form. I guessed at that.
It the module, VB complains that 'cbDesignTime_Click' is not declared.
How do I declare a public click event that resides in the form?

Gary...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top