How to use button click command

Simke

Member
Joined
Sep 18, 2006
Messages
6
Location
Belgium
Programming Experience
Beginner
With this code i add the button to my form

VB.NET:
Function Knop1()
Dim Button1 AsNew Button
Button1.Location = New Point(30, 40)
Button1.Size = New Size(160, 24)
Button1.Text = "Klik hier voor vraag 1."
Me.Controls.Add(Button1)
EndFunction
but when i want to this, it won't do a thing:


--> Here is my problem:
VB.NET:
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Vraag1()
EndSub
<--


VB.NET:
Function Vraag1()
Dim strVraag1 AsString
Dim strAntwoord1A AsString
Dim strAntwoord1B AsString
Dim strAntwoord1C AsString
Dim GroupBox1 AsNew GroupBox
Dim CheckBox1A AsNew CheckBox
Dim CheckBox1B AsNew CheckBox
Dim CheckBox1C AsNew CheckBox
strVraag1 = InputBox("Voer vraag " & intTeller & " in.", "Vraag " & intTeller)
strAntwoord1A = InputBox("Voer antwoord 1 in.", "Antwoord 1")
strAntwoord1B = InputBox("Voer antwoord 2 in.", "Antwoord 2")
strAntwoord1C = InputBox("Voer antwoord 3 in.", "Antwoord 3")
GroupBox1.Location = New Point(30, 40)
GroupBox1.Size = New Size(540, 100)
GroupBox1.Text = strVraag1
Me.Controls.Add(GroupBox1)
CheckBox1A.Size = New Size(95, 24)
CheckBox1A.Location = New Point(20, 12)
CheckBox1A.Text = strAntwoord1A
GroupBox1.Controls.Add(CheckBox1A)
'De checkbox aan de groupbox toevoegen.
CheckBox1B.Size = New Size(95, 24)
CheckBox1B.Location = New Point(20, 38)
CheckBox1B.Text = strAntwoord1B
GroupBox1.Controls.Add(CheckBox1B)
'De checkbox aan de groupbox toevoegen.
CheckBox1C.Size = New Size(95, 24)
CheckBox1C.Location = New Point(20, 66)
CheckBox1C.Text = strAntwoord1C
GroupBox1.Controls.Add(CheckBox1C)
'De checkbox aan de groupbox toevoegen.
EndFunction

There ain't anything wrong with the function Vraag1()


 
Last edited by a moderator:
If you have an existing sub 'Button_Click' you want to use for a dynamically generated button you can attach the event handler with AddHandler method. Example:
VB.NET:
...
Me.Controls.Add(Button1)
AddHandler Button1.Click, AddressOf Button_Click
Perhaps you also want name your new controls something in event of you need to identify them later.
 
you shouldnt be using a function unless you're returning a value, you should be using a Sub instead:
VB.NET:
Private Sub Vraag1()
  Dim strVraag1 AsString
  Dim strAntwoord1A AsString
  Dim strAntwoord1B AsString
  Dim strAntwoord1C AsString
  Dim GroupBox1 AsNew GroupBox
  Dim CheckBox1A AsNew CheckBox
  Dim CheckBox1B AsNew CheckBox
  Dim CheckBox1C AsNew CheckBox
  '...
  CheckBox1C.Size = New Size(95, 24)
  CheckBox1C.Location = New Point(20, 66)
  CheckBox1C.Text = strAntwoord1C
  GroupBox1.Controls.Add(CheckBox1C)
  'De checkbox aan de groupbox toevoegen.
End Sub
 
If you have an existing sub 'Button_Click' you want to use for a dynamically generated button you can attach the event handler with AddHandler method. Example:
VB.NET:
...
Me.Controls.Add(Button1)
AddHandler Button1.Click, AddressOf Button_Click
Perhaps you also want name your new controls something in event of you need to identify them later.

It still doesn't work.

AddHandler Button1.Click, AddressOf Button_Click
the button gives error: "Name Button_Click is not declared"


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.click
Vraag1()
End Sub

"Button1" gives error: "Handles clause requires a WithEvents varialbe
 
Simke, remove that and put this in instaed:
VB.NET:
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  Vraag1()
End Sub
Now the suggested add handler code should suit you fine.
 
Simke, remove that and put this in instaed:
VB.NET:
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  Vraag1()
End Sub
Now the suggested add handler code should suit you fine.

Thank you very much, i understand now.
But now i have another problem :)

during the sub Vraag1() i want to make the Button1 invisible
but when i ad

Button1.visible = False

to the public sub Vraag1() it gives a error:
name Button1 is not declared.

I tried to ad "Dim Button1 As New Button" to my global variables but then it won't work neither.
 
Button1 is being declared as a procedure variable in your Knop1 sub therefor it's not usable outside of the Knop1 sub, declare the button towards the top of the code window (where the other module level variables are declared) then it'll be accessible throughout the that form
 
Button1 is being declared as a procedure variable in your Knop1 sub therefor it's not usable outside of the Knop1 sub, declare the button towards the top of the code window (where the other module level variables are declared) then it'll be accessible throughout the that form

ok thx, it works now
 
How can i save my screen so i can give the questionform to other people?
because i am making a test creator for teachers.
teacher has to login and then he gets a layout to create tests, and when students login they get to chose from a test. but how can i code that the pupils get the test the teachers created?
 

Attachments

  • printscreen.JPG
    printscreen.JPG
    81.8 KB · Views: 27
Back
Top