Combo box's last value stays activated even if .removeitem has removed it - vba

I have a situation where combobox's last value stays in the screen even if it has been removed via VBA.
I was using the method
cboBox.Removeitem "Drafter"
but here's what I would see
i've try cboBox.Requery afterwards, but no luck.
any help would be appreciated. thanks.
UPDATE:
I tried changing the combo box's source type from Value list to Table/Query, and just .requery after the values are updated. The same problem presists. Access has seemed to set "Drafter" as the default value now.

The Combo Box retains its .Value even after the underlying Item has been removed from the list. That's why it still shows up in the (top) text box portion of the control even after it has been removed from the drop-down list portion. You could try this:
Dim itemToRemove As String
itemToRemove = "Drafter"
Me.cboBox.RemoveItem itemToRemove
If Me.cboBox.Value = itemToRemove Then
Me.cboBox.Value = Null
End If

Related

Why is text clearing from the text field of a combobox if message is displayed?

I'm working in VB.NET, Visual Studio 2017. I have a combobox with DropdownStyle = Dropdown. If the user types something invalid in the text field of the combobox (invalid means it doesn't match a value in the combobox) then we display a message and then return focus to the text field with the text highlighted, so they can see what they typed. The message is displayed from the Validating event.
This works fine if they don't open the dropdown. If they do open the dropdown and, while it is open, they type in an invalid entry, the message displays but the entry they typed clears.
I have put in debug statements to see what events are firing. Before the message displays, I get a DropDownClosed (text is still there), then a TextChanged (text is still there), then a second TextChanged (text is now empty). I think something about losing focus to display the message may be triggering something, but I can't figure out what.
I can save off the text and then replace it after the message displays, but while the message is up, the text field is blank.
Any ideas?
While I still don't understand the series of events that causes the problem, I found a solution. Just before the message is displayed from within the Validating event of the combobox, I set focus to the combobox. I'm guessing that tabbing away from the invalid entry loses focus, and because the dropdown is open, it then closes which somehow clears the entry. Go figure.
Here is the code from the validating event:
If testInList Then
ResultTextBox.Enabled = True
Else
TestComboBox.Focus
'If test is not in combobox, display message.
MyUtility.MicroUtilities.DisplayMessage(
My.Resources.RES_MSG_INVALID_TEST, MessageBoxIcon.Information, , , True)
e.Cancel = True
End If
The "TestComboBox.Focus" line has fixed the issue of the text disappearing. However, if the user's invalid entry is a partial match for an item in the dropdown, then the text in the text field of the combobox is updated to that item, so now it looks like they typed a valid entry but are getting an message that it's invalid. For example, if there is an entry in the dropdown of "NAMC" and they type "NA" (with the dropdown open) and tab away, the entry changes to "NAMC".
Any ideas on how to prevent that?
p.s. The AutocompleteMode is set to None.
So again, I don't understand the sequence of Events that causes the issue, but I've found something that works. If I move the new "TestCombobox.Focus" line outside of the If condition, the text remains in the text field before, during and after the message displays:
TestComboBox.Focus()
testInList = TestEntryValid()
If testInList Then
EnableResultFields(True)
Else
'If test is not in combobox, display message.
MyUtility.MicroUtilities.DisplayMessage(
My.Resources.RES_MSG_INVALID_TEST, MessageBoxIcon.Information, , , True)
e.Cancel = True
End If
This moves the .Focus before TestEntryValid, but I don't see anything in there that would trigger any events:
Private Function TestEntryValid() As Boolean
Dim item As String = TestComboBox.Text
Dim validTest As Boolean = False
If item.Length > 0 Then
Dim index As Integer = TestComboBox.FindStringExact(item)
If index > -1 Then
validTest = True
End If
End If
Return validTest
End Function
If anyone can explain why this works, I'd love to know. If not, thanks to everyone who responded!

How do I get the updated value of a textbox in VBA Access in a custom Sub?

This is for VBA Access 2003
I've got a textbox I want to use as a filter for a list box rowsource command. I also have a checkbox which adds another filter for the same rowsource command. I've only programmed in C# and I'm trying to write a single Sub which will simply set the RowSource regardless of if my textbox filter is changed or if my checkbox filter is changed. However, my textbox is giving me problems.
If my checkbox filter changes and I run my method the textbox.Text throws an error saying that it must have focus - Text is null. If I do a null check on that property it throws an error saying the control must have focus.
I've used the .Value property, but for whatever reason it doesn't update to the newer values.
My current attempt:
If Me.txtClientFilter.Text = Null Then ' Error 2185
filter = Me.txtClientFilter.Value
Else
filter = Me.txtClientFilter.Text
End If
Should I
Manually add focus then remove it everytime I want to check a
control?
Duplicate my code in each control's event Sub?
Manually set
the .Value property when the change happens?
I fixed my problem with some code which I'll show below. I don't know what was happening behind the scenes, but the .Value was not getting updated with the .Text value. I decided to set it explicitly, which then caused the entire text box value to be selected.
I ended up with the following code which explicitly sets the .Value of the control and also reset the cursor to the end of the text in the control. Thanks to some guy named Brent Spalding here for the cursor code.
Private Sub txtClientFilter_Change()
Me.txtClientFilter.Value = Me.txtClientFilter.Text
ProcessFilter
txtClientFilter.SelStart = Len(Me.txtClientFilter.Text)
txtClientFilter.SelLength = 0
End Sub

Checkbox in a DataGridView column is not getting checked sometimes?

I use this code to check all the check-boxes in a data grid view in VB.NET (2010 - .NET Framework 4.0),
Dim i As Integer
For i = 0 To (DataGridView.Rows.Count - 1)
DataGridView.Rows(i).Cells(0).Value = True
Next
Sometimes when one of the row is selected (not checked but just the entire row is selected) while this code is running, it checks all other check-boxes in the column except the selected one. What is wrong here?
I also try adding this code,
DataGridView.SelectedCells(0).Cells(0).Value = True
Did not work.
Like I said in the comments, the issue is that the box is checked in the datasource, but the GUI hasn't refreshed that cell to reflect it. If you click off of it, the cell should change/refresh to your new value. After you have made your data changes, at the very end you should deselect cells/rows. You can do it by:
Datagridview.ClearSelection()
or
Datagridview.CurrentCell = Nothing 'deselects
You get the idea. This is what has worked for me in the past.

How to see if a datagridviewcheckbox is checked or not

I have created a datagridview with a DataGridViewCheckBoxColumn. Initially I read in my data and set the value of the checkbox to true if the data is there and false if it is not. This works fine, but If I change the state of the checkbox to either checked or unchecked from what it was previously, I can't read this change. When I try to save the data it always shows, the checkbox's original state.
How can I re-read the checkbox's value after it has been clicked?
I tried the solution show here:
How to check a checkbox created with VB's DataGridViewCheckBoxColumn on Runtime
using: DirectCast(DataGridView1(0, 2).Value, Boolean)
but it does not work for me. I always get the original state of the checkbox when I look at it. If the box was originally checked and I uncheck it, this value will still show True.
You may be looking at the wrong row (you are looking at the first row only with the index of 0). You can use the DataGridView_CellEndEdit Event to find the value of the checkbox, like so...
If e.ColumnIndex = 2 Then 'this is the columnIndex of your checkbox column
If MyBindingSource.Current("CheckBoxColumnName") = true then
'the column is checked, so do something
End If
End If

Can't un-select my DevExpress LookUpEdit

I have a DevExpress LookUpEdit that I am using withing Visual Studio 2008 in VB.Net.
I have the LookUpEdit bound to my datasource and the value that it is displaying may be null or one of the rows in the datasource. My program displays a treelist and when a node is select the LookUpEdit is supposed to display the assigned value (could be null) and let the user reassign the value. I do have the LookUpEdit.Properties.AllowNullInput set to True. Right now when the program first starts if the first node in the treelist I choose has a null value the LookUpEdit displays nothing, if I change the value of the LookUpEdit the value changes in the database, if I change to a node that has a value for the LookUpEdit the value does display.
The problem is that if I switch from a node with a value to one without the LookUpEdit displays the previous value. I have gone through the debugger and it is still going through the fetch properly.
I have tried to reset the LookUpEdit.Text, LookUpEdit.EditValue and LookUpEdit.SelectedText but nothing works. I even replicated the conditions that the LookUpEdit has when it first displays nothing (LookUpEdit.Text = "" and LookUpEdit.EditValue = " ") but it still displays the last value.
I am setting the actual value with lueLocation.EditValue = lueLocation.Properties.GetKeyValueByDisplayText(valueName)
EDIT
So I narrowed it down. After I set the Text and EditValue to nothing
lueLocation.Text = Nothing
lueLocation.EditValue = Nothing
The values are set. The problem is that in the act of setting the value the dropdown menu opens. So I get it to close with lueLocation.ClosePopup(). For some reason when it gets called it changes the .Text and .EditValue back to the previous values and thus calls the TextChanged Event.
No clue why but I can't keep the dropdown menu open.
Generally in the UI the key combination of Ctrl-Del should clear it and set it to nothing. Otherwise in code, you should be able to set the EditValue = Nothing and that should do it.
Here are a couple of links on the DX site:
Search: http://search.devexpress.com/?q=clear+lookupedit&p=T4%7cP1%7c4&d=447
http://www.devexpress.com/Support/Center/p/Q96464.aspx
http://www.devexpress.com/Support/Center/p/Q270901.aspx
I solved the problem. As you can see in the Edit it was the lueLocation.ClosePopup() that actually caused it to revert back to the previous .Text and .EditValue values. I removed the lueLocation.ClosePopup() which then caused my interface to have the dropdowns remain open if the value was null and closed if there was an actual assigned value.
I found that if I set the .EditValue to DBNull.Value (lueLocation.EditValue = DBNull.Value) rather than Nothing, "", or " " it set the value assigned to the LookUpEdit to nothing and automatically closed the dropdown menu.