Only the checkbox have Backcolor "RED" will be enabled true - vb.net-2010

I have a 3 checkboxes and my checkboxes is .enabled = False. I want when I click a button, only the checkboxes with a red BackColor will be set to enabled = True
How to do that?

You can do this with something like this :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each CB As CheckBox In Controls.OfType(Of CheckBox)
If CB.BackColor = Color.Red Then
CB.Enabled = True
End If
Next
End Sub
This will loop through all the controls in the form and if the control is of type CheckBox then it will check its BackColor property and then change to fit your needs, hope you understood the code, because Stackoverflow is not about giving out ready/full codes, if you didn't understand anything in the code, please feel free to ask about it.
Hope that helped :)

Related

Set focus on ListView

I've just starting using ListView control in VS2019.
I'm sure there's a simple solution, but I can't figure out how to set the focus on the control like I can with most other controls.
I want the ListView to be focused when the form loads, and then I want to programmatically focus an item in the ListView.
I know how to focus a particular item using ListView1.FocusedItem.Index, but I can't focus the actual control.
I have tried both ListView1.Select() and ListView1.Focus() but these seem to do nothing.
What am I missing!?
Thanks
EDIT
As pointed out below, the control is actually focused using ListView1.Select() it's just not focused in the same way I'd expect, for example, a ListBox to be focused - ie, with a particular item in the list highlighted.
How would you best focus the ListView and highlight a particular item?
I have tried this in a command button on the form, but it doesn't do anything. Though it works correctly AFTER I click an Item in the ListView.
ListView1.Select()
If (ListView1.SelectedItems.Count > 0) Then
ListView1.Items(4).Selected = True
End If
listview1.focus()
For I as Integer = 0 to ListView1.Items.Count - 1
If ListView1.Items(i).Text = "youritemtexthere" then
ListView1.Items(i).Selected = true
End If
End For
or if you have the index but not a known text just do:
listview1.focus()
ListView1.Items(index).Selected = true
ListView1.Select works, you probably just don't see that the ListView has focus. You can verify this by checking the GotFocus and LostFocus events on the ListView:
Private Sub ListView1_GotFocus(sender As Object, e As EventArgs) Handles ListView1.GotFocus
Me.Text = "Got Focus"
End Sub
Private Sub ListView1_LostFocus(sender As Object, e As EventArgs) Handles ListView1.LostFocus
Me.Text = "Lost Focus"
End Sub
This simply updates your Form's title to "Got Focus" or "Lost Focus". You can force the focus in your Form Load event:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
ListView1.Items.Add("a")
ListView1.Items.Add("b")
ListView1.Items.Add("c")
ListView1.Select()
End Sub

Unchecking a Previously Selected Checkbox when another checkbox is checked

My form has 3 checkboxes available to the User. When the User mistakenly checks one Checkbox and realizes the mistake then checks the correct Checkbox, I need the incorrect checked Checkbox to Uncheck automatically.
I have diligently searched the web but have been unable to find any sites that offer answers for VB 2010. Mostly they provide solutions for HTML, Excel, Java, etc.
Private Sub BeginnerForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
StartupForm.Close()
If Me.CheckBox1.Checked Then
Me.CheckBox2.Checked = False
Me.CheckBox3.Checked = False
ElseIf Me.CheckBox2.Checked Then
Me.CheckBox1.Checked = False
Me.CheckBox3.Checked = False
ElseIf Me.CheckBox3.Checked Then
Me.CheckBox1.Checked = False
Me.CheckBox2.Checked = False
End If
End Sub
The results of this code is that it does not uncheck the Checkbox(es). I get no error code, it just does not UNCHECK the Checkbox(es). I have also tried other code entries such as Me.Checkbox2.CheckState.Checked = CheckState.Unchecked to no avail. Trusting you can steer me in the right direction to UNCHECK a Checkbox when another Checkbox is CHECKED.
First of all, as mentioned by daShier, you place the code in the wrong event handler. Since you want to uncheck the other checkboxes when one is being checked, you must put the code in the either Click or CheckChanged event handler of all your checkboxes that take part in this selection logic. I would suggest to put in the CheckChanged event since it will be triggered even when your checkbox check state is changed through code in addition to through mouse or keyboard actions.
' declare variable to keep track of the previous selected checkbox
Dim prevSelectedCheckBox As CheckBox
' all checkboxes to be controlled are handled by this event handler
Private Sub CheckBoxes_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, CheckBox3.CheckedChanged
' first, identify which checkbox is triggering this event
Dim this As CheckBox = DirectCast(sender, CheckBox)
' if this event is triggered when this checkbox is being checked
If this.Checked Then
' if there is a checkbox previously selected/checked
If prevSelectedCheckBox IsNot Nothing Then
prevSelectedCheckBox.Checked = False ' uncheck it
End If
' now this checkbox is currently the checked one, save it to the variable
prevSelectedCheckBox = this
' if event is triggered by unchecking this checkbox, and this checkbox is the previously checked checkbox
ElseIf this.Equals(prevSelectedCheckBox) Then
' clear the variable since now, there is no checkbox is being checked
prevSelectedCheckBox = Nothing
End If
End Sub
However, if user can only select one option among the options represented by the checkboxes, you better use radio button instead.
It appears you are processing the code in the wrong handler. You are changing things when closing the form. Try moving the code to the CheckBoxx.CheckedChanged handlers:
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
CheckBox2.Checked = False
CheckBox3.Checked = False
End Sub
UPDATE: I realize that your original code that I copied and pasted would cause a loop since the changing of the checkbox states would trigger the other routines. It's not necessary since you only need clear the other boxes if the box your user is clicking is checked.
You will need to add similar code to CheckBox2.CheckedChanged and CheckBox3.CheckedChanged as well.
Also, I should point out that you are trying to make checkboxes act as radio buttons. Why not replace the checkbox controls with radio buttons instead so that VB handles the one-only behavior for you?

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)

How to disable/enable combo box using a checkbox in VB.NET?

I have a dropdown combo box containing a few options for the user. I also have a checkbox that allows the user to use a default value (also in the dropdown combo box). When the user checks the check box, I want to have the combo box become disabled.
My code should work in theory, but it doesn't.
Private Sub chkboxUseDefault_Click(sender As Object, e As System.EventArgs) Handles chkboxUseDefault.Click
If chkboxUseDefault.CheckState.Equals(1) Then
cmbSelectOptions.Enabled = False
Else
cmbSelectOptions.Enabled = True
End If
Can someone point out what my (potentially obvious) error is?
You're problem is with: If chkboxUseDefault.CheckState.Equals(1) Then
You're code will have the desired outcome if you change that line to:
If chkboxUseDefault.CheckState = CheckState.Checked Then
Or perhaps even more succinctly:
If chkboxUseDefault.Checked Then
Although, I am inclined to also agree with OneFineDay in that I find the chkboxUseDefault.CheckedChanged a more appropriate event for this endeavor, and his proposed code is more readable IMO, I felt you might like to know how to make as small a change as possible and achieve the desired result.
Lets simplify the check:
Private Sub chkboxUseDefault_CheckedChanged(sender As Object, e As System.EventArgs) Handles chkboxUseDefault.CheckedChanged
cmbSelectOptions.Enabled = Not chkboxUseDefault.Checked
End Sub
Private Sub chkboxUseDefault_CheckedChanged(sender As Object, e As EventArgs) Handles chkboxUseDefault.CheckedChanged
If chkboxUseDefault.Checked = True Then
cmbSelectOptions.Enabled = False
Else
cmbSelectOptions.Enabled = True
End If
End Sub
This enables and disables your combobox.

datagridview editingcontrolshowing checkboxcell

I want to change the backcolor of the edited cell when I have a checkboxcolumn in my datagridview.
For textboxcolumns the backcolor is easy to set but for some reason I don't catch this event on the checkboxcolumn when it goes into edit mode.
Is there something different I need to do to handle this scenario? I am guessing comboboxcolumns are similarly different?
Based on thetimmer's suggested answer I added the following code to the cellbeginedit event of my datagridview but it has no effect on the coloring.
If e.ColumnIndex = datagridview1.Columns("checkboxcolumn").Index Then
Dim c As DataGridViewCheckBoxCell = datagridview1.CurrentCell
c.Style.BackColor = Color.Red
End If
This works once you've left the cell
Private Sub DataGridView1_CellBeginEdit(sender As System.Object, e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
Me.DataGridView1.CurrentCell.Style.BackColor = Color.Yellow
End Sub