Programmatically add controls to form - vb.net

I'm using the attached code to add another line\row of controls beneath an existing set (when a label is clicked). There could be quite a few rows added so I'm having to repeat the code many times using the counter (i) to keep track...
Is there a better method for doing this?
Private Sub Label10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LblExpandSearch.Click
If i = 0 Then
'TextBox7
'
Dim TextBox7 As New TextBox
TextBox7.Size = New Size(302, 20)
TextBox7.Name = "TextBox7"
TextBox7.Location = New System.Drawing.Point(60, 135)
Me.ExpAdvancedSearch.Controls.Add(TextBox7)
'RadioButton5
'
Dim RadioButton5 As New RadioButton
RadioButton5.AutoSize = True
RadioButton5.Checked = True
RadioButton5.Location = New System.Drawing.Point(77, 112)
RadioButton5.Name = "RadioButton5"
RadioButton5.Size = New System.Drawing.Size(55, 17)
RadioButton5.TabIndex = 48
RadioButton5.TabStop = True
RadioButton5.Text = "NEAR"
RadioButton5.UseVisualStyleBackColor = True
ElseIf i = 1 Then
ExpAdvancedSearch.Size_ExpandedHeight = 260
'TextBox8
'
Dim TextBox8 As New TextBox
TextBox8.Size = New Size(302, 20)
TextBox8.Name = "TextBox8"
TextBox8.Location = New System.Drawing.Point(60, 185)
Me.ExpAdvancedSearch.Controls.Add(TextBox8)
'RadioButton9
'
Dim RadioButton9 As New RadioButton
RadioButton9.AutoSize = True
RadioButton9.Checked = True
RadioButton9.Location = New System.Drawing.Point(77, 162)
RadioButton9.Name = "RadioButton9"
RadioButton9.Size = New System.Drawing.Size(55, 17)
RadioButton9.TabIndex = 48
RadioButton9.TabStop = True
RadioButton9.Text = "NEAR"
RadioButton9.UseVisualStyleBackColor = True
End If
i = i + 1
End Sub

Hmmm.. UseVisualStyleBackColor says 'winforms' to me.
A few points...
Don't add controls all to one panel, use a usercontrol.
Then just add instances of that.
Don't process click events from a label
Use a linklabel or button. Anything else = being mean to users. Of course it makes sense to you, you thought of it! Now so with users, this is black and white.
Sample...
Very minimal of course. You'll want to:
Put the items in a scrollable panel instead of right on the form.
Add them to a generic list of uc probably, too.
Set form's min/max size - to allow reasonable sizing (allow any height > ~100)
Set uc's and controls .Anchor properties to allow reasonable resizing
uc.vb
Public Class uc
Inherits System.Windows.Forms.UserControl
Private components As System.ComponentModel.IContainer
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents LinkLabel1 As System.Windows.Forms.LinkLabel
Public Sub New()
MyBase.New()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.LinkLabel1 = New System.Windows.Forms.LinkLabel
Me.SuspendLayout()
Me.TextBox1.Location = New System.Drawing.Point(8, 8)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(88, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "TextBox1"
Me.LinkLabel1.Enabled = False
Me.LinkLabel1.Location = New System.Drawing.Point(112, 8)
Me.LinkLabel1.Name = "LinkLabel1"
Me.LinkLabel1.Size = New System.Drawing.Size(24, 16)
Me.LinkLabel1.TabIndex = 1
Me.LinkLabel1.TabStop = True
Me.LinkLabel1.Text = "add"
Me.Controls.Add(Me.LinkLabel1)
Me.Controls.Add(Me.TextBox1)
Me.Name = "uc"
Me.Size = New System.Drawing.Size(148, 36)
Me.ResumeLayout(False)
End Sub
Private _addcallback As EventHandler = Nothing
Public Property AddCallback() As EventHandler
Get
Return _addcallback
End Get
Set(ByVal Value As EventHandler)
_addcallback = Value
LinkLabel1.Enabled = Not Value Is Nothing
End Set
End Property
Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
If AddCallback Is Nothing Then Throw New ApplicationException("AddCallback not set on a uc") ' ALWAYS check for errors like this
_addcallback(Me, Nothing)
AddCallback = Nothing ' gray myself out, can't insert in thie implementation
End Sub
End Class
frm.vb
Public Class frm
Inherits System.Windows.Forms.Form
Private components As System.ComponentModel.IContainer
Public Sub New()
MyBase.New()
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Name = "Form1"
Me.Text = "Form1"
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddClicked(Me, Nothing)
End Sub
Private Sub AddClicked(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myuc As New uc
myuc.AddCallback = AddressOf AddClicked
If Controls.Count > 0 Then
myuc.Top = Controls(Controls.Count - 1).Bottom
End If
Me.Controls.Add(myuc)
End Sub
End Class

I don't know if there's a "less code" approach to this but I do know that you can save your fingers using a With statement.
Dim RadioButton5 As New RadioButton
With RadioButton5
.AutoSize = True
.Checked = True
.Location = New System.Drawing.Point(77, 112)
.Name = "RadioButton5"
.Size = New System.Drawing.Size(55, 17)
.TabIndex = 48
.TabStop = True
.Text = "NEAR"
.UseVisualStyleBackColor = True
End With

If you need to add an indefinite number of items to a single page, then you need to store those items in an array list that we can later add to the page dynamically.
Imports System.Collections.Generic
Partial Class Default2
Inherits System.Web.UI.Page
''# the i integer is here for helping to set the ID of the radio button
''# as well as the tabindex
Private Shared _i As Integer
Public Shared Property i As Integer
Get
Return _i
End Get
Set(ByVal value As Integer)
_i = value
End Set
End Property
''# we need to create an array of our control list class
Public Shared _ctrlList As List(Of ControlList)
''# page load event
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
''# if the page is not a postback, then we need to initialize the Control List
_ctrlList = New List(Of ControlList)
i = 0
End If
End Sub
''# button click event
Protected Sub button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button.Click
''# create a new RadioButton every time the button is clicked
Dim rb As RadioButton = New RadioButton
With rb
.ID = "radioButton" + i.ToString
.Checked = True
.TabIndex = 48 + i
.Text = "NEAR"
End With
''# create a new literal every time the button is clicked
Dim lt As Literal = New Literal
With lt
.ID = "literal" + i.ToString
.Text = " <strong>my fancy text</strong><br />"
End With
''# add the radio button and literal to our custom array
_ctrlList.Add(New ControlList(rb, lt))
''# loop through the array and add the controls to the page
For Each cl In _ctrlList
LabelPlaceHolder.Controls.Add(cl.RadioBtn)
LabelPlaceHolder.Controls.Add(cl.Litrl)
Next
''# increment the i counter so that we have unique radioButton ID's
i = i + 1
End Sub
''# this is our custom Control List
''# the idea behind this is for us to store
''# an array of Radio Buttons and literals to
''# spit out onto the page
''# NOTE: you can add as many controls as you like
''# to this list and even add static "Literals" to
''# help you with your formatting (IE: DIV tags or <BR> tags
Public Class ControlList
Private _RadioBtn As RadioButton
Public Property RadioBtn As RadioButton
Get
Return _RadioBtn
End Get
Set(ByVal value As RadioButton)
_RadioBtn = value
End Set
End Property
Private _Litrl As Literal
Public Property Litrl As Literal
Get
Return _Litrl
End Get
Set(ByVal value As Literal)
_Litrl = value
End Set
End Property
Public Sub New(ByVal radioBtn As RadioButton, ByVal litrl As Literal)
_RadioBtn = radioBtn
_Litrl = litrl
End Sub
End Class
End Class
Try this and see how it works. All you need in your ASPX is
<form id="form1" runat="server">
<asp:PlaceHolder runat="server" id="LabelPlaceHolder" /><br />
<asp:Button ID="button" runat="server" Text="click me" />
</form>
Basically what this does is add an additional control set to the page every time the button is clicked. You can have an indefinite number of controls on the page without adding any additional code.

Related

How do I code out with numeric keypads with multi textboxes?

I'm trying to code out a programme where the user sees a form and in that form, there are 2 text boxes and 10 buttons.
Username:
Password:
1 2 3
4 5 6
7 8 9
0
I've tried this code
Private Sub Btn1_Click(sender As Object, e As EventArgs) Handles Btn1.Click
If UsernameTextbox.Focused = True Then
UsernameTextbox.Text = UsernameTextbox.Text + "1"
End If
End Sub
I understand that clicking on Btn1 will steal the focus from the text box. So how can I write the programme?
One option would be to declare a variable of type Control and, in the Leave event handler for each control, assign the sender to that variable. You can then use that variable in the Click event handler of your Button to determine which control had focus and possibly reassign back to that control and then update it appropriately. You can do the lot with two event handlers, e.g.
Private previouslyActiveTextBox As TextBox
Private Sub TextBoxes_Leave(sender As Object, e As EventArgs) Handles TextBox2.Leave,
TextBox1.Leave
previouslyActiveTextBox = DirectCast(sender, TextBox)
End Sub
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button3.Click,
Button2.Click,
Button1.Click
previouslyActiveTextBox.Select()
previouslyActiveTextBox.SelectedText = CStr(DirectCast(sender, Button).Tag)
End Sub
That code handles multiple events with a single method in both cases. It also requires that you assign the number for each Button to the Tag property of that control. Note that it also sets the SelectedText, rather than appending to the Text property. That is more correct because it will add the new text where the caret is actually located and replace text if it is selected.
An even better option might be to use a custom button control that doesn't take focus. Here's one I prepared earlier:
http://www.vbforums.com/showthread.php?459890-Building-Blocks-for-an-On-screen-Keyboard
Items within a ToolStrip do not grab focus when clicked. While the standard ToolStrip usage is as a menu bar, there is nothing that prevents you from using it as a container for buttons laid out in a grid. In fact, the class ToolStrip.LayoutStyle Property allows you select a table style.
The following is a proof-of-concept custom ToolStrip that is prepopulated with the buttons to create a number pad like control. The control has sufficient function to work as intended, but is not locked down to prevent misuse by manipulating the Items collection and other properties.
Public Class NumPadToolstrip : Inherits ToolStrip
Private _ButtonSize As Size = New Size(50, 50)
Private _ButtonMargin As Padding = New Padding(5)
Private _ButtonBackColor As Color = Color.Ivory
Public Sub New()
MyBase.New
LayoutStyle = ToolStripLayoutStyle.Table
Dim settings As TableLayoutSettings = CType(LayoutSettings, TableLayoutSettings)
settings.ColumnCount = 3
settings.RowCount = 4
AddButtons(7, 9)
AddButtons(4, 6)
AddButtons(1, 3)
AddButtons(0, 0)
Dock = DockStyle.None
AutoSize = True
BackColor = Color.LightGray
End Sub
Public Property ButtonSize As Size
Get
Return _ButtonSize
End Get
Set(value As Size)
If value <> _ButtonSize Then
_ButtonSize = value
UpdateButtonSizes()
End If
End Set
End Property
Public Property ButtonMargin As Padding
Get
Return _ButtonMargin
End Get
Set(value As Padding)
If value <> _ButtonMargin Then
_ButtonMargin = value
UpdateMargins()
End If
End Set
End Property
Public Property ButtonBackColor As Color
Get
Return _ButtonBackColor
End Get
Set(value As Color)
If value <> _ButtonBackColor Then
_ButtonBackColor = value
UpdateButtonBackColor()
End If
End Set
End Property
Private Sub AddButtons(start As Int32, [end] As Int32)
For num As Int32 = start To [end]
Dim b As New ToolStripButton With {.Text = num.ToString(),
.Size = ButtonSize,
.Margin = ButtonMargin,
.BackColor = ButtonBackColor,
.AutoSize = False}
AddHandler b.Paint, Sub(sender As Object, e As PaintEventArgs)
With e.Graphics
Dim r As Rectangle = e.ClipRectangle
r.Inflate(-1, -1)
r.Location = Point.Empty
.DrawRectangle(Pens.Black, r)
End With
End Sub
Items.Add(b)
Next
End Sub
Private Sub UpdateButtonSizes()
SuspendLayout()
For Each btn As ToolStripButton In Items.OfType(Of ToolStripButton)
btn.Size = _ButtonSize
Next
ResumeLayout()
End Sub
Private Sub UpdateMargins()
SuspendLayout()
For Each btn As ToolStripButton In Items.OfType(Of ToolStripButton)
btn.Margin = _ButtonMargin
Next
ResumeLayout()
End Sub
Private Sub UpdateButtonBackColor()
SuspendLayout()
For Each btn As ToolStripButton In Items.OfType(Of ToolStripButton)
btn.BackColor = _ButtonBackColor
Next
ResumeLayout()
End Sub
End Class
Add the above class to your project and perform a build operation. The NumPadToolstrip control should then be available in the ToolBox. Add the control to the form and then add a handler for its ItemClicked event to pass the proper text to the TextBox.
Private Sub NumPadToolstrip1_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles NumPadToolstrip1.ItemClicked
Dim tb As TextBoxBase = TryCast(ActiveControl, TextBoxBase)
If tb IsNot Nothing Then tb.SelectedText = e.ClickedItem.Text
End Sub

Is it possible to have things defined and controlled inside a class, without being assigned in the "form" (outside the class) in VB?

My problem:
I have a checkbox I use to control if certain textboxes are enabled or not, and I need to do this around 30+ times. I've named my textboxes numerically/sequentially (TB_name_1, TB_name_2, etc) so if I know the Checkbox name I know which textboxes are affected.
My question:
Can I make a class for my checkboxes that says "if this box is checked/unchecked, then enable/disable these 3 textboxes" without the class also having to be told which textboxes (finds them itself)?
Here's the copy/paste code I'm currently using (not a class, obviously). I change the first 2 values and the rest of the code solves itself. (PS - I see you laughing)
Private Sub T1_cb_c_1_CheckedChanged(sender As Object, e As EventArgs) Handles T1_cb_c_1.CheckedChanged
'change here for current checkbox
Dim b As CheckBox = T1_cb_c_1
'change here for start value of first textbox (of 3), the next 2 will be in sequence
Dim a As Integer = 1
'How much of the below code can be moved to, and controlled from, a class?
Dim a1 As Integer = a + 1
Dim a2 As Integer = a + 2
Dim TB_PtNum As TextBox = Me.Controls.Find("T1_tb_c_" & a, True).FirstOrDefault
Dim TB_Qty As TextBox = Me.Controls.Find("T1_tb_c_" & a1, True).FirstOrDefault
Dim TB_Seq As TextBox = Me.Controls.Find("T1_tb_c_" & a2, True).FirstOrDefault
If b.Checked = True Then
TB_PtNum.Enabled = True
TB_Qty.Enabled = True
TB_Seq.Enabled = True
Else
TB_PtNum.Enabled = False
TB_Qty.Enabled = False
TB_Seq.Enabled = False
End If
End Sub
Here's a design time only class that will do this. You only have to the AssociatedCheckbox property in the designer:
Public Class TextBoxWithCheckboxProperty
Inherits TextBox
Private m_CheckBox As CheckBox
Public Property AssociatedCheckBox As CheckBox
Get
Return m_CheckBox
End Get
Set(value As CheckBox)
If Not m_CheckBox Is Nothing Then
RemoveHandler m_CheckBox.CheckedChanged, AddressOf OnCheckBoxChanged
End If
m_CheckBox = value
If Not value Is Nothing Then
AddHandler m_CheckBox.CheckedChanged, AddressOf OnCheckBoxChanged
End If
OnCheckBoxChanged(m_CheckBox, Nothing)
End Set
End Property
Private Sub OnCheckBoxChanged(ByVal sender As Object, ByVal e As System.EventArgs)
If Not sender Is Nothing Then
Me.Enabled = CType(sender, CheckBox).Checked
Else
Me.Enabled = False
End If
End Sub
End Class
Here's a sample Form1 that uses it:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.CheckBox1 = New System.Windows.Forms.CheckBox()
Me.TextBoxWithCheckboxProperty1 = New WindowsApp4.TextBoxWithCheckboxProperty()
Me.SuspendLayout()
'
'CheckBox1
'
Me.CheckBox1.AutoSize = True
Me.CheckBox1.Location = New System.Drawing.Point(293, 131)
Me.CheckBox1.Name = "CheckBox1"
Me.CheckBox1.Size = New System.Drawing.Size(81, 17)
Me.CheckBox1.TabIndex = 0
Me.CheckBox1.Text = "CheckBox1"
Me.CheckBox1.UseVisualStyleBackColor = True
'
'TextBoxWithCheckboxProperty1
'
Me.TextBoxWithCheckboxProperty1.AssociatedCheckBox = Me.CheckBox1
Me.TextBoxWithCheckboxProperty1.Location = New System.Drawing.Point(428, 131)
Me.TextBoxWithCheckboxProperty1.Name = "TextBoxWithCheckboxProperty1"
Me.TextBoxWithCheckboxProperty1.Size = New System.Drawing.Size(100, 20)
Me.TextBoxWithCheckboxProperty1.TabIndex = 1
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(800, 450)
Me.Controls.Add(Me.TextBoxWithCheckboxProperty1)
Me.Controls.Add(Me.CheckBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents CheckBox1 As CheckBox
Friend WithEvents TextBoxWithCheckboxProperty1 As TextBoxWithCheckboxProperty
End Class
I would use the property Tag for the related controls.
Suppose to set this property to the value "line1" for the first set of textboxes and on the checkbox that controls them.
Next row of controls (checkbox+textboxes) will have the property set to "line2" and so on until the last row. (You can do this through the Winforms Designer or through code)
At this point you could have a single event handler for all your checkboxes
Private Sub onCheckedChanged(sender As Object, e As EventArgs) _
Handles T1_cb_c_1.CheckedChanged, T2_cb_c_2.CheckedChanged, _
..... add other checkbox events here .......
' Get whatever checkbox has been clicked and extract its tag
Dim b As CheckBox = DirectCast(sender, CheckBox)
Dim tag = b.Tag.ToString()
' Find the textbox controls in this form with the same Tag
Dim ctrls = Me.Controls.OfType(Of TextBox).Where(Function(x) x.Tag.ToString() = tag)
' Enabled status matches the status of the Checked property
For Each c as TextBox in ctrls
c.Enabled = b.Checked
Next
End Sub

How to stop controls from flickering during MouseMove Event

I know this is a pretty popular question, but none of the solutions I have found have worked for me.
Background: I have a windows forms project in VS2015 that reads data from text files and plots the data as multiple series on a line chart. The Chart.MouseMove event finds the point nearest the mouse and draws a circle around it. The circle is drawn in the Chart_Paint event
Private Sub crtLogView(sender As Object,e As PaintEventArgs) Handles crtLogView.Paint
Dim whitePen as New Pne(Color.White,2)
e.Graphics.DrawEllipse(whitePen,cir) '//cir is a Public Rectangle
End Sub
When moving the mouse across the chart, random controls flicker off then back on which is very annoying. I have posted the MouseMove event code below.
Potential solutions I have tried:
Turning on the DoubleBuffered property of the form, which does nothing
Using the Me.Invalidate() and Me.Update() method, which does not move the circle
Using the Chart.Invalidate() and Chart.Update() method, which works, but is very slow
Adding the following code to my Form_Load routine, which appears to do nothing
Any help with this would be greatly appreciated
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.DoubleBuffer, True)
Me.SetStyle(ControlStyles.UserPaint, True)
MouseMove Event Code:
Private Sub crtLogView_MouseMove(sender As Object, e As MouseEventArgs) Handles crtLogView.MouseMove
'//Show data for closest point to cursor & draw circle around point
Dim hResult As HitTestResult = crtLogView.HitTest(e.X, e.Y)
Dim srsNam As String = ""
Dim mouseY As Single
Dim pntDist As Double = 0
Dim pntX As Single
Dim pntY As Single
Dim mouseX As Single
On Error GoTo ErrTrap
'//Get X-Axis Position as integer
mouseX = Int(hResult.ChartArea.AxisX.PixelPositionToValue(e.X))
'// Set time value
lblTime.Text = String.Format("{0:n2}", hResult.ChartArea.AxisX.PixelPositionToValue(e.X) / 160)
'//Get Y-Axis Position
mouseY = hResult.ChartArea.AxisY.PixelPositionToValue(e.Y)
'//Get distance from mouse to point on Series(0)
pntDist = Math.Abs(crtLogView.Series(0).Points(mouseX).YValues(0) - mouseY)
srsNam = crtLogView.Series(0).Name '//1st series name
'//Find closest series
For i As Integer = 1 To crtLogView.Series.Count - 1
If Math.Abs(crtLogView.Series(i).Points(mouseX).YValues(0) - mouseY) < pntDist Then
pntDist = Math.Abs(crtLogView.Series(i).Points(mouseX).YValues(0) - mouseY)
srsNam = crtLogView.Series(i).Name
End If
Next
'//Set Top/Left values for circle
pntY = crtLogView.ChartAreas(0).AxisY.ValueToPixelPosition(crtLogView.Series(srsNam).Points(mouseX).YValues(0)) - 4
pntX = crtLogView.ChartAreas(0).AxisX.ValueToPixelPosition(Val(mouseX)) - 4
'//Move circle to closest point
cir.Location = New Point(pntX, pntY)
'//Refresh the form to move the circle
'//This works, but takes 2+ seconds to take effect
'crtLogView.Invalidate()
'crtLogView.Update()
'//This does not work
'Me.Invalidate()
'Me.Update()
'//This works, but randomly makes other controls flash/flicker
Me.Refresh()
ErrTrap:
End Sub
In the comments, I offered to provide an example of using a Chart Annotation or a DataPoint Label as an alternative to custom painting a circle around the point under the mouse-cursor and have included that in the code below. However, I realized that a DataPoint Marker should provide the function the OP is seeking and is likely the proper solution. Therefore, that option is also included.
Annotations are chart level graphics where-as the DataPoint Label and DataPoint Marker are as the name implies tied to the individual DataPoints. Proper sizing of annotations can be involved as their size is specified as a percentage of the Chart Area dimensions. This example does not attempt to resize the annotation based on the current chart size.
The following code sample is for a WinForm. In VS, add a new Class to a WinForm project and replace the auto-generated code with this. The set this Form as the startup Form.
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports Charting = System.Windows.Forms.DataVisualization.Charting
Public Class ChartDemo : Inherits Form
Const yMultiplyer As Double = 100.0
Private rnd As Random
Friend WithEvents chart As System.Windows.Forms.DataVisualization.Charting.Chart
Friend WithEvents rbAnnotation As System.Windows.Forms.RadioButton
Friend WithEvents rbDataLabel As System.Windows.Forms.RadioButton
Friend WithEvents rbMarker As System.Windows.Forms.RadioButton
Private lastPoint As Charting.DataPoint
Private ellispeAnnotation As Charting.EllipseAnnotation
Public Sub New()
InitializeComponent()
rnd = New Random(0) ' use same basis for each run
SetupChart()
End Sub
Private Sub InitializeComponent()
Me.chart = New System.Windows.Forms.DataVisualization.Charting.Chart()
Me.rbAnnotation = New System.Windows.Forms.RadioButton()
Me.rbDataLabel = New System.Windows.Forms.RadioButton()
Me.rbMarker = New System.Windows.Forms.RadioButton()
CType(Me.chart, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
Me.chart.Anchor = AnchorStyles.Top Or
AnchorStyles.Bottom Or
AnchorStyles.Left Or
AnchorStyles.Right
Me.chart.Location = New Point(4, 50)
Me.chart.Size = New Size(600, 500)
Me.rbAnnotation.AutoSize = True
Me.rbAnnotation.Location = New Point(50, 10)
Me.rbAnnotation.TabIndex = 1
Me.rbAnnotation.Text = "Use Annotation"
Me.rbAnnotation.UseVisualStyleBackColor = True
Me.rbDataLabel.AutoSize = True
Me.rbDataLabel.Location = New Point(200, 10)
Me.rbDataLabel.TabIndex = 2
Me.rbDataLabel.Text = "Use Data Label"
Me.rbDataLabel.UseVisualStyleBackColor = True
Me.rbMarker.AutoSize = True
Me.rbMarker.Location = New Point(400, 10)
Me.rbMarker.TabIndex = 3
Me.rbMarker.Text = "Use Data Marker"
Me.rbMarker.UseVisualStyleBackColor = True
Me.rbMarker.Checked = True
Me.AutoScaleDimensions = New SizeF(96.0!, 96.0!)
Me.AutoScaleMode = AutoScaleMode.Dpi
Me.ClientSize = New Size(610, 555)
Me.Controls.AddRange({Me.rbDataLabel, Me.rbAnnotation, Me.rbMarker, Me.chart})
Me.Text = "Charting Demo"
CType(Me.chart, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Private Sub SetupChart()
chart.ChartAreas.Clear()
chart.Legends.Clear()
chart.Series.Clear()
chart.Annotations.Clear()
Dim area1 As New Charting.ChartArea("Area1")
chart.ChartAreas.Add(area1)
Dim ser As Charting.Series = chart.Series.Add("Series1")
ser.ChartArea = area1.Name
ser.ChartType = Charting.SeriesChartType.Line
' define defaults for point DataLabels
ser.LabelBorderColor = Color.Red
ser.LabelBorderWidth = 1
ser.LabelBackColor = Color.WhiteSmoke
ser.LabelForeColor = Color.Black
' define defaults for point DataMarkers
ser.MarkerSize = 10
ser.MarkerBorderWidth = 3
ser.MarkerBorderColor = Color.Red
ser.MarkerColor = Color.Transparent
' points for demo chart
For x As Double = -5.0 To 5.0
ser.Points.AddXY(x, rnd.NextDouble * yMultiplyer)
Next
ellispeAnnotation = CreateEllipseAnnotation()
ellispeAnnotation.Visible = False
chart.Annotations.Add(ellispeAnnotation)
End Sub
Private Sub chart_MouseLeave(sender As Object, e As EventArgs) Handles chart.MouseLeave
ellispeAnnotation.Visible = False
ClearLastPointDataLabel()
ClearLastPointMarker()
End Sub
Private Function CreateEllipseAnnotation() As Charting.EllipseAnnotation
Dim ret As New Charting.EllipseAnnotation()
ret.ForeColor = Color.Black
ret.Font = New Font("Arial", 10)
ret.LineWidth = 2
ret.Height = 7.5 ' % ChartArea height
ret.Width = 15 ' % ChartArea width
ret.BackColor = Color.PaleGoldenrod
ret.LineDashStyle = Charting.ChartDashStyle.Solid
Return ret
End Function
Private Sub chart_MouseMove(sender As Object, e As MouseEventArgs) Handles chart.MouseMove
Dim htr As Charting.HitTestResult = chart.HitTest(e.X, e.Y)
If htr.ChartElementType = Charting.ChartElementType.DataPoint Then
Dim pt As Charting.DataPoint = DirectCast(htr.Object, Charting.DataPoint)
If pt IsNot lastPoint Then
SetDataPointLabel(pt)
SetDataPointAnnotation(pt)
SetDataPointMarker(pt)
lastPoint = pt
End If
End If
End Sub
Private Sub SetDataPointAnnotation(pt As Charting.DataPoint)
If rbAnnotation.Checked Then
ellispeAnnotation.AnchorDataPoint = pt
ellispeAnnotation.Text = String.Format("{0:N2}, {1:N2}", pt.XValue, pt.YValues(0))
ellispeAnnotation.Visible = True
End If
End Sub
Private Sub SetDataPointLabel(pt As Charting.DataPoint)
ClearLastPointDataLabel()
If rbDataLabel.Checked Then
pt.Label = "#VALX{N2}, #VALY{N2}" ' case sensative, use uppercase for #VALX, #VALY
pt.IsValueShownAsLabel = True
End If
End Sub
Private Sub ClearLastPointDataLabel()
If lastPoint IsNot Nothing Then
lastPoint.Label = String.Empty
lastPoint.IsValueShownAsLabel = False
End If
End Sub
Private Sub SetDataPointMarker(pt As Charting.DataPoint)
ClearLastPointMarker()
If rbMarker.Checked Then pt.MarkerStyle = Charting.MarkerStyle.Circle
End Sub
Private Sub ClearLastPointMarker()
If lastPoint IsNot Nothing Then
lastPoint.MarkerStyle = Charting.MarkerStyle.None
End If
End Sub
Private Sub rbAnnotation_CheckedChanged(sender As Object, e As EventArgs) Handles rbAnnotation.CheckedChanged
If Not rbAnnotation.Checked Then
ellispeAnnotation.Visible = False
End If
End Sub
Private Sub rbDataLabel_CheckedChanged(sender As Object, e As EventArgs) Handles rbDataLabel.CheckedChanged
ClearLastPointDataLabel()
End Sub
Private Sub rbMarker_CheckedChanged(sender As Object, e As EventArgs) Handles rbMarker.CheckedChanged
ClearLastPointMarker()
End Sub
End Class

.NET (4.5) ListView - add/remove column bug(?)

I came across an awkward (that is, unexpected for me) behaviour of the .NET Listview. The issue may have been posted before. If so, apologies for the duplication: I didn't find it and would appreciate being pointed to "the" discussion.
Context: I'm using VisualStudio 2013, VB, target framework 4.5
Build a Form with a listview and a few buttons.
The ListView (Details-view) is initially configured with 1 column labeled "1" and 2 rows, each containing a "1".
Then there's a button to Add a column with header "A" (if it's not there yet) and for each listviewItem a subitem "a". Likewise for a column "B" with subitem "b".
Finally there are buttons to remove columns "A" and "B" respectively.
Now, if I do the following:
Add "A"
Delete "A"
Add "B"
I wind up with a column "B" (header) with column contents "a", i.e. those of the "removed" column "A".
Playing around you always find that upon removal of a column (listview.columns.removeBykey('key'), for instance) the subitems seem to persist "invisibly" and come to life if "any" columnHeader is added later on.
I'm not looking for help to solve a programming problem: there's an easy way around (whenever you remove a column, also remove explicitly the corresponding subitems from each listviewItem), but it puzzles me that this clumsy (in my view) approach would be necessary.
Maybe I don't properly use/configure the listview and should have known all this. Any comment is appreciated.
I copy the code below, to make it easier to verify for anyone interested.
(Designer code)
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.lvStd = New System.Windows.Forms.ListView()
Me.btnAdd_A = New System.Windows.Forms.Button()
Me.btnDel_A = New System.Windows.Forms.Button()
Me.btnAdd_B = New System.Windows.Forms.Button()
Me.btnDel_B = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'lvStd
'
Me.lvStd.Dock = System.Windows.Forms.DockStyle.Top
Me.lvStd.Location = New System.Drawing.Point(0, 0)
Me.lvStd.Name = "lvStd"
Me.lvStd.Size = New System.Drawing.Size(661, 207)
Me.lvStd.TabIndex = 0
Me.lvStd.UseCompatibleStateImageBehavior = False
Me.lvStd.View = System.Windows.Forms.View.Details
'
'btnAdd_A
'
Me.btnAdd_A.Location = New System.Drawing.Point(12, 235)
Me.btnAdd_A.Name = "btnAdd_A"
Me.btnAdd_A.Size = New System.Drawing.Size(75, 23)
Me.btnAdd_A.TabIndex = 1
Me.btnAdd_A.Text = "Add A"
Me.btnAdd_A.UseVisualStyleBackColor = True
'
'btnDel_A
'
Me.btnDel_A.Location = New System.Drawing.Point(12, 264)
Me.btnDel_A.Name = "btnDel_A"
Me.btnDel_A.Size = New System.Drawing.Size(75, 23)
Me.btnDel_A.TabIndex = 2
Me.btnDel_A.Text = "Del A"
Me.btnDel_A.UseVisualStyleBackColor = True
'
'btnAdd_B
'
Me.btnAdd_B.Location = New System.Drawing.Point(159, 235)
Me.btnAdd_B.Name = "btnAdd_B"
Me.btnAdd_B.Size = New System.Drawing.Size(75, 23)
Me.btnAdd_B.TabIndex = 3
Me.btnAdd_B.Text = "Add B"
Me.btnAdd_B.UseVisualStyleBackColor = True
'
'btnDel_B
'
Me.btnDel_B.Location = New System.Drawing.Point(159, 264)
Me.btnDel_B.Name = "btnDel_B"
Me.btnDel_B.Size = New System.Drawing.Size(75, 23)
Me.btnDel_B.TabIndex = 4
Me.btnDel_B.Text = "Del B"
Me.btnDel_B.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(661, 474)
Me.Controls.Add(Me.btnDel_B)
Me.Controls.Add(Me.btnAdd_B)
Me.Controls.Add(Me.btnDel_A)
Me.Controls.Add(Me.btnAdd_A)
Me.Controls.Add(Me.lvStd)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
Friend WithEvents lvStd As System.Windows.Forms.ListView
Friend WithEvents btnAdd_A As System.Windows.Forms.Button
Friend WithEvents btnDel_A As System.Windows.Forms.Button
Friend WithEvents btnAdd_B As System.Windows.Forms.Button
Friend WithEvents btnDel_B As System.Windows.Forms.Button
End Class
and the "Form" code:
Public Class Form1
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
If (Me.DesignMode) Then Return
Me.Configure()
Me.FillList()
End Sub
Private Sub Configure()
Me.lvStd.BeginUpdate()
Me.lvStd.Clear()
Me.lvStd.Columns.Add("1")
Me.lvStd.EndUpdate()
End Sub
Private Sub FillList()
Dim LI As ListViewItem
For k As Integer = 1 To 2
LI = Me.lvStd.Items.Add("1")
Next
End Sub
Private Sub btnAdd_A_Click(sender As Object, e As EventArgs) Handles btnAdd_A.Click
Dim col As ColumnHeader = Me.lvStd.Columns("A")
If (col IsNot Nothing) Then
Return
End If
Me.lvStd.Columns.Add("A", "A")
For Each LI As ListViewItem In Me.lvStd.Items
LI.SubItems.Add("a")
Next
End Sub
Private Sub btnDel_A_Click(sender As Object, e As EventArgs) Handles btnDel_A.Click
Dim col As ColumnHeader = Me.lvStd.Columns("A")
If (col IsNot Nothing) Then
Me.lvStd.Columns.Remove(col)
End If
End Sub
Private Sub btnAdd_B_Click(sender As Object, e As EventArgs) Handles btnAdd_B.Click
Dim col As ColumnHeader = Me.lvStd.Columns("B")
If (col IsNot Nothing) Then
Return
End If
Me.lvStd.Columns.Add("B", "B")
For Each LI As ListViewItem In Me.lvStd.Items
LI.SubItems.Add("b")
Next
End Sub
Private Sub btnDel_B_Click(sender As Object, e As EventArgs) Handles btnDel_B.Click
Dim col As ColumnHeader = Me.lvStd.Columns("B")
If (col IsNot Nothing) Then
Me.lvStd.Columns.Remove(col)
End If
End Sub
End Class

How can I respond to events raised by programmatically created UI elements?

I'm creating a board game for a piece of coursework. For the board, I'm using some nested For loops running through a 2D array to generate a "Space" object at each square.
The Space object contains a picturebox and some data about that space.
How can I handle events caused by clicking on the generated picturebox without having to hard-code it for each space?
I noticed this question seems to address this, but it's in C# and I couldn't translate it to VB.Net.
Edit:
This is how the board is generated
Dim board(23, 24) As Space
Private Sub GenerateBoard()
Dim spaceSize As New Size(30, 30)
Dim spaceLocation As New Point
Dim validity As Boolean
For Y = 0 To 24
For X = 0 To 23
spaceLocation.X = 6 + (31 * X)
spaceLocation.Y = 6 + (31 * Y)
If validSpaces(Y).Contains(X + 1) Then
validity = True
Else
validity = False
End If
board(X, Y) = New Space(validity, spaceSize, spaceLocation)
Me.Controls.Add(board(X, Y).imageBox)
board(X, Y).imageBox.BackColor = Color.Transparent
board(X, Y).imageBox.BringToFront()
Next
Next
End Sub
Space Class:
Public Class Space
Dim _active As Boolean
Dim _imageBox As PictureBox
Public Sub New(ByVal activeInput As Boolean, ByVal size As Size, ByVal location As Point)
_active = activeInput
_imageBox = New PictureBox
With _imageBox
.Size = size
.Location = location
.Visible = False
End With
End Sub
Property active As Boolean
Get
Return _active
End Get
Set(value As Boolean)
_active = value
End Set
End Property
Property imageBox As PictureBox
Get
Return _imageBox
End Get
Set(value As PictureBox)
_imageBox = value
End Set
End Property
Public Sub highlight()
With _imageBox
.Image = My.Resources.Highlighted_slab
.Visible = True
End With
End Sub
End Class
First all controls created by designer(textbox, label...) a generated by code too, but VisualStudio write this for you. If you open Designer file(yourForm.Designer.vb), then you can see all code how to generate a controls.
If you want a create event handler for your pictureBox , then:
//Initialize control
Private WithEvents _imageBox as PictureBox
Then create a event handler method:
Private Sub imageBox_Click(sender as Object, e as EventArgs)
//Your code
End Sub
Then in VB.NET you can assign a Event handler to the Event in two ways
first: In class constructor after you created a pictureBox( New PictureBox()) add
AddHandler Me._imageBox, AddressOf Me.imageBox_Click
second: On line we you created a event handler add next:
Private Sub imageBox_Click(sender as Object, e as EventArgs) Handles _imageBox.Click
//Your code
End Sub
And remember add your pictureBox to form controls YourForm.Controls.Add(spaceInstance.ImageBox)