I have two dropdowns, dependent upon each other selection. Now what I want is to reload both dropdown, when user clicks on first item, i.e, index 0.
onchange event is already associated with the dropdowns, so selectedindexchanged event is not being called.
What else can be done in this scenario?
'so selectedindexchanged event is not being called.'
In the properties for your combobox, set 'AutoPostBack' to true and this event will now be called.
Sorry; had to make an edit - I meant true not false.
Edit:
OnChange event is associated with my dropdown, and now am trying to add selectedindex changed event. Is it possible?
Yes, controls can have many events attached to it.
Code which works for selectedindex :
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 1; i < 10; i++)
{
DropDownList1.Items.Add(i.ToString());
}
DropDownList1.AutoPostBack = true;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
Related
Using C# Winforms, I have a form with a BindingNavigator.
When the user clicks the delete button I want to log the record before it is deleted.
It is easy to tell that delete has been clicked using
private void bindingSource_ListChanged(object sender, ListChangedEventArgs e)
{
if (e.ListChangedType == ListChangedType.ItemDeleted)
{
}
}
However BindingSource.ListChanged is a completed event which fires after the row
deletion is done. When this event is firing, the row is already deleted so how can I trap it?
I am binding to a list of objects.
this.bindingSource.DataSource = Controller.Data; // returning a List<Person>()
this.bindingNavigator.BindingSource = this.bindingSource;
.
The bindingNavigator has a ToolStripItem called bindingNavigatorDeleteItem
The bindingNavigator's DeleteItem property had been set to this ToolStripItem.
Instead I set it to none.
Then set the Click Event of the bindingNavigatorDeleteItem ToolStripItem
this.bindingNavigatorDeleteItem.Click += this.bindingNavigatorDeleteItem_Click;
private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
{
this.bindingSource.EndEdit();
MyLogger( this.bindingSource.Current);
this.bindingSource.RemoveCurrent();
}
I have a combobox and with this I have a associated event
private void comboBox8_SelectedIndexChanged(object sender, EventArgs e)
{
}
My comobox is populated with two items a and b
I am setting combobox8.selectedItem = x where x= a or b. My event only fires if I select a from b or b from a. It does not fire if I again select a from a.
How can I do it and what's the appropriate event to deal with it ?
Moreover I am doing it all programmatically.
It makes sense that the event doesn't fire again. The selected item doesn't change. Depending on what you actually want, there are a lot of events you can utilize. You might start with Click, or DropDown, or DropDownClosed, for instance.
It won't fire because Selected Index did not change...
Take a look at msdn documentation for a list of comboBox events:
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox_events.aspx
You'll find out you can use more one depending on what you want to achieve (leave,lostfocus, [...])
Because the index did not change, the event is not fired. Since you need this processing when you are refreshing the form programmatically, call the appropriate code programmatically as well:
private void comboBox8_SelectedIndexChanged(object sender, EventArgs e)
{
ProcessComboBoxInput();
}
private void RefreshFormProgrammatically()
{
// Refresh the form here...
ProcessComboBoxInput();
}
private void ProcessComboBoxInput()
{
// Process the comboBox8 here...
}
Because its selected index changed event. From a to a nothing has been changed. You can try onclick event.
I have designed a gridview with a radio button inside the itemtemplet. I also have a confirm button. A user is allowed to check several radio button and when a confirm button is pressed then database is updated with the value of radio button.
But when i check the button it is shown as checked in UI but when I check through code behind, it shows checked property of radio button as false.
for (int i = 0; i < gvTransaction.Rows.Count; i++)
{
if (!((String.IsNullOrEmpty(gvTransaction.Rows[i].Cells[0].Text)) || (gvTransaction.Rows[i].Cells[0].Text == " ")))
{
string transactionID = gvTransaction.Rows[i].Cells[0].Text;
RadioButton btn = (RadioButton)gvTransaction.Rows[i].FindControl("rbtSelect");
if (btn.Checked)
{
// Although the radiobutton is checked code never reach here.
}
}
}
If this is Winforms, my guess would be because the edited Radio button cell value is not committed until it's validated, which happens when the cell lose focus. If you want to commit the modifications immediately, you can handle the CurrentCellDirtyStateChanged event, and call the CommitEdit method in the handler like this:
void gvTransaction_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (gvTransaction.IsCurrentCellDirty)
{
gvTransaction.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
Use this if the radiobutton.checkedchanged event.
How are you binding the GridView? If you are doing it in Page Load, make sure that you are not reloading the gridView on page PostBacks
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //Bind GridView here }
do let us know if this help
I have a windows form with a listview control. I set the MultiSelect property to true and I added a selected_index changed event.
I get the event fired when I click the same index as the current selected index.
My expectation is that I will not get the event fired. The strange thing is that the event fired 1 second after I click the index.
I appreciate for any reply to explain why this is happening.
Edited:
Sample Code:
private void Form1_Load(object sender, EventArgs e)
{
listView1.View = View.Details;
listView1.MultiSelect = true;
listView1.FullRowSelect = true;
listView1.Columns.Add("Number");
listView1.Items.Add("1");
listView1.Items.Add("2");
listView1.Items.Add("3");
listView1.Items.Add("4");
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
MessageBox.Show("Selected Index Changed event fired: ");
}
}
Follow these steps to see the problem:
Try to select one item, for instance: select number 3
expected result: listview1_SelectedIndexChanged is fired
Result: It is fired.
Try to click the number 3 again.
expected result: listview1_SelectedIndexChanged is NOT fired
Result: It is fired with one second delay.
From the MSDN documention on ListView.SelectedIndexChangedEvent:
In a multiple selection ListView control, this event occurs whenever an item is removed or added to the list of selected items. To determine which items are selected in the ListView control, use the SelectedItems property to access the ListView.SelectedListViewItemCollection.
As for why the event waited so long to fire: I can only imagine the processor was tied up doing something else. Do you have more details regarding what you're seeing, exactly (sample code would help)?
I have a C# ComboBox using WPF. I have code that executes when the ComboBox's GotFocus is activated. The issue is that the GotFocus event is executed every time a selection is made from the ComboBox. For example, the GotFocus is executed when you first click on the ComboBox and then when you make a selection even though you have not click on any other control.
Is it possible to prevent this event from firing if a selection is being made in the list or is there a flag or something else in the event handler that can be used to determine if the GotFocus event handler was fired as a result of the user selecting an item in the list?
You can solve this problem with next verification:
private void myComboBox_GotFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() == typeof(ComboBoxItem))
return;
//Your code here
}
This code will filter all focus events from items (because they use bubble routing event). But there is another problem - specific behaviour of WPF ComboBox focus: when you open drop-down list with items your ComboBox losing focus and items get. When you select some item - item losing focus and ComboBox get back. Drop-down list is like another control. You can see this by simple code:
private void myComboBox_GotFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() != typeof(ComboBoxItem))
{
Trace.WriteLine("Got " + DateTime.Now);
}
}
private void myComboBox_LostFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() != typeof(ComboBoxItem))
{
Trace.WriteLine("Lost " + DateTime.Now);
}
}
So you will get anyway atleast two focus events: when you select ComboBox and when you selecting something in it (focus will return to ComboBox).
To filter returned focus after selecting item, you can try to use DropDownOpened/DropDownClosed events with some field-flag.
So the final code with only 1 event of getting focus:
private bool returnedFocus = false;
private void myComboBox_GotFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() != typeof(ComboBoxItem) && !returnedFocus)
{
//Your code.
}
}
private void myComboBox_LostFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() != typeof(ComboBoxItem))
{
ComboBox cb = (ComboBox)sender;
returnedFocus = cb.IsDropDownOpen;
}
}
Choose from this examples what you actually need more for your application.
I'm not too hot on WPF; but if you're trying to detect changes to the list (click on new value etc) you can use SelectedIndexChanged events..
On the other hand, if you really do want to know simply when the control is focussed, can you filter it by saying something like;
if (combo1.Focused && combo1.SelectedIndex == -1)
{
...
}
.. ? It really depends on what youre trying to detect, exactly.
Another solution is used is to determine whether the new focused element is an existing item in the combobox. If true then the LostFocus event should not be performed, because the combobox still has focus. Otherwise an element outside the combobox received focus.
In the code snipplet below I added the functionality in a custom combobox class
public class MyComboBox : System.Windows.Controls.Combobox
{
protected override void OnLostFocus(RoutedEventArgs e)
{
//Get the new focused element and in case this is not an existing item of the current combobox then perform a lost focus command.
//Otherwise the drop down items have been opened and is still focused on the current combobox
var focusedElement = FocusManager.GetFocusedElement(FocusManager.GetFocusScope(this));
if (!(focusedElement is ComboBoxItem && ItemsControl.ItemsControlFromItemContainer(focusedElement as ComboBoxItem) == this))
{
base.OnLostFocus(e);
/* Your code here... */
}
}
}