How to fire an event when making a selection of an editable combobox? - wpf

I have a ComboBox that is databound to an ObservableCollection of strings. The ComboBox is also editable, so you can either enter in your own value or select one from the list. The issue I'm running into is the index of SelectedItem seems to be the index of the last item you selected when you've entered in your own value in the ComboBox, though it's -1 when you have IsTextSearchEnabled set to true.
The problem is, if someone entered their own value and then decide to instead select the item on the ComboBox that had been selected before, the index doesn't change so the SelectionChange event doesn't fire. How could I get an event to fire in this situation?

Test this...
I hope this helps:
Dim oldSEL As String = ""
'always checking while you move your mouse over the combobox (when altering selection) and using the keyboard to (alter selection)
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.MouseMove, ComboBox1.KeyPress
Dim currentSEL As String = ComboBox1.SelectedText
If Not (oldSEL = "" And currentSEL = oldSEL) Then
fire()
oldSEL = currentSEL
End If
End Sub
Private Sub fire()
Trace.Write("text selected changed")
End Sub
You should change all the Combobox1 to your liking.

Related

Setting IsChecked property of programmatically created RadioButtons

I have a bunch of programmatically-created RadioButtons that are displayed in a StackPanel.
I want to use some on-screen buttons or the arrow keys to navigate these RadioButtons, and set the IsChecked property to True when landing on them.
I'm just not sure how to reference these RadioButtons?
Any direction would be appreciated, thanks!
When you dynamically add the controls you will want to store them in a private member variable for reference, eg Private ctrls As New List<Controls>() and then in the Key_Down event set the ctrl[0].IsChecked.
Private Sub Window_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
If e.Key = Key.Down Then
Dim ctrlIndex As Integer = ControlIdWithFocus()
ctrls(ctrlIndex).IsChecked = True
End If
End Sub
Alternatively you could iterate through the Me.Controls collection, although that will contain all the controls not just the Radio buttons.

reflect values to combo box from datagrid on Selection Changed event

I have a combobox and textbox using which values are filled into DataGridViewfor display purpose.
The datagriduses SelectionChanged event.
I am Trying to reflect the same data (when the user moves keys up and down or on mouse click) onto the combobox and textbox on SelectionChanged event of datagrid
Private Sub DataGridView1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
combobox.Text = DataGridView1.CurrentRow.Cells("DatabaseTable_ColumnName1").Value.ToString()
textbox.Text = DataGridView1.CurrentRow.Cells("DatabaseTable_ColumnName2").Value.ToString()
End Sub
Above code works fine for textbox but not for combobox.
In case of combobox, it reflects data of initially selected row only and not the rest.
How do I fix this?
i guess you have to use this code for combobox
combobox1.items.add('your string here')
in your situation would be like this :
Private Sub DataGridView1_SelectionChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
combobox1.items.add(DataGridView1.CurrentRow.Cells("DatabaseTable_ColumnName1").Value.ToString())
textbox.Text = DataGridView1.CurrentRow.Cells("DatabaseTable_ColumnName2").Value.ToString()
End Sub

how to get the Hidden Columns Control Value in PreparingCellForEdit Silverlight Datagrid

how to get the Hidden Columns Control Value in PreparingCellForEdit Silverlight Datagrid
Code as Follows:
Private Sub TaskDataGrid_LoadingRow(ByVal sender As System.Object, ByVal e As
System.Windows.Controls.DataGridRowEventArgs)
Dim row As DataGridRow = e.Row
Dim cellContent As FrameworkElement = TaskDataGrid.Columns(8).GetCellContent(e.Row)
Dim cboLabValidated As ComboBox = CType(cellContent.FindName("cboLabValidated"), ComboBox)
Dim ViewModel As New NonFirmWareNewRequestViewModel()
If cboLabValidated IsNot Nothing Then
cboLabValidated.ItemsSource = ViewModel.YesNoValues
End If
TaskDataGrid.Columns(1).Visibility = Visibility.Collapsed
End Sub
in the above code I am hidden the column 1 in LoadingRow Event and in need to get the value of that Column in PreparingCellForEdit
Code for PreparingCellForEdit as Follows:
Dim fe As FrameworkElement = TaskDataGrid.Columns(5).GetCellContent(e.Row)
Dim fe1 As FrameworkElement = TaskDataGrid.Columns(1).GetCellContent(e.Row)
Dim gridCmbo As Grid = DirectCast(fe, Grid)
Dim gridCmbo1 As Grid = DirectCast(fe1, Grid)
Dim lbltaskId As Label = CType(gridCmbo1.FindName("lbltaskId"), Label)
Dim cboCompVerSel As ComboBox = CType(gridCmbo.FindName("cboCompVerSel"), ComboBox)
Dim lblCompVer As Label = CType(gridCmbo.FindName("lblCompVer"), Label)
I am using label control to show column 1 and I am identifying the label control object but the content becomes empty..
this is working fine
1) While in the Loading Row Event We should not hide the Column, if you hide the column you cannot get the value from the column let the data Load first in the LoadingRow Event.
2) Hide the column in the Selection changed event code as follows :
Private Sub TaskDataGrid_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles TaskDataGrid.SelectionChanged
TaskDataGrid.Columns(1).Visibility = Visibility.Collapsed
End Sub
this event will fire after LoadingRow Datagrid event or PreparingCellForEdit event once the Row is loaded we can hide the column and can get the value where ever you want.If you Hide in LoadingRow event without loading the Data you cannot get the value of that Control in the Datagrid template or Data Column.

SelectedIndexChanged event of dropdownlist in Listview not happening. in a usercontrol

I have a ListView and DropDownList(primary) and tow more and a text box, inside every Row of that ListView. All on a user control ,and using the this user control on the other usercontol(here the operations to be performed.)(we are using DNN) .
Now I would like to add event handler for dropdownlist which would react on SelectedIndexChanged. I wanted to show and hide the other dropdowns and textbox depending on a specific value of the DropDrownList(primary) selected.
I am nowhere to do the same..despite the several attempts.
Screening Outcome:&nbsp
Comments:
Subset Used:&nbsp
TAR Status:&nbsp
code behind like this
Public Sub ddlFindingsScreeningOutcome_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim item As ListViewItem = CType(ddl.NamingContainer, ListViewItem)
Dim txtFindingsComments As TextBox = CType(item.FindControl("txtFindingsComments"), TextBox)
Dim ddlFindingsTarStatus As DropDownList = CType(item.FindControl("ddlFindingsTarStatus"), DropDownList)
If Not ddl Is Nothing Then
If Not ddl.SelectedItem.Text = "Not Screened" Then
txtFindingsComments.Visible = True
ddlFindingsTarStatus.Visible = True
End If
Else
txtFindingsComments.Visible = False
ddlFindingsTarStatus.Visible = False
End If
End Sub

Apply and validate a bound DataGridViewComboBoxCell directly upon selection change

I have a windows forms DataGridView that contains some DataGridViewComboBoxCells that are bound to a source collection using DataSource, DisplayMember and ValueMember properties. Currently the the combobox cell commits the changes (i.e. DataGridView.CellValueChanged is raised) only after I click on another cell and the combobox cell loses focus.
How would I ideally commit the change directly after a new value was selected in the combobox.
This behaviour is written into the implementation of the DataGridViewComboBoxEditingControl. Thankfully, it can be overridden. First, you must create a subclass of the aforementioned editing control, overriding the OnSelectedIndexChanged method:
protected override void OnSelectedIndexChanged(EventArgs e) {
base.OnSelectedIndexChanged(e);
EditingControlValueChanged = true;
EditingControlDataGridView.NotifyCurrentCellDirty(true);
EditingControlDataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
This will ensure that the DataGridView is properly notified of the change in item selection in the combo box when it takes place.
You then need to subclass DataGridViewComboBoxCell and override the EditType property to return the editing control subclass from above (e.g. return typeof(MyEditingControl);). This will ensure that the correct editing control is created when the cell goes into edit mode.
Finally, you can set the CellTemplate property of your DataGridViewComboBoxColumn to an instance of the cell subclass (e.g. myDataGridViewColumn.CellTemplate = new MyCell();). This will ensure that the correct type of cell is used for each row in the grid.
I tried using Bradley's suggestion, but it was sensitive to when you attached the cell template. It seemed like I couldn't allow the design view to wire up the column, I had to do it myself.
Instead, I used the binding source's PositionChanged event, and triggered updates from that. It's a little bit odd, because the control is still in edit mode, and the databound object doesn't get the selected value yet. I just updated the bound object myself.
private void bindingSource_PositionChanged(object sender, EventArgs e)
{
(MyBoundType)bindingSource.Current.MyBoundProperty =
((MyChoiceType)comboBindingSource.Current).MyChoiceProperty;
}
A better way to achieve this that I am using successfully rather than subclassing or the somewhat inelegant binding source method above, is the following (sorry it's VB but if you can't translate from VB to C# you have bigger problems :)
Private _currentCombo As ComboBox
Private Sub grdMain_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles grdMain.EditingControlShowing
If TypeOf e.Control Is ComboBox Then
_currentCombo = CType(e.Control, ComboBox)
AddHandler _currentCombo.SelectedIndexChanged, AddressOf SelectionChangedHandler
End If
End Sub
Private Sub grdMain_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdMain.CellEndEdit
If Not _currentCombo Is Nothing Then
RemoveHandler _currentCombo.SelectedIndexChanged, AddressOf SelectionChangedHandler
_currentCombo = Nothing
End If
End Sub
Private Sub SelectionChangedHandler(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myCombo As ComboBox = CType(sender, ComboBox)
Dim newInd As Integer = myCombo.SelectedIndex
//do whatever you want with the new value
grdMain.NotifyCurrentCellDirty(True)
grdMain.CommitEdit(DataGridViewDataErrorContexts.Commit)
End Sub
That's it.

Resources