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.
Related
I have a listbox that I am populating from a text file with this code..
(Works)
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
Listbox1.ItemsSource = File.ReadAllLines(#"c:\temp\servers.txt");
}
But when i try to populate a Combobox dropdown list the same way.. the list is empty.
(Doesnt work.. is blank combobox)
private void ComboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox2.ItemsSource = File.ReadAllLines(#"c:\temp\servers.txt");
}
thanks!
You're setting the value for the ComboBox in the Selection_Changed event handler. The code in this block will only fire if someone changes the selected value in the combo box - which will never happen because there isn't anything in it - hence your code isn't being run. You should put that code in the constructor for the page, or an OnNavigatedTo() method.
public YourPageName()
{
this.InitializeComponent();
ComboBox2.ItemsSource = File.ReadAllLines(#"c:\temp\servers.txt");
}
I have this code behind a winforms which simply has a listbox as its only control:
private void Form1_Load(object sender, EventArgs e)
{
listBox1.DataSource = dtv;
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "IDName";
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet dss = UseDatabase.FillDataSet("Select * From Table Where IDName=" + listBox1.SelectedValue);
string st = dss.Tables[0].Rows[0][0].ToString();
MessageBox.Show(st);
}
Run it say: "Additional information: There is no row at position 0."
Debug I see Event SelectedIndexChanged called when add one item.
Why user have not select item, that event is called
And how to fix this?
As far as I know the only way for you to fix this is by checking that SelectedIndex != -1 before doing anything else on the event handler method.
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());
}
I haven't found something that matches my problem so I ask it here. I have some code which belongs to a Textbox:
if ((sender as TextBox).Text == form1.filterType())
{
//Do something
}
This comes from the TextBox TextChanged event. So when the TextChanged Event gets fired it calls a method which has the if-construct above and recognizes that the textchanged event came from the textbox.
Now I want exactly the same just when someone writes into a cell in a DataGridView (not when it's just clicked - when the content changes).
How to do this correctly and which event fires when the content changes in a cell without leaving the cell?
I have found a solution for this:
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
}
void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
TextBox tb = (TextBox)e.Control;
//"unwire" the event before hooking it up ensures the event handler gets called only once
tb.TextChanged -= new EventHandler(tb_TextChanged);
tb.TextChanged += new EventHandler(tb_TextChanged);
}
}
void tb_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("changed");
}
Now it fires everytime the value in the cell gets changed - it behaves like a textbox.
Now - I still need a solution for the "if-construct" above. How to do this exactly? I've casted the cell into a textbox but now? Does this still comes from the dataGridview?
Once I had a similar problem an solved it this way (think there are better ones but it worked)
private void DataGridView1_onFocus ( Object sender, EventArgs e)
{
DataGridView1.onKeyPress+=DataGridView1_onKeyStroke;
}
private void DataGridView1_onFocusLost( Object sender, EventArgs e)
{
DataGridView1.onKeyPress-=DataGridView1_onKeyStroke;
}
private void DataGridView1_onKeyStroke( Object sender , EventArgs e)
{
//Do your thing
}
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;
}
}