Deselect combobox selected value - vb.net

First of all, I apologize to the community because the title may seem a bit 'misleading from what actually is the real problem.
I have made a code that populates two combobox with the same values, with each change that occurs on the combobox I perform a check to see if the selected value is equal to another combobox; if it is equal, then execute a particular code otherwise proceed.
I need to implement a de-selection of value and I did it with:
ComboBox1.SelectedIndex = -1
Combobox2.SelectedIndex = -1
This works fine, but the code that checks if the values are equal interfere with this operation.
In fact, within each combobox I have something like this:
Private Sub ComboBox2_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
If ComboBox3.SelectedIndex = ComboBox2.SelectedIndex Then
MsgBox ("You can not compare two equal teams", "Warning")
off ()
End If
...
where "off ()" is the function that then doesn't continue what you're doing.
How do I solve this problem?

You'll have to disable the event when resetting the combobox. This can be done by removing the event with RemoveHandler/AddHandler and adding it again.
An other option is to use a flag. (This is just an example to show an idea, the flag variable should be properly placed).
Private FreezeEventFlag As Boolean = False ' or True, it depends..
' Declare it toplevel, initialize its value depending on
' the way you're going to initialize your Comboboxes selected values.
' Then set it's value to True each time you want to
' disable the event handling in any code block that resets
' SelectedIndexes to -1 like below :
' ...
FreezeEventFlag = True
ComboBox1.SelectedIndex = -1
Combobox2.SelectedIndex = -1
FreezeEventFlag = False ' Don't forget to set the Flag to false !
Private Sub ComboBox2_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
If FreezeEventFlag Then Exit Sub ' Just ignore this event this time.
If ComboBox3.SelectedIndex = ComboBox2.SelectedIndex Then
MsgBox ("You can not compare two equal teams", "Warning")
off ()
End If
End Sub

Related

Creating/Deleting Objects via Checking if Toggles in CheckListBox in VB.Net

I am playing around with some CheckedListItems in CheckBoxes and i am having issues with determining when a specific checked Product is checked and/or unchecked via its index position.
I know from MsgBox debugging that if i select either Product Allpurpose Cleaner or Cleaning Wipes it calls the MsgBoxes of both methods despite me attempting using logic to only execute for that specific Product. Somehow it thinks I have selected both items?
So essentially i am trying to do this:
If item zero in CheckedListBox is checked
Call conform menu to get desired amountand then come back to ordering menu.
ElseIF item zero in CheckedListBox is unchecked
Remove it from the current order.
This would essentially be rinse and repeat for all items in my CheckedListBox. I suspect VB.NET is causing my code to 'fall through' and it thinks all of the items i select are the same despite my attempts at preventing this.
May i please have some thoughts on this?
Thank you.
Private Sub CleaningProductsList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CleaningProductsList.SelectedIndexChanged
Dim allPurposeCleaner = New AllPurposeCleaner()
Dim cleaningCloths = New cleaningCloths()
' If checked.
If CleaningProductsList.GetItemChecked(0) = True Then
isChecked = True
Me.Hide()
' MsgBox("All Purpose Cleaner Selected")
AmountMenue.setGivenProduct(allPurposeCleaner)
AmountMenue.Show()
' If unchecked.
ElseIf CleaningProductsList.GetItemChecked(0) = False Then
isChecked = False
' MsgBox("All Purpose Cleaner UnSelected ")
MsgBox(CleaningProductsList.GetItemChecked(0).ToString + " ALLPURPOSE UNCHECKED")
AmountMenue.removedGivenProduct(allPurposeCleaner)
End If
' If checked.
If CleaningProductsList.GetItemChecked(1) = True Then
Me.Hide()
AmountMenue.setGivenProduct(cleaningCloths)
MsgBox("cleaning cloths Selected ")
AmountMenue.Show()
' If unchecked.
ElseIf CleaningProductsList.GetItemChecked(1) = False And CleaningProductsList.CheckOnClick = False Then
MsgBox("cleaning cloths UnSelected ")
MsgBox(CleaningProductsList.GetItemChecked(2).ToString + " Cleaning Cloths UNCHECKED")
isChecked = False
AmountMenue.removedGivenProduct(cleaningCloths)
End If
End Sub
Here's an example of how you determine changes in a CheckedListBox:
Private checkedIndexes As New List(Of Integer)
Private checkedItems As New List(Of String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CheckedListBox1.Items.AddRange({"First", "Second", "Third", "Fourth", "Fifth"})
End Sub
Private Sub CheckedListBox1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
If e.NewValue = CheckState.Checked Then
'An item is being checked.
checkedIndexes.Add(e.Index)
checkedItems.Add(CStr(CheckedListBox1.Items(e.Index)))
Else
'An item is being unchecked.
checkedIndexes.Remove(e.Index)
checkedItems.Remove(CStr(CheckedListBox1.Items(e.Index)))
End If
Label1.Text = $"Checked indexes: {String.Join(", ", checkedIndexes)}"
Label2.Text = $"Checked items: {String.Join(", ", checkedItems)}"
End Sub
The e parameter tells you what item is changing, via the Index property, and what it is changing from and to, via the CurrentValue and NewValue properties.
If you want to get a full list of checked items in that event handler, because the event is raised before the change is finalised, you need to start with the list provided by the control and then add or remove the current item:
Dim checkedItems = CheckedListBox1.CheckedItems.Cast(Of String)().ToList()
If e.NewValue = CheckState.Checked Then
checkedItems.Add(CStr(CheckedListBox1.Items(e.Index)))
Else
checkedItems.Remove(CStr(CheckedListBox1.Items(e.Index)))
End If
'Use checkedItems here.

Cancel EnterCell event of spread farpoint

My program use spread farpoint as a 3rd-party control, it has a TextBox control and a Spread control for showing data. When user change active cell in spread, I want to validate that the TextBox must not empty. If the TextBox is empty, EnterCell event must be cancel and the TextBox must got focus, also active cell of spread must not change. I'm stuck here.
Currently I performed validation in LeaveCell event but it doesn't work. EnterCell event fired and spread still changed the active cell :(
If TextBox1.Text = String.Empty Then
MsgBox ("TextBox1 cannot blank")
TextBox1.Select()
'TextBox1.Focus() 'I have tried this function but still not working
End If
Please support me!
Thanks all.
As far as my knowledge, there's nothing we can do to "cancel" EnterCell event. However, I found out that we could do a little trick to achieve it.
Private Sub spread_LeaveCell(sender as Object, e as LeaveCellEventArgs) Handles sprSheet.LeaveCell
If TextBox1.Text = String.Empty Then
MsgBox ("TextBox1 cannot blank")
'This is the trick
e.NewColumn = e.Column
e.NewRow = e.Row
Exit Sub
End If
'Other leave cell proceses..
End Sub
...
Private Sub spread_EnterCell(sender as Object, e as EnterCellEventArgs) Handles sprSheet.EnterCell
If TextBox1.Text = String.Empty Then
TextBox1.Select()
End If
Exit Sub

How do I get the textbox to enable after a certain amount of text is inputted?

So my next question(i know i know ive had a lot of questions already but im learning and my teachers suck)
but I am trying to get the textbox to go to readonly after a certain amount of text has been entered. I know how to make it a read only textbox but only after Ive had one set of data entered. i need it to be readonly after 7 days of data has been entered
I've tried inputtextbox.enabled = false
'Validating if user input is a number or not
Dim output As Integer
If Not Integer.TryParse(InputTextbox.Text, output) Then
MessageBox.Show("ERROR! Data must be a number")
InputTextbox.Text = String.Empty
Else
UnitsTextbox.AppendText(Environment.NewLine & InputTextbox.Text)
InputTextbox.Text = String.Empty
End If
InputTextbox.Enabled = False
I'm expecting it to disable after the user has entered 7 days worth of data but it only disables after one day of data is entered
Since the entries to UnitsTextbox are all done in code, this TextBox can be set to read only at design time.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim output As Integer
If Not Integer.TryParse(InputTextbox.Text, output) Then
MessageBox.Show("ERROR! Data must be a number")
Else
UnitsTextbox.AppendText(Environment.NewLine & InputTextbox.Text)
End If
'Moved this line outside of the If because it happens either way
InputTextbox.Text = String.Empty
If UnitsTextbox.Lines.Length >= 7 Then
Button2.Enabled = False
End If
End Sub
Here's some simple psuedocode
Private Sub InvalidateTextbox(sender As TextBox, e As KeyEventArgs) Handles TextBox1.KeyUp, TextBox2.KeyUp
'FOR ANY TEXTBOX YOU WANT TO CONTROL WITH THIS SUB, ADD AN ADDITIONAL HANDLE.
If Strings.Len(sender.Text) > 7 Then
'^SIMPLE CONDITIONAL, CHECKING IF THE LENGTH IS MORE THAN SEVEN CHARACTERS, MODIFY THIS TO SUIT YOUR NEEDS.
sender.Enabled = False
'^IF THE CONDITIONAL IS TRUE, DEACTIVATE THE CONTROL, IF THAT IS WHAT YOU ARE LOOKING FOR.
sender.ReadOnly = true
'^IF YOU WANT READONLY,NOT ENABLED/DISABLED.
End If
End Sub
This code will execute every time a key is pressed while the text boxes are active. What is after "Handles" defines what events will trigger the sub.
sender becomes the textbox object that triggered the sub. e holds all the event arguments for the keyboard, so you can evaluate things like which key was pressed and other neat things.
There was some confusion on if you wanted enabled/disabled or readonly, both options included.

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)

Using combobox from class

I have some questions on using of combobox.
1) I need to reference combobox from class like this:
If Me.ActiveControl.GetType Is GetType(ComboBox) And combodroppeddown = False) Then
something...
End If
From Here I need right from the AND to check if this combobox is dropped down but I don't know how.
2) My actual type of combobox is of "DropDownList" type.
Problem is that if I drop it down and type with up/down keys the value is changed according to selected row. If I then press ESC then last value stays as choosen what is not wanted.
Is here way to return original value from moment of dropping in case if I press ESC when list is dropped?
How to do that?
Here is closer look to my xCombo subclass to get help in second question...
Public Class xAutoCombo
Inherits ComboBox
Private entertext As String
Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
If e.Control Then ' this dropped a list with keyboard
If e.KeyCode = Keys.Down Then
Me.DroppedDown = True
End If
End If
'' this should return value back if ESC was pressed
'' but don't work!
If Me.DroppedDown And e.KeyCode = Keys.Escape Then
Me.Text = entertext
End If
MyBase.OnKeyDown(e)
End Sub
Protected Overrides Sub OnDropDown(ByVal e As System.EventArgs)
entertext = Me.Text '' this remember text in moment of droping
MyBase.OnDropDown(e)
End Sub
EDIT:
Here I found one issue in functionality which I like to be solved.
When combo is dropped and I navigate through list by keyboard and then press with mouse to form (or outside of combo) it closes a list and set value which is last been selected.
Instead of that I would like that combo set his new value ONLY on click with mouse to list or with pressing Enter key with keyboard.
Examine the DroppedDown property, but it seemed like you have other things you wanted to do while dropped down.
Dim cbo As ComboBox
If Me.ActiveControl.GetType Is GetType(ComboBox) then
cbo=Ctype(Me.ActiveControl, ComboBox)
' make it easier to refernece
if cbo.DroppedDOwn then
....
End iF
End if
' Note:
Ctype(Me.ActiveControl, ComboBox).DroppedDown
' should work, but the above is easier to read and use since you apparently
' will have other things to do/override with it
Note also that I think one of the three combobox dropdown types does not use/support the DroppedDown property.
For the rest of your question, which I dont entirely follow, you could also store the last selected item and restore it in similar fashion. Overriding windows default behavior though, is rarely a good idea because you are creating something the user has never encountered before.
EDIT
To change Escape functionality from CLOSE DROPDOWN to ABORT CHOICE:
NOTE: I just used a std cbo, refs will need to be changed to MyBase, events to On...
Private selectedindex As Integer = -1
Private bEsc As Boolean = False
Private Sub cbo_Enter(....
' reset tracking vars...might not be needed
selectedindex = -1
bEsc = False
End Sub
Private Sub cbo_DropDown(...
' capture starting state
selectedindex = cbo.SelectedIndex
bEsc = False
End Sub
KeyDown:
If cbo.DroppedDown AndAlso e.KeyCode = Keys.Escape Then
bEsc = True
e.Handled = True
' this MUST be last!
cbo.DroppedDown = False
End If
Private Sub cbo_DropDownClosed(...
' rest cbo.selectedindex if escape was pressed
If bEsc Then
' note: SelectedIndexChanged event will fire and any code there will run
' may need to qualify with If Esc = False...
cbo.SelectedIndex = selectedindex
End If
End Sub