To make it as simple as possible: ComboBox1 is bound to an empty list (in Form1 load event handler), and there is an event handler associated with ComboBox1:
private void CB1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("Event fired");
}
private void Form1_Load(object sender, EventArgs e)
{
CB1.DataSource = list1;
CB1.ValueMember = "Name";
CB1.DisplayMember = "Name";
}
The form is loaded, CB1.SelectedIndex = -1, CB1.Text = "", CB1.Items.Count = 0
When I click on Button1, list1 is populated. Now the situation is as follows:
CB1.SelectedIndex = 0, CB1.Text = "Some Text", CB1.Items.Count =196
BUT, the event didn't fire, although SelectedIndex changed from -1 to 0, and I didn't get MessageBox.Show("Event fired"). However, when the user selects some item from the list, the event fires. Also, there is another button that clears list1, and consequently CB1.Items. When this button is pressed, the event also fires (SelectedIndex changes from X to -1).
I've tried to use other events, such as SelectedValueChanged, TextChanged, SelectionChangeCommitted, with no success.
Although there is a simple brute force workaround for this problem, I still do not understand why the problem appears in the first place, and thus cannot anticipate similar situations. That's why I would be grateful if somebody explained to me why no events are firing in the situation I described.
My comment got enough attention, so it seemed appropriate that I should put this as a potential answer. You should make sure that you've actually assigned the event to the method either through a delegate or in the designer with the properties of the combobox itself.
// Somewhere in the form load or init events
CB1.SelectedIndexChanged += new EventHandler(CB1_SelectedIndexChanged);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.button1.Click += new System.EventHandler(this.comboBox1_SelectionChangeCommitted);
}
private void Form1_Load(object sender, EventArgs e)
{
List<string> list = new List<string> { "a", "b", "c" };
comboBox1.DataSource = list;
comboBox1.SelectedIndex = 0;
}
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
MessageBox.Show(comboBox1.SelectedValue.ToString());
}
private void button1_Click(object sender, EventArgs e)
{
comboBox1.SelectedIndex = 1;
}
}
Related
I have one Combo box and it has SelectedIndexChanged Event but i want Ignore that event in some case how can i achieve that functionality.
describe code is below
private void Form1_Load(object sender, EventArgs e)
{
List<string> lstString = new List<string>();
lstString.Add("One");
lstString.Add("Two");
lstString.Add("Three");
foreach (string str in lstString)
cBox.Items.Add(str);
//Here I want Ignore cbox_SelectedIndexChanged Event
cBox.SelectedIndex = 0;
}
private void cBox_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("Your Selected Item is :- " + cBox.SelectedItem.ToString());
}
You can choose either of the 2 approaches.
Have a bool flag which will be set for those conditions when you want to ignore the event handler from running. And use that flag inside your SelectedIndexChanged method
Subscribe to the event only after you have set cBox.SelectedIndex=0 if that is the only case.
Instead of subscribing the event within the designer (I expect you to do this at the moment) you can subscribe to the event in code after the initialization is done.
private void Form1_Load(object sender, EventArgs e)
{
// Init stuff
cBox.SelectedIndex = 0;
// Event subscription
cBox.SelectedIndexChanged += cBox_SelectedIndexChanged;
}
private void cBox_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("Your Selected Item is :- " + cBox.SelectedItem.ToString());
}
in winform when i create a combobox i can found event "SelectedIndexChanged"
the event work after index of combobox changed
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("after index change app show this MessageBox ");
}
but in wpf i cannot found event "SelectedIndexChanged"
instead of i can found event "SelectionChanged"
but i have a problem when is use it before index of combobox event work but i want to after index change show my code in event "SelectionChanged"
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show("before index change app show this MessageBox ");
}
what should i do . i want to show my MessageBox after i change index of my combobox
sry for my poor english
Actually The event 'SelectionChanged' is called after index and value are changed you can check it simple
public partial class MainWindow : Window
{
private string[] _cmbxSource = new string[] {
"ZeroIndex",
"FirstIndex"
};
public MainWindow()
{
InitializeComponent();
cmbx.ItemsSource = _cmbxSource;
cmbx.SelectionChanged += cmbx_SelectionChanged;
}
void cmbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show(string.Format("Value and Index has been changed {0} {1}",
_cmbxSource[cmbx.SelectedIndex], cmbx.SelectedIndex));
}
}
I Create a function NewLoad() and call it in butto1_click.
And i have event listBox1_SelectedIndexChanged which called itself during operation function "NewLoad"
private void button1_Click(object sender, EventArgs e)
{
NewLoad();
}
private void NewLoad()
{
String text = textBox1.Text.Trim();
textBox1.Text = text;
oleDbSelectCommand1.Parameters[0].Value = text;
dataSet11.Clear(); <<<--- call listbox1_SelectedIndexChanged
oleDbDataAdapter1.Fill(dataSet11);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
dataSet21.Clear();
}
why this happens and how i can to avoid it?
My psychic debugging skills tell me that the listbox is databound to the dataset.
When you clear your dataset, the listbox is emptied, and the selection changes.
This raises the relevant event.
If you have something selected in list box 1, when you clear it, the selected index will change, thus raising selection changed event.
I have 70 buttons whose names are like button1, button2, button3 and so on.
My aim is that whenever button1 is clicked, it will say "1", button2 will say as "2" and so on for the others.
The code for button1 to speak is:
SpeechSynthesizer synthesizer = new SpeechSynthesizer();
private void button1_Click(object sender, EventArgs e)
{
synthesizer.Speak("1");
}
For button2
private void button2_Click(object sender, EventArgs e)
{
synthesizer.Speak("2");
}
and so on for other 68 buttons.
Now it is difficult to implement the 70 button's actions. These button actions follow a pattern - so can anyone suggest a more efficient way I can implement these button handlers to save me writing out 70 different actions?
Try something like this
button1.Tag = "1";
button2.Tag = "2";
...
private void button_Click(object sender, EventArgs e)
{
synthesizer.Speak(((Button)sender).Tag.ToString());
}
Use same handler for all buttons. Sender of event will be the button which raised event. You can get it's name and extract text to say:
private void button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
string text = button.Name.Substring("button".Length);
synthesizer.Speak(text);
}
Create a single handler for all of the buttons.
Cast the sender parameter to get the Button instance that was clicked, and figure out what to do based on its Name, Text, or Tag.
Depending on your UI, you might want to generate all of those buttons in a loop, too.
You can subscribe them to a single eventhandler.
thus:
button1.Click += buttonClicked;
button2.Click += buttonClicked;
// and so on
and the code for the buttonClicked;
private void buttonClicked(object sender, EventArgs e)
{
//This will get the Type first, the name and then the last character on the Name
synthesizer.Speak(sender.GetType().Name.Substring(sender.GetType().Name.Length - 1, 1));
}
this promotes code reuse for you :)
Yesterday I try to implement a new listview that support sub-item edit, my solution is to show a textbox when double click the sub-item. The key code as following:
protected override void OnDoubleClick(EventArgs e)
{
Point pt = this.PointToClient(Cursor.Position);
ListViewItem curItem;
int subItemIndex = GetSubItemAt(pt.X, pt.Y, out curItem);
DoubleClickEventArgs args = new DoubleClickEventArgs(subItemIndex);
base.OnDoubleClick(args);
if (subItemIndex>=0 && !args.Cancel)
{
//StartEdit(...);
}
}
public void EndEdit(bool acceptChanges)
{
//validation
.................
.................
AfterSubItemEventArgs e = new AfterSubItemEventArgs(this.SelectedItems[0], m_editSubItemIndex, this.SelectedItems[0].SubItems[m_editSubItemIndex].Text, m_textbox.Text, false);
OnAfterSubItemEdit(e);
if (e.Cancel)
{
//....
}
else
{
//set new value
}
m_textbox.Visible = false;
m_editSubItemIndex = -1;
}
OnAfterSubItemEdit is a event that user can do some validations or other operations. I add a check in this method, if the new value exist, I will show a messagebox to user firstly, then hide the textbox. But now, the problem comes, when i move the mouse, the listview items can be selected, I don't how to solve this issue, I tried my best to find out the way, but failed. So, please help me!
Listview has a LabelEdit property; when you set it "true", then in an event handler you can call Listview.Items[x].BeginEdit(), and edit an item. As an example, you can handle ListView.DoubleClick event and call BeginEdit right there:
private void Form1_Load(object sender, System.EventArgs e)
{
listView1.LabelEdit = true;
}
private void listView1_DoubleClick(object sender, System.EventArgs e)
{
if(this.listView1.SelectedItems.Count==1)
{
this.listView1.SelectedItems[0].BeginEdit();
}
}
The problem is that your form still calls the DoubleClick event whether the value exists or not. Add appropriate condition before calling base DoubleClick in your code, i.e.:
if(!new value exists)
base.OnDoubleClick(args);