Setting focus to another control prevents current control value from changing in VB.NET - vb.net

I have a ComboBox in a WinForms app written in VB.NET. In the .SelectionChangeCommitted event I want to change the focus to a different specific control to assist user workflow. However when I do that, the change is not saved on the initial ComboBox and the value & index are reverted to the original values.
I've used both myControl.Focus and myControl.Select
The Combobox is setup like this:
With ChoosePartType
.DisplayMember = "PartName"
.DataSource = GetTable(qry) 'This custom function returns a DataTable with fields PartNum and PartName
.ValueMember = "PartNum"
.SelectedIndex = -1
End With
I assume something in the changing focus is short-circuiting the property change. Is there a way to force that to happen before I change focus?
Note: seems like a different issue from WInforms Combobox SelectionChangeCommitted event doesn't always change SelectedValue
Similar to this but I don't use databindings: Combobox DataBinding Bug - Won't write value if programmatically losing focus

Try using:
ChoosePartType.SelectedItem.Row("PartNum")
Assuming that this is the first column of the control's datasource, this could be also written as:
ChoosePartType.SelectedItem.Row(0)

Related

Entity with Winforms / bindingsource - can't save user input from controls

I have a winforms program I am migrating to entity (EF5). The edit works okay. The add does not. The basic workflow is user can either add or edit an object. There is a list of comboboxes, text boxes, etc on screen for the user to input.
I have the entity bound to a binding source. I have the textbox bound to the bindingsource.
Add functionality looks like this.
If (mViewAddEdit = ViewAddEditEnum.Add) Then
Dim ps As New RADS.PHANTOM_SESSION
Me.bsPhantom.DataSource = db.PHANTOM_SESSION.Add(ps)
Me.EdtMuscleWater.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.bsPhantom, "MUSCLE_OR_WATER", True, DataSourceUpdateMode.OnValidation)) ' etc
End If
So that seems okay, but when user clicks ok button (to save).
drvPhantom = TryCast(Me.bsPhantom.Current, RADS.PHANTOM_SESSION)
' examining the object shows nothing bound.
If mViewAddEdit = ViewAddEditEnum.Add Then
db.PHANTOM_SESSION.Add(drvPhantom)
db.SaveChanges()
End If
The issue arose from the form blocking validation.
Me.AutoValidate was set to disable. So this blocked the binding source from updating even with a call to Me.Validate()
Also since I found it so hard. This is how to Add or Edit - controls bound to binding source using Entity EF5.
If (mViewAddEdit = ViewAddEditEnum.Add) Then
Me.bsPhantom.DataSource = New PHANTOM_SESSION
SetPhantomDataBindings()
Else
Dim query = From ps In db.PHANTOM_SESSION Where (ps.PHNTM_SESSION_NO = CDbl(mPhntmSessionNum))
bsPhantom.DataSource = query.ToList() ' resize occurs here
SetPhantomDataBindings()
...
End If
The other thing is if you are programmatically writing to the text box, ie textbox1.text = "foo" you must also update the underlying object.
CType(Me.bsPhantomBlock.Current, RADS.PHANTOM_BLOCK).OTHER_CORRECTION = CDec(val)
Here we are casting the current object of the binding source to its type (PHANTOM_BLOCK) then setting the field "OTHER_CORRECTION" to val.
Updating the binding source does not work as it causes all inputted user data to be thrown away. For example there are 5 text boxes (a,b,c,d,e). user enters 1,2,3,4,5 into those. The 5th is handled by bindingsource Write: textbox1.DataBindings(0).WriteValue - this will update the 5th field (e),but causes a,b,c,d to be blank as the are reverting to stored value. This is my experience anyway.

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

Setting the value of cell in datagridview not working in form load

This is my code for setting the value in datagridview cell:
For i = 0 To dvJOBranch.Rows.Count - 1
dvJOBranch.Rows(i).Cells.Item("XS").Value = 0
dvJOBranch.Rows(i).Cells.Item("S").Value = 0
dvJOBranch.Rows(i).Cells.Item("M").Value = 0
dvJOBranch.Rows(i).Cells.Item("L").Value = 0
dvJOBranch.Rows(i).Cells.Item("XL").Value = 0
Next
Its working in button event, shown form event, but not in form load, and there are no errors.
My question is why it does not work in form load?
My guess is that you are using the DataGridView.AutoGenerateColumns functionnality and even if you set the DataSource property, the DatagridView won't create columns until the grid is displayed.
It could explain why it's not working in formload (grid is not initialized yet) and it works after (with shown event for example).
So it's possible that:
you try to access items that do not exist yet (but the code should raise an exception)
or you access valid rows or columns, but they are replaced when the grid is displayed the first time or bound again to a data source, and so your code has no effect (probably your case since you do not mention an exception).
Using form_shown is maybe a possible workaround, but I recommend you to use the DataGridView.DataBindingComplete event which is more especially designed to handle this situation.
See also these related issues (same cause):
Why DataGridColumn not getting removed from DataGridView
Datagirdview and tabcontrol issue
Strange issue with a datagridview and a tabcontrol C#
DataGridView has no columns

MS Access Force Update on Combobox

How do I force an update when the ComboBox value is changed in code. Below is piece of code I have tried but does not seem to work
If (Not Mid(sCode, 1, 2) = ddlLevelID1) Then
ddlLevelID1 = Mid(sCode, 1, 2) 'force change/force AFTER_UPDATE event to run.
End If
Assuming ddlLevelID1 is the ComboBox:
ddlLevelID1.value = foo
will change the value. I do not believe you can link a value displayed in a ComboBox to a variable value without pushing changes up to the userform after the value is changed.
Regarding the AfterUpdate method, from msdn:
Changing data in a control by using Visual Basic or a macro containing
the SetValue action doesn't trigger these events for the control.
However, if you then move to another record or save the record, the
form's AfterUpdate event does occur.
http://msdn.microsoft.com/en-us/library/office/bb238392(v=office.12).aspx

Changing styling of DataGridViewComboBoxCell from DropDownList to DropDown?

(This question is narrower in scope than this question I asked earlier.)
Say I have a DataGridViewComboBoxColumn, and want to switch the style of the ComboBox control between DropDownList and DropDown (mainly for the text field editing capability). I'd like to do this on a row-by-row basis (at the DataGridViewComboBoxCell level).
How would I do that programatically?
Or maybe a different way of asking: How do I access the ComboBox control given an object of type DataGridViewComboBoxCell?
(I'm in VB 2005.)
Thanks as always.
Not sure if you still need an answer, but i have a question that covers similar ground: Detect which column is showing an editing control in a datagridview
i use something like this line to get the combobox out of the DataGridView (DGV) in my DataGridView's CellValidating event:
Dim comboBox As DataGridViewComboBoxCell = DGV.Item(e.ColumnIndex, e.RowIndex)
later i use this line to get change the DropDownList ComboBoxCell to a DropDown:
cb.DropDownStyle = ComboBoxStyle.DropDown
For mine to work i had to make sure the 'cb' was of the ComboBox type, i dont remember if i was able to get it working well if the combobox was of the DataGridViewComboBoxCell type.