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

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

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

.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

VB, Pass variables to a control

I have been tasked with creating a cinema booking system in VB.net by my teacher. So far I have created 50 CheckBoxes and I am trying to rename them all to seat (number). I have this code in my Form1.load but it is not working because it is a type and not an expression. I tried using a variable for this but it did not work.
Here is my code:
For count As Integer = 1 To 54 Step 1
CheckBox(count).text = "Seat " & count
Next
Please help, and or recommend me another way to accomplish this.
set the name of the checkbox when you create it. To find out how to create a checkbox programmatically add a checkbox to a form then look at .designer.vb
dim cb as new checkbox
cb.name = "1"
cb.text = "Seat 1"
you need to also add the location and other properties
If you have already created your textboxes with names like 1, 2 then iterate through and get the numbers like this: If you call them CB_1 then cut the CB_ off before looking for the number.
dim cbNumber as int16
For Each c As Control In myContainer.Controls
If c.GetType() Is GetType(CheckBox) Then
cbnumber = cint(c.name)
c.text = "Seat" & cbnumber
End If
Next
Well, here's my approach. In order to test just drop on a FlowLayoutPanel, a Button, and a NumericUpDownonto the form.
Option Strict On
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For count As Integer = 1 To 54 Step 1
' Make a new CheckBox
Dim chkBox As New CheckBox()
' Setup the Checkbox
With chkBox
.Tag = count.ToString
.Name = CStr("seatCheckBox" & count.ToString)
.Text = String.Format("Seat {0}", count)
.ThreeState = False
.Checked = False
End With
' add an event listener for the checkbox checkstate changed event
AddHandler chkBox.CheckStateChanged, AddressOf Me.CheckBox_CheckStateChanged
' Add the checkbox to the control
Me.FlowLayoutPanel1.Controls.Add(chkBox)
' Keep the user from picking something that doesn't exist
Me.NumericUpDown1.Maximum = CDec(count)
Next
' Add and event listener for the find button click event
AddHandler Button1.Click, AddressOf Me.FindButton_Clicked
End Sub
' Find the checkbox in the form and return it
Private Function GetCheckBox(ByVal seatNumber As Integer) As CheckBox
Dim chkbox As CheckBox
' Try to find the Checkbox
Try
chkbox = TryCast(Me.Controls.Find(CStr("seatCheckBox" & seatNumber.ToString), True).First, CheckBox)
Catch ex As Exception
chkbox = Nothing
End Try
'Check if the trycast worked
If IsNothing(chkbox) Then
Throw New ArgumentOutOfRangeException("seatNumber", "The seat number to be searched for was not found")
Else
Return chkbox
End If
End Function
' Handle the Chekbox checkState event.
Private Sub CheckBox_CheckStateChanged(sender As Object, e As EventArgs)
' Convert to Checkbox
Dim chkBox As CheckBox = DirectCast(sender, CheckBox)
' Simple result string
Dim resultstring As String = CStr("Seat Number {0} is now {1}.")
' Set the values
Select Case chkBox.Checked
Case True
resultstring = String.Format(resultstring, chkBox.Tag, "taken")
Case False
resultstring = String.Format(resultstring, chkBox.Tag, "available")
End Select
' Display it
MsgBox(resultstring)
End Sub
Private Sub FindButton_Clicked(sender As Object, e As EventArgs)
Try
' Get the checkbox and return it's name
MsgBox(GetCheckBox(CInt(Me.NumericUpDown1.Value)).Name.ToString)
Catch ex As Exception
' Display the error
MsgBox(ex.Message)
End Try
End Sub
End Class

Why does binding to a set of Winforms cascading combo boxes cause the root combo box to not set its value properly?

I have a Windows form with two combo boxes. The SelectedValue property of each combo box is data bound to a property on a simple DTO. The options for each combo box are drawn from a list of model objects. I only require the controls on the form to update the DTO; I have no need to modify any of the DTO's properties programmatically and see the corresponding control being updated - i.e., I only need one-way (control -> source) data binding to work.
When the user changes the value of the first combo box, the options for the second combo box will change completely. However, I have run into two issues with this setup that I cannot figure out why they occur or how to solve them:
Whenever the first combo box is changed, an NRE is generated and swallowed by the data binding framework (I can see it thrown in the Immediate Window of the Visual Studio IDE), which tips me off that something isn't set up correctly. Changing the second combo box or any other unrelated, data bound control (combo box or otherwise) does not generate an NRE.
Also whenever the first combo box is changed, after generating the NRE mentioned above, the second combo box loads successfully, but the first combo box's selected index resets to -1. I suspect this is because the data binding's "push" event fires to update the controls, and for some reason, the value of my DTO's property backing the first combo box gets reset to NULL / Nothing.
Does anyone have any idea why these things occur? I mocked up my problem, which exhibits the two issues above. I also added a third combo box that has nothing to do with either of the first two, just as a sanity check to show that a combo box without any dependency on another combo box works fine.
This code replicates the issues - paste as the code for the default Form1 class of a Visual Basic Windows Forms project (3.5 Framework).
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Windows.Forms
Public 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.cboA = New System.Windows.Forms.ComboBox()
Me.cboB = New System.Windows.Forms.ComboBox()
Me.cboC = New System.Windows.Forms.ComboBox()
Me.SuspendLayout()
'
'cboA
'
Me.cboA.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.cboA.FormattingEnabled = True
Me.cboA.Location = New System.Drawing.Point(120, 25)
Me.cboA.Name = "cboA"
Me.cboA.Size = New System.Drawing.Size(121, 21)
Me.cboA.TabIndex = 0
'
'cboB
'
Me.cboB.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.cboB.FormattingEnabled = True
Me.cboB.Location = New System.Drawing.Point(120, 77)
Me.cboB.Name = "cboB"
Me.cboB.Size = New System.Drawing.Size(121, 21)
Me.cboB.TabIndex = 1
'
'cboC
'
Me.cboC.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
Me.cboC.FormattingEnabled = True
Me.cboC.Location = New System.Drawing.Point(120, 132)
Me.cboC.Name = "cboC"
Me.cboC.Size = New System.Drawing.Size(121, 21)
Me.cboC.TabIndex = 2
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 262)
Me.Controls.Add(Me.cboC)
Me.Controls.Add(Me.cboB)
Me.Controls.Add(Me.cboA)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
Friend WithEvents cboA As System.Windows.Forms.ComboBox
Friend WithEvents cboB As System.Windows.Forms.ComboBox
Friend WithEvents cboC As System.Windows.Forms.ComboBox
Private _DataObject As MyDataObject
Private _IsInitialized As Boolean = False
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
_DataObject = New MyDataObject()
BindControls()
End Sub
Private Sub BindControls()
LoadComboA(cboA)
Dim cmbABinding As New Binding("SelectedValue", _DataObject, "ValueA", True, DataSourceUpdateMode.OnPropertyChanged)
cboA.DataBindings.Add(cmbABinding)
Dim cmbBBinding As New Binding("SelectedValue", _DataObject, "ValueB", True, DataSourceUpdateMode.OnPropertyChanged)
cboB.DataBindings.Add(cmbBBinding)
LoadComboC(cboC)
Dim cmbCBinding As New Binding("SelectedValue", _DataObject, "ValueC", True, DataSourceUpdateMode.OnPropertyChanged)
cboC.DataBindings.Add(cmbCBinding)
End Sub
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
_IsInitialized = True
cboA.SelectedIndex = 0
cboC.SelectedIndex = 0
End Sub
Private Sub ComboA_SelectedValueChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cboA.SelectedValueChanged
If _IsInitialized Then
LoadComboB(cboB, cboA.SelectedValue.ToString())
cboB.SelectedIndex = 0
End If
End Sub
Private Sub LoadComboA(ByVal cmbBox As ComboBox)
Dim someData As New Dictionary(Of String, String)()
someData.Add("Value1", "Text 1")
someData.Add("Value2", "Text 2")
someData.Add("Value3", "Text 3")
cmbBox.DataSource = someData.ToList()
cmbBox.DisplayMember = "Value"
cmbBox.ValueMember = "Key"
End Sub
Private Sub LoadComboB(ByVal cmbBox As ComboBox, ByVal selector As String)
Dim someSubData As New Dictionary(Of String, String)()
Select Case selector
Case "Value1"
someSubData.Add("SubValue1", "Value1 - Sub Text 1")
someSubData.Add("SubValue2", "Value1 - Sub Text 2")
someSubData.Add("SubValue3", "Value1 - Sub Text 3")
Case "Value2"
someSubData.Add("SubValue4", "Value2 - Sub Text 4")
someSubData.Add("SubValue5", "Value2 - Sub Text 5")
someSubData.Add("SubValue6", "Value2 - Sub Text 6")
Case "Value3"
someSubData.Add("SubValue7", "Value3 - Sub Text 7")
someSubData.Add("SubValue8", "Value3 - Sub Text 8")
someSubData.Add("SubValue9", "Value3 - Sub Text 9")
End Select
cmbBox.DataSource = someSubData.ToList()
cmbBox.DisplayMember = "Value"
cmbBox.ValueMember = "Key"
End Sub
Private Sub LoadComboC(ByVal cmbBox As ComboBox)
Dim someData As New Dictionary(Of String, String)()
someData.Add("Value100", "Text 100")
someData.Add("Value101", "Text 101")
cmbBox.DataSource = someData.ToList()
cmbBox.DisplayMember = "Value"
cmbBox.ValueMember = "Key"
End Sub
End Class
Public Class MyDataObject ' DTO class
Private _ValueA As String
Public Property ValueA() As String
Get
Return _ValueA
End Get
Set(ByVal value As String)
_ValueA = value
End Set
End Property
Private _ValueB As String
Public Property ValueB() As String
Get
Return _ValueB
End Get
Set(ByVal value As String)
_ValueB = value
End Set
End Property
Private _ValueC As String
Public Property ValueC() As String
Get
Return _ValueC
End Get
Set(ByVal value As String)
_ValueC = value
End Set
End Property
End Class
DataBinding can be difficult to debug when it misbehaves. There are two things going wrong here, enough to make it hard to diagnose. The first thing you didn't count on is that the SelectedValueChanged event fires before the currency manager updates the bound object. That normally isn't a problem but your event handler has a side-effect. The next thing you didn't count on is that changing one property of the bound object causes the binding of all other properties to be updated as well.
And there's the rub, _DataObject.ValueA is still Nothing when you update combo B. Which updates _DataObject.ValueB. So the currency manager updates combo A again, trying to make it match the value of Nothing in property ValueA. Which is what also produced the NullReferenceException.
One possible fix is to delay the side-effect in your SelectedValueChanged event handler and postpone it until the currency manager updated the bound object. Than can be cleanly done by using Control.BeginInvoke(), the target runs when the UI thread goes idle again. This fixed your problem:
Private Sub ComboA_SelectedValueChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cboA.SelectedValueChanged
If _IsInitialized Then Me.BeginInvoke(New MethodInvoker(AddressOf LoadB))
End Sub
Private Sub LoadB()
LoadComboB(cboB, cboA.SelectedValue.ToString())
cboB.SelectedIndex = 0
End Sub
There's probably a cleaner fix, updating _DataObject instead of trying to update the combobox. But you made that a bit difficult by using a Dictionary, I didn't pursue it.

Programmatically add controls to form

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.