adding a button event to a generated form

jnash

Well-known member
Joined
Oct 20, 2006
Messages
111
Programming Experience
Beginner
my application generates forms via writing to a text file and then compiles via vbc, im having trouble adding the button onclick event, im getting errors saying the the method for onclick is incorrect, which it probably is

my app outputs the below code, im not sure what i need in there to make a button method work
thanks

VB.NET:
Imports system.Drawing.color
Imports AxWMPLib.AxWindowsMediaPlayer
Public Class grrrrrr
Shared Sub Main()
Dim temp1 As New System.Drawing.Size
Dim temp As New System.Drawing.point

Dim grrrrrr As New form
grrrrrr.Text = "grrrrrr"
grrrrrr.BackColor =White
grrrrrr.width = 800
grrrrrr.height = 600

Dim image1 As New PictureBox
temp.X =137
temp.Y =42
image1.Location = temp
temp1.width = 490
temp1.height = 262
image1.Size = temp1
image1.Parent = grrrrrr
image1.ImageLocation ="C:\Documents and Settings\halo9\My Documents\Visual Studio 2005\Projects\Multimedia Authoring 

System\bin\media\Image\glory1.jpg"
image1.SizeMode = PictureBoxSizeMode.StretchImage

Dim button1 As New Button
temp.X =135
temp.Y =310
Button1.Location = temp
temp1.width = 491
temp1.height = 85
Button1.Size = temp1
Button1.Parent = grrrrrr
Button1.backColor = red


Dim hs1 As New Button
temp.X =482
temp.Y =410
hs1.Location = temp
temp1.width = 143
temp1.height = 65
hs1.Size = temp1
hs1.Parent = grrrrrr
hs1.backColor = Transparent
hs1.foreColor = Transparent
hs1.flatstyle = FlatStyle.Flat

grrrrrr.Showdialog()

end sub

Shared Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Process.Start("2.exe")

me.close
End Sub


end Class

im baffled anyone help?
 
thank you jon, i have recoded the application to spit out the below code however im getting errors with ad events so what ive done is look at the generated text and found a addevents command, however nothing works when i click on the buttons , any ideas thanks.

VB.NET:
Imports System
Imports Microsoft.VisualBasic
Imports System.Windows.Forms
Imports system.Drawing.color
Imports AxWMPLib.AxWindowsMediaPlayer
Public Class grrrrrr

Inherits System.Windows.Forms.Form


Dim temp1 As New System.Drawing.Size
Dim temp As New System.Drawing.point

Shared Sub Main()

Application.Run(New grrrrrr)
end sub

Public Sub form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

me.Text = "grrrrrr"

me.BackColor =green
me.width = 800
me.height = 600

Dim image1 As New PictureBox
temp.X =137
temp.Y =42
image1.Location = temp
temp1.width = 490
temp1.height = 262
image1.Size = temp1
image1.Parent = me
image1.ImageLocation ="C:\Documents and Settings\halo9\My Documents\Visual Studio 2005\Projects\Multimedia Authoring 

System\bin\media\Image\glory1.jpg"
image1.SizeMode = PictureBoxSizeMode.StretchImage

Dim button1 As New Button

temp.X =135
temp.Y =310
Button1.Location = temp
temp1.width = 491
temp1.height = 85
Button1.Size = temp1
Button1.Parent = me
Button1.backColor = yellow

Dim button2 As New Button
temp.X =280
temp.Y =415
Button2.Location = temp
temp1.width = 189
temp1.height = 72
Button2.Size = temp1
Button2.Parent = me
Button2.backColor = red

Dim hs1 As New Button
temp.X =482
temp.Y =410
hs1.Location = temp
temp1.width = 143
temp1.height = 65
hs1.Size = temp1
hs1.Parent = me
hs1.backColor = Transparent
hs1.foreColor = Transparent
hs1.flatstyle = FlatStyle.Flat

end sub


public Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
image1.ImageLocation = "C:\"
End Sub

Friend WithEvents button1 As System.Windows.Forms.Button
Friend WithEvents image1 As System.Windows.Forms.PictureBox


end Class
 
Last edited:
adding a button event to a generated form:

Option1: declare the control WithEvents and use the Handles clause on the event handler method. I'm sure you have seen this many times in the generated code in Visual Studio. And I emphasize again: look at the generated code of a form with some controls in Visual Studio, if form is file Form1.vb you should be looking into Form1.Designer.vb.

Option2: Use the AddHandler statement to attach a new objects event to an event handler method dynamically. For a generated form like yours there is no practical reason to use this approach.
 
thank you once again John, i have managed to write the handler and it workes but in the handler i tried

system.process(blah.exe) however it doesnt recognise this, does this part need a reference or a import statement
 
Another comment because I have seen much of the troubles you have been asking about this form generating stuff: Make the code happen in Visual Studio first, just create the project as if you were creating a normal static application, the code that is the result of this is what you have to generate. Simple as that. Trying to add lines of code manually in this text file is absolutely utterly ridiculous, especially when you don't have a clear knowledge about the basic mechanisms of namespaces, classes, forms, events, variables etc. The Visual Studio editor will generate the code for the controls for you, all you have to do is read it and understand it, then replicate it. The Visual Studio editor will tell about every little coding error and usually tell how to fix it or give an indication how right away, without you having to recompile and read these cryptic messages in a command window that is half impossible to debug.
 
There is one more place in Visual Studio I know you haven't discovered, because I see you have also problems with qualifying variables to namespace and/or not importing the necessary namespaces; That is, Project Properties the References tab - there you can see what additional namespaces is imported, these don't show anywhere else in generated code but they are needed still.
 
Back
Top