How to disable/enable combo box using a checkbox in VB.NET? - 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.

Related

how to set minimum limit of a textbox vb.net

I need to set a minimum length for a password that can be used in a textbox and then set a label which will say if it meets the minimum number of characters required. I know how to set the limit of characters, what I can't do is the part where it will show in a label as soon as I leave the textbox. I was thinking I need to use an event, like maybe Leave or LostFocus, but it's not working. Please help :(
Ok, There are plenty of ways to do what you want to achieve. I personally like to a separate subroutine; if you need to change one thing, you wont have to edit every single event that has the same codeFrom what I can understand, something like this should help get you on your way. Basically, we just setup a subroutine that will check to see if textbox1.text's length is more than five and we trigger the subroutine by using events such as a button click of if the textbox is clicked off.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ''save button
checkPassword(TextBox1.Text)
End Sub
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
checkPassword(TextBox1.Text)
End Sub
Private Sub checkPassword(password As String)
If Not password.Length > 5 Then
Label1.Text = "The password must be more than 5 charcharacters"
TextBox1.Clear()
Else
Label1.Text = "Password accepted"
End If
End Sub

How to make Datagridview Editable and Change it to Number Format?

Good Morning.
I have a Datagridview in a Form and it is connected in Database and it looks like this.
I have no problem with this part.
My question here is like this. How can I make the Fourth Column Editable? I mean I can edit it by clicking this property
and now the output will be like this.
Now here is the real question, I will ask a question based on the flow that my system will do.
1.The 4th Column are the column that will become editable and the rest will be locked or uneditable
2.Lets say I will put 48 how can I make it 48.00 when i leave the cell? That kind of format with .00 at the end.
3.Unable to input Letters on the 4th column.
TYSM for future help
Set the ReadOnly property to True for any column that you don't
want the user to be able to edit.
Set the DefaultCellStyle.Format property of the column to "n2" or "f2".
I'd probably advise against that because the fact that you want to include a decimal point makes it more complex. If you're determined to go ahead then you should research how to allow only numeric input in a regular TextBox, because this will work exactly the same way. You simply need to access the TextBox control used for editing via the appropriate event handlers of the grid.
In the example below, because the editingTextBox field is declared WithEvents, the last method will handle the TextChanged event for the editing control while it's assigned to that field for the duration of the editing session.
Private WithEvents editingTextBox As TextBox
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
Me.editingTextBox = DirectCast(e.Control, TextBox)
End Sub
Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
Me.editingTextBox = Nothing
End Sub
Private Sub editingTextBox_TextChanged(sender As Object, e As EventArgs) Handles editingTextBox.TextChanged
'...
End Sub
Set the other columns to readonly = true and your numeric column = false and set the defaultcellstyle.format of your numeric column to "###,##0.00" and in your datagridview's cellvalidating event, do the ff:
Private Sub DatagridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DatagridView1.CellValidating
Try
If DatagridView1.IsCurrentCellDirty Then
Select Case DatagridView1.Columns(e.ColumnIndex).Name.ToUpper
Case "<NAME OF YOUR NUMERIC COLUMN>"
If Not IsNumeric(e.FormattedValue) Then
MsgBox("Invalid value.")
e.Cancel = True
Exit Sub
End If
If CType(e.FormattedValue, Integer) < 0 Then
MsgBox("Invalid value.")
e.Cancel = True
Exit Sub
End If
End Select
End If
Catch ex As Exception
ErrMsg(ex)
End Try
End Sub
Private Sub dgvwithdraw_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvwithdraw.CellClick
Select Case dgvwithdraw.Columns(e.ColumnIndex).Name
Case "Select", "Alloted"
dgvwithdraw.ReadOnly = False
Case Else
dgvwithdraw.ReadOnly = True
End Select
End Sub

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)

Parallel programming in VB.NET

I have a simple For Each loop in a Button's click even, which looks like so:
Protected Sub SelectAllButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SelectAllButton.Click
RadGrid1.AllowPaging = False
RadGrid1.Rebind()
For Each item In RadGrid1.Items
TryCast(TryCast(item, GridDataItem)("template").FindControl("CheckBox1"), CheckBox).Checked = True
Next
SaveSelectedRFPs()
RadGrid1.AllowPaging = True
RadGrid1.Rebind()
End Sub
Now inside that for each loop there could be upwards of 100k records to loop through and I know there has to be a more efficient way to accomplish this, maybe parallel programming?
#Nico Schetler:
I tried that, here's how the code looks
Protected Sub SelectAllButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SelectAllButton.Click
RadGrid1.AllowPaging = False
RadGrid1.Rebind()
Parallel.ForEach(RadGrid1.Items.AsQueryable.Cast(Of GridDataItem).ToList(), Sub(item)
TryCast(TryCast(item, GridDataItem)("template").FindControl("CheckBox1"), CheckBox).Checked = True
End Sub)
SaveSelectedRFPs()
RadGrid1.AllowPaging = True
RadGrid1.Rebind()
End Sub
But that gives me a Source is not IEnumerable<> error when I try to run it.
Correct me if I'm wrong... (no, seriously)
Don't you want to select every term in the array?
Currently, as far as I can tell, when you perform actions on your selected set, you check which one is checked. Maybe add a parameter to SaveSelectedRFPs() that determines whether to save selected RFPs or unselected ones. That saves you the entire loop process. Of course, you lose the visual effect, but it may be worth it to the customer for a massive performance advantage.
Public Sub SaveRFPs(SelectedOrUnselected As Boolean)
If selectedRFP.Checked = SelectedOrUnselected Then
selectedRFP.save()
End If
End Sub

NumericUpDown.value is not saved in the User Settings

I have a NumericUpDown Control on a form. In the Application Settings / Properties Binding, for the value parameter, i can't select my USER setting called : Heures (Integer / User).
I tried to save the value by this way :
Private Sub NumericUpDownHeures_Leave(sender As System.Object, e As System.EventArgs) Handles NumericUpDownHeures.Leave
My.Settings.Heures = NumericUpDownHeures.Value
My.Settings.Save()
End Sub
But it's not saved.
No problem for other settings (String / User). But i don't understand why the settings (Integer / User) are not saved.
Please help, Thanks.
As you are putting "NumericUpDown1.Value" you have to set the value at My.Settings.Heures to decimal.
In Form1_Load add:
NumericUpDownHeures.Value = My.Settings.Heures
and add to the event listener for your button or other widget:
My.Settings.Heures = NumericUpDownHeures.Value
I would guess the issue is that the Leave event is not being fired as you expect it to be, especially if the user just clicks the up/down arrows. I suspect that it is only fired when the user actually clicks into the value area, then leaves. You could verify this by debugging to see if your code is ever hit or by showing a simple msgbox from that event.
I think that you will have better luck if you hook the LostFocus or ValueChanged event.
I want to add to this as well for anyone looking at this in the future.
Save your settings as shown already by putting
My.Settings.Heures = NumericUpDownHeures.Value into your ValueChanged event, and then doing reverse in the form load event.
The problem is, this value changed event fires before the form load when you first initialize, so it will keep defaulting to whatever value you have set in the designer because you're overwriting the setting value with the designer value.
To get around this, you need a private/public boolean at the top of your code that is only set to true once your form has loaded (set to true at the bottom of your form_load event), then you can add the condition to the ValueChanged event checking if the form is loaded yet or not. If it is, then change the setting value, if not, then don't.
An example:
Private IsFormLoaded As Boolean = False
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
NumericUpDown1.Value = My.Settings.SavedNumValue
IsFormLoaded = True
End Sub
Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
If IsFormLoaded = False Then Exit Sub
My.Settings.SavedNumValue = NumericUpDown1.Value
End Sub
OR
Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
If IsFormLoaded Then
My.Settings.SavedNumValue = NumericUpDown1.Value
End If
End Sub