PrivateSub ButtonClickHandler(ByVal sender AsObject, ByVal e As EventArgs)
Dim btn As Button
btn = CType(sender, Button)
If btn.Text = "CANCEL"Then
end
'the test could also be for the Button.Name or Button.Tag data
EndIf
EndSub
One of the buttons that is displayed automatically is a button with the text cancel on it. I want that when someone press that button it ends the program or do what ever else i want it to do, for ex go back to a previous screen.
You're not creating a new instance of each button. When you make the array, it creates the dimensions and sets the type to button but doesn't actually create any new buttons.
Try this mod to your code:
Dim modelbuttons(4, 4) As Button
Dim row AsInteger = 1
Dim column AsInteger = 1
PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
Dim reader As System.IO.StreamReader = _
New System.IO.StreamReader("C:\CARS.txt")
WhileNot reader.EndOfStream
Dim str AsString = reader.ReadLine
Dim btn AsNew Button
btn.Size = New System.Drawing.Size(90, 76)
btn.Location = New System.Drawing.Point(90 * row, 76 * column)
btn.BackColor = System.Drawing.Color.Aqua
btn.Text = str
Me.Controls.Add(btn)
modelbuttons(row - 1, column - 1) = btn
column += 1
If column > 4 Then
row += 1
column = 1
EndIf
EndWhile
reader.Close()
EndSub
That also fixes a layout issue (you're sizes and locations caused overlaps).
I have a .txt file which has names of 16 cars. I want to have a 4x4 array , rows and columns. I want all 16 buttons to be created automatically and display on each buttons the name of each car. Here is what i have so far but i keep get that NullReference error.
Option Strict On Imports System.IO
Public Class Form1
Dim models(4, 4) As String
Dim modelbuttons(4, 4) As Button Dim btns As String
Dim row As Integer
Dim column As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim reader As StreamReader = _ New StreamReader("c:\CARS.txt")
For column = 1 To 4 For row = 1 To 4
Do models(row, column) = (reader.ReadLine)
modelbuttons(row, column).Text = models(row, column) Loop Until reader.Peek = -1
Or use the .Name property (presumably it's named something like CancelButton), then you can change the text for the time that you need to use a different language...
One more thing. I need to generate a action behind the cancel button which is one of the buttons that will be displayed automatically. Since as the buttons are being displayed from a file how can I set an action behind one of the buttons. I hope you can help.
The button still has a name; it might be "Button14" but it has a name.
WHen you create the buttons from the array, you can set the name:
btn.Name = "ButtonName"
You set a breakpoint in your code by clicking in the vertical grey bar on the far left of the code window at the line where you need the breakpoint. Then run your program. WHen the breakpoint is reached the program will pause and VS will have a yellow highlight on the line. Press F8 to start stepping through your code. You can hover the mouse over your variables and see what their values are.
Place a breakpoint on the ButtonClick event handler subroutine. Click your cancel button. Is the breakpoint reached If not, no handler has been attached to the Cancel button's click event. If the breakpoint is reached, step through the code and check the value of the Text property for the button that raised the event. Be sure your code is testing for the exact text. If matching the text is a problem, use Tag data or the button's name.
Array of Buttons
Nfrf
Private Sub ButtonClickHandler(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button
btn = CType(sender, Button)
If btn.Text = "CANCEL" Then
end
'the test could also be for the Button.Name or Button.Tag data
End If
End Sub
One of the buttons that is displayed automatically is a button with the text cancel on it. I want that when someone press that button it ends the program or do what ever else i want it to do, for ex go back to a previous screen.
Any thoughts
Thanks!!
yonis
You're not creating a new instance of each button. When you make the array, it creates the dimensions and sets the type to button but doesn't actually create any new buttons.
Try this mod to your code:
Dim modelbuttons(4, 4) As Button
Dim row As Integer = 1
Dim column As Integer = 1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim reader As System.IO.StreamReader = _
New System.IO.StreamReader("C:\CARS.txt")
While Not reader.EndOfStream
Dim str As String = reader.ReadLine
Dim btn As New Button
btn.Size = New System.Drawing.Size(90, 76)
btn.Location = New System.Drawing.Point(90 * row, 76 * column)
btn.BackColor = System.Drawing.Color.Aqua
btn.Text = str
Me.Controls.Add(btn)
modelbuttons(row - 1, column - 1) = btn
column += 1
If column > 4 Then
row += 1
column = 1
End If
End While
reader.Close()
End Sub
That also fixes a layout issue (you're sizes and locations caused overlaps).
Hope that helps.
Peter Cwik
What you posted should work. You'll have to match the case so you might want to use "If btn.Text.Trim.ToUpper = "CANCEL" Then..."
What problem are you having
Francisco Tavares
Here is what i have so far but i keep get that NullReference error.
Option Strict On
Imports System.IO
Public Class Form1
Dim models(4, 4) As String
Dim modelbuttons(4, 4) As Button
Dim btns As String
Dim row As Integer
Dim column As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim reader As StreamReader = _
New StreamReader("c:\CARS.txt")
For column = 1 To 4
For row = 1 To 4
Do
models(row, column) = (reader.ReadLine)
modelbuttons(row, column).Text = models(row, column)
Loop Until reader.Peek = -1
Me.modelbuttons(row, column).Size = New System.Drawing.Size(90, 76)
Me.modelbuttons(row, column).Location = New System.Drawing.Point(29 * row, 27 * column)
Me.modelbuttons(row, column).BackColor = System.Drawing.Color.Aqua
Me.Controls.Add(modelbuttons(row, column))
Next row
Next column
reader.Close()
Close()
End Sub
End Class
Nikster
Here's a mod to the previous code that demonstrates the AddHandler:
Dim modelbuttons(4, 4) As Button
Dim row As Integer = 1
Dim column As Integer = 1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim reader As System.IO.StreamReader = _
New System.IO.StreamReader("C:\CARS.txt")
While Not reader.EndOfStream
Dim str As String = reader.ReadLine
Dim btn As New Button
btn.Size = New System.Drawing.Size(90, 76)
btn.Location = New System.Drawing.Point(90 * row, 76 * column)
btn.BackColor = System.Drawing.Color.Aqua
btn.Text = str
AddHandler btn.Click, AddressOf ButtonClickHandler
Me.Controls.Add(btn)
modelbuttons(row - 1, column - 1) = btn
column += 1
If column > 4 Then
row += 1
column = 1
End If
End While
reader.Close()
End Sub
Private Sub ButtonClickHandler(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button
btn = CType(sender, Button)
If btn.Text = "Text to test for" Then
'the test could also be for the Button.Name or Button.Tag data
End If
End Sub
Good luck
chare
Or use the .Name property (presumably it's named something like CancelButton), then you can change the text for the time that you need to use a different language...
andy_Lee
aus82
You'll use the AddHandler statement:
AddHandler btn.Click, AddressOf ButtonClickHandler
The "ButtonClickHanlder" is just a subroutine with the correct signiture
Private Sub ButtonClickHandler(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Jonathan Brown
kcchesnut
Any other thoughts
Thanks
hazz
The button still has a name; it might be "Button14" but it has a name.
WHen you create the buttons from the array, you can set the name:
btn.Name = "ButtonName"
You set a breakpoint in your code by clicking in the vertical grey bar on the far left of the code window at the line where you need the breakpoint. Then run your program. WHen the breakpoint is reached the program will pause and VS will have a yellow highlight on the line. Press F8 to start stepping through your code. You can hover the mouse over your variables and see what their values are.
NickoM
That's not much to go on...
You create controls via code like:
Dim b As New Button
b.Text = "Button Text"
b.Location = New Point(x,y)
Addhandler b.Click, AddressOf ButtonClickHandlerSub
Me.Controls.Add(b)
Using a TableLayoutPanel may facilitate laying out the buttons in a grid format.
I'm not sure where you want the array to come into play, but you'd probably be better off using a collection of some type rather than an array.
Rakesh Luhar
Learning VB