How to exclude some controls in a FOR EACH CONTROL Loop? - vb.net

So I have a neat little function that I use to make sure only one checkbox can be checked inside a GroupBox.
This is what it looks like...
Private Sub ToggleCheckBoxOnEntry(sender As Object, e As EventArgs)
'This handles mutually exclusivity for the check boxes so that only one is ever allowed to be checked
Static CurrentlySelectedbox As CheckBox
If CType(sender, CheckBox).Checked Then
CurrentlySelectedbox = sender
End If
For Each cntrl As CheckBox In gbxReports.Controls
If cntrl.Checked AndAlso cntrl.Name <> CurrentlySelectedbox.Name Then
cntrl.Checked = False
End If
Next
End Sub
And for each checkBox_CheckChanged I include this line...
Private Sub chkReports_CheckedChanged(sender As Object, e As EventArgs)
ToggleCheckBoxOnEntry(sender, e)
End Sub
So I have this great looking groupbox with about 10 reports and it works great. The problem comes when I try to include a combobox for one of the extracts where I let the user select something from a drop down and use it as a parameter. I do not want to include it outside of the GroupBox (unless there's no way to fix my issue), however, if I include it inside of it I get an error..
Unable to cast object of type 'System.Windows.Forms.ComboBox' to type
'System.Windows.Forms.CheckBox'.
Is there a way for me to exclude some controls from the loop such as
For each cntrl as CheckBox in gbxReports.controls // except comboboxes/ or only checkboxes??
The only controls that I would potentially have in the group box are checkboxes and comboboxes.

Why don't you be specific to what you want to loop rather than the mix of Controls:
For Each chexkb As Checkbox In Controls.OfType(Of Checkbox)()
'do the loop just on the check boxes
Next

Related

Triggering an event in VB based on a change in a control in a group box

I have a group box with multiple Checkboxes(food item) and each one has a corresponding NumericUpDown control(quantity). For context, it is for a project based on a restaurant menu. I want to hide a button called btnSave whenever either a checkbox is unchecked or the quantity (NumericUpDown) is changed. I currently have btnSave.Hide under the CheckBox1_CheckedChanged and NumericUpDown1_CheckedChanged SubProcedures but I want to know if there's a way to do this when anything within this group box is changed instead of putting the code under each SubProcedure. Thanks
I think you meant .ValueChanged for the NumericUpDown control. (There is no .CheckedChanged) Although this doesn't matter much in this case, this is a good pattern for future reference. Instead of calling an event call a Sub from your events.
When you have several controls responding to a single Event handler, you can find out which control triggered the event by checking the sender parameter. Since, as you can see, sender is an Object you will have to cast it to the appropriate type to get the properties of a CheckBox.
Private Sub HideSaveButton()
btnSave.Hide
End Sub
Private Sub CheckBoxInGroupBox_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged
HideSaveButton()
Dim WhichCheckBox As CheckBox = DirectCast(sender, CheckBox)
Select Case WhichCheckBox.Name
Case "CheckBox1"
MessageBox.Show("CheckBox1 has changed")
Case "CheckBox2"
MessageBox.Show("CheckBox2 has changed")
End Select
End Sub
Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
HideSaveButton()
End Sub

How to remove the most recently added control?

I Intended to display an PictureBox in my form when the mouse hovered over another control. I then wanted to use a separate event for when the mouse left the control. This event would remove the displayed PictureBox from controls. However, because my events are private subs, I can't directly access the name of the control in the latter event. A solution to this would be a method that removes the most recently added control. If no such method exists, or there is an alternative way of approaching this problem, any help would be appreciated.
I tried simply using Controls.Remove(), but this requires a parameter. The name of the control as a string did not work either, as the parameter must be a control itself.
Private Sub Tile_MouseEnter(Sender As Object, e As EventArgs)
Dim CloseUpPic As New PictureBox With {Properties}
CloseUpPic.Image = Sender.Image
Controls.Add(CloseUpPic)
Refresh()
End Sub
Private Sub Tile_MouseLeave(Sender As Object, e As EventArgs)
Me.Controls.Remove()
End Sub
The program won't compile due to .Remove() needing a parameter
I expected for the Control to be created and displayed when the mouse entered the tile, and to cease to exist when the mouse left the tile.
For future reference, controls have Tag property which allows you to store whatever you like. In this case, you can store a reference to the newly created PictureBox. Furthermore, the "Sender" parameter tells you which control was the source of the event. You can cast sender to a control, then store the reference. Then, in the leave event, you can cast sender to a control, cast the .Tag to a control, and finally remove it:
Private Sub Tile_MouseEnter(Sender As Object, e As EventArgs)
Dim ctl As Control = DirectCast(Sender, Control)
Dim CloseUpPic As New PictureBox With {Properties}
CloseUpPic.Image = Sender.Image
Controls.Add(CloseUpPic)
ctl.Tag = CloseUpPic
Refresh()
End Sub
Private Sub Tile_MouseLeave(Sender As Object, e As EventArgs)
Dim ctl As Control = DirectCast(Sender, Control)
Dim ctlToRemove As Control = DirectCast(ctl.Tag, Control)
Me.Controls.Remove(ctlToRemove)
End Sub
I ended up using the following code to solve my issue:
For Each Closeup In Controls.OfType(Of CloseUp)
Controls.Remove(Closeup)
Next
I created a new class of my own called Closeup, that inherits PictureBox. I then looped through each Closeup in controls (There was only one but this code works for multiple controls), and removed them.

Disable Button If Combox Input Deleted

In my project, I have a few textbox inputs, and some combo boxes with maybe 2 indexed items on a form. There's a button I'm disabling on load if no input is supplied to both textbox inputs, and it works great even if I delete out any text. However, I'm having issues with forcing the combo box to behave in the same manner. This work however:
Private Sub cboPickShirts_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboPickShirts.SelectedIndexChanged
InputCheck_3 = True
If cboPickShirts.SelectedIndex < 0 Then
InputCheck_3 = False
End If
If InputCheck_3 = False Then
btnInputResult.Enabled = False
ElseIf InputCheck_3 = True Then
btnInputResult.Enabled = True
End If
End Sub
I have InputCheck_3 set up as a global variable in a Public Module. On form load, I'm disabling my button and it doesn't enable until I select one of the indexed items. My struggle to get the button disable again if any combo box text is entered and deleted out, leaving it null or empty. Any thoughts on what I'm missing or what I can add to get results? I guess I need a variable or event to notice the change (entering & deletion of text).
The problem you are having is that your SelectedIndexChanged event is not being triggered when you remove the selected item from your ComboBox. I would use the TextChanged event of your TextBox's and ComboBox and give it a common handler and check it that way. Something like this
Private Sub TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, cboPickShirts.TextChanged
EnableCheck()
End Sub
Private Sub EnableCheck()
btnInputResult.Enabled = (String.IsNullOrEmpty(TextBox1.Text) And String.IsNullOrEmpty(TextBox2.Text) And ComboBox1.SelectedIndex = -1)
End Sub
You can also check that the comboBox is NullorEmpty the same way as the textbox's. As it stands right now the combobox will be enabled when the text no longer matches a selection.
One line code
btnInputResult.Enabled = If((cboPickShirts.SelectedIndex<0),False, True)

Check which checkbox is checked with loop

What would be the syntax to check inside a visual basic form panel for checkboxes and find which are checked? I understand how I Could use a for loop and an if statement, but I'm confused as to the syntax to check for each checkbox. for example:
Dim i As Integer
For i = 1 To 10
'Here is where my code would go.
'I could have ten checkboxes named in sequence (cb1, cb2, etc),
'but how could I put i inside the name to test each checkbox?
Next
You need to loop through the Controls collection of the control that has the Checkbox's added to it. Each Control object has a Controls collection. I would prefer a For Each loop in this scenario so I get the Control right away without having to get it using the Controls index If your CheckBoxes are added to the Panel directly, the easiest way to do it would be..
For Each ctrl As var In panel.Controls
If TypeOf ctrl Is CheckBox AndAlso DirectCast(ctrl, CheckBox).IsChecked Then
'Do Something
End If
Next
I'm not very familiar with the VB.Net syntax, but in psudo-code:
ForEach CheckBox in ControlContainer
DoSomething
Next
If you have all of your CheckBox controls in a single container - e.g. a Panel - then the above code would iterate each control that is a CheckBox.
Try this :
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If CheckBoxList1.Text = "" Then
do/display something
Exit Sub
Else
For Each item As ListItem In CheckBoxList1.Items
If item.Selected Then
do/display something
End If
Next
End If
End Sub

Rookie With Events; Causing Action after hitting F5

So yes I'm very new to creating my own custom events. I can do the basics when I put controls on a form but this one is a little more complex. I have an application that reads in a .TSV and populates a form with controls based on the number of objects it "reads." So for instance: I have a file that contains 10 people objects and my code populates a form with controls for each person. Easy stuff!
Now lets say I have a ComboBox with the items: "Alive", "Deceased", "Unborn". Right next to this I have a textbox for age. Now originally, this textbox is not enabled because the default value for the ComboBox is "Unborn". But lets say when the user selects "Alive", I want that textbox to become enabled so an age can be entered.
Obviously from me asking this and the title of this question, I don't know how to go about doing this. I don't really understand events and I learn by example but the MSDN examples don't quite cut it.
Any help (especially an awesome Step-by-Step guide) would be greatly appreciated.
From what I gather from the comments, you want to add events to a form object that is created at runtime. Use the AddHandler command to the object. Something to the effect of:
AddHandler NameOfFormObject.TypeOfAction, AddressOf HowToHandle
Private Sub HowToHandle(ByVal sender as System.Object, ByVal e As System.EventArgs)
DropDownMenu.enabled = True
End Sub
Doing it this way, you will be able to modify the events of an object created at runtime. In your case, it sounds like you'll want to use the action that Josaph recommended, and end up incorporating both solutions offered, like so
AddHandler ComboBox1.SelectedIndexChanged, AddressOf HowToHandle
Private Sub HowToHandle(ByVal sender as System.Object, ByVal e As System.EventArgs)
If DirectCast(sender, ComboBox).SelectedIndex = 0 'Alive
DirectCast(DirectCast(sender, ComboBox).Tag, TextBox).enabled = True
Else
DirectCast(DirectCast(sender, ComboBox).Tag, TextBox).enabled = False
End If
End Sub
You will want to use the ComboBox_SelectedIndexChanged() event to capture that the combo box item has been changed. At that point, you will need to check to see which combo box item has been selected and make a decision as to whether the TextBox should be enabled or not. Here is an example. Note: This example assumes that "Alive" is the first item in your combobox at the 0 index.
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
If ComboBox1.SelectedIndex = 0 Then 'Alive
TextBox1.Enabled = True
Else
TextBox1.Enabled = False
End If
End Sub
Dynamically generate the combobox and add handler.
Dim cmb as New ComboBox
AddHandler cmb.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
Me.Controls.Add(cmb)
I suppose that as you will have 10 comboboxes... in the same way you'll have 10 textboxes.
In that case... once you attach and handle the event as AndyPerfect and Joseph... in that method you will need code something to know which of the Textboxs you need to enable/disable.
First you need to know which combobox triggered the event: this is done using the "sender" parameter. ctype(sender, Combobox) to access the methods and properties of the ComboBox.
Once you know which combo, you need to activate/deactivate the correct textbox.
To accomplish this, you'll need to add a reference to the TextBox in the "TAG" property of the Combobox at the moment of creating it.
Dim txt as new TextBox
Dim cmb as new ComboBox
cmb.Tag = txt
Then... you simple use:
ctype(ctype(sender, Combobox).Tag, TextBox).Enable = true
Here is how I ended up writing it in the end. I appreciate all the help! Thank you!
If DirectCast(sender, ComboBox).SelectedIndex = 2 Then
DirectCast(Me.Controls.Item(DirectCast(sender, ComboBox).Tag), TextBox).Enabled = True
Else
DirectCast(Me.Controls.Item(DirectCast(sender, ComboBox).Tag), TextBox).Enabled = False
End If