C# combobox selection feeding another combobox - c#

using mysql I feed combobox2 with selection of combobox1.It works OK. but the problem is the first select seems does not trigger the eventhandler. the second time I do it it will trigger.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.SelectedValueChanged += new EventHandler(comboBox1_selectedvaluechanged);
}
private void comboBox1_selectedvaluechanged(object sender, EventArgs e)
{
region = comboBox1.SelectedItem.ToString();
values_to_venue();
db.connection.Close();
}

It's because you're not creating your event handler until the SelectedIndex of the combobox changes. This is fired alongside SelectedValue changes. Create the event handler in the load method ensuring it is there when the first SelectedValue changes. If you put it in the Load make sure you clean it up with -= in the unload. Or, you can just create it in the constructor and then you won't need to remove it.

You do want to set the events in your constructor or load method; also, I think the event you want to use is ComboBox.SelectionChangeCommitted because if the user navigates the DropDown list using the up/down arrows on the keypad, SelectedIndexChanged will fire before the selection is committed - is that what you want??????

Related

Combobox selected index changed event not fired in code

I am working on a Windows Forms application and I have a combobox named cmbCountry. I am binding this combobox to a list which contains names of countries. Following is the code to populate the combobox.
cmbCountry.DataSource = lstcountry;
Next I want to set selected item as "United States of America". so I added the following code
cmbCountry.SelectedItem="United States of America";
I want to do some code on selection change event of this combobox.
private void cmbCountry_SelectionChangeCommitted(object sender, EventArgs e)
{
\\some code
}
This method is suppose to be call when I set the selected item. But it is not getting called. However when I select "United States of America" from UI part(Design Part) this event getting called. I want to get called this event when I set the selected item.
SelectionChangeCommitted fires when the user manipulates via the UI.
SelectionChangeCommitted is raised only when the user changes the
combo box selection. Do not use SelectedIndexChanged or
SelectedValueChanged to capture user changes, because those events are
also raised when the selection changes programmatically.
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectionchangecommitted.aspx
Use SelectedIndexChanged or SelectedValueChanged
Change your event to SelectedIndexChanged:
private void cmbCountry_SelectedIndexChanged(object sender, EventArgs e)
{
\\some code
}
And change the event handler (which may be automatically generated):
this.cmbCountry.SelectedIndexChanged += new System.EventHandler(this.lstResults_SelectedIndexChanged);

Get selected row in DataGridView

ALL,
I have a DataGridView control on my WinForms application with the selection property as "Entire Row" and no multi-selection. I also attach the SelectionChanged delegate to it, where I need to get the currently selected row.
private void order_SelectionChanged(object sender, EventArgs e)
{
ordersItemIndex = order.SelectedRows[0].Index;
}
Problem is, when the program starts, there should be no selection at all and only later user can change the selection with mouse or keyboard. So in my Form_Load() event I have this:
order.ClearSelection();
However, this code path throws an exception on the start-up of the program.
Is there a nice way to tell the program "We are loading the form, don't call the delegate", without any additional variable?
Thank you.
You can add your order_SelectionChanged after you clear selection in Form_Load() (not in InitializeComponents())
But better check in your handler is there any selected rows.
private void order_SelectionChanged(object sender, EventArgs e)
{
if (order.SelectedRows.Count > 0)
ordersItemIndex = order.SelectedRows[0].Index;
}

Detecting a Mouse click on selecting a row in dataGridView

I am having trouble getting my application to work correctly. I am trying to select a row in a datagridview with the mouse. I need to save the index of this row to allow me to navigate around the selected row.
I have been looking at DataGridView.CellMouseClick Event (Link) But I am unable to ensure that the event handler is associated with the CellMouseClick event.
My code for this so far is simple, Im just trying to see if its detecting mouse clicks:
public event DataGridViewCellMouseEventHandler CellMouseClick;
private void DataGridView1_CellMouseClick(Object sender, DataGridViewCellMouseEventArgs e)
{
MessageBox.Show("Mouse clicked in the datagridview!");
}
Can anyone point out where I may be going wrong. Any help would be great!
You need to "wire up" the event.
If your DataGridView is called DataGridView1 then you need the following line of code in either the constructor for your form, the designer (if you add the event handler via the designer) or in the Load event:
DataGridView1.CellMouseClick += DataGridView1_CellMouseClick;
This attaches the event handler in your code to the event.
Your current sample looks like this:
public event DataGridViewCellMouseEventHandler CellMouseClick;
private void DataGridView1_CellMouseClick(Object sender, DataGridViewCellMouseEventArgs e)
{
MessageBox.Show("Mouse clicked in the datagridview!");
}
There is no need to redeclare the event (public event DataGridViewCellMouseEventHandler CellMouseClick;) unless you are building your own user control that will host a DataGridView and you effectively want to "wrap" or "rebroadcast" that event.

How to trigger an event when any of CheckState values in CheckListBox is changed

I have a CheckListBox in c# and I am trying to trigger an event whenever one of the checkstates in the box is changed. The event purpose is to change some RichTextBox.
I have this piece of code, but it triggers the event only when one of the check boxes is turning from checked to unchecked, for some reason.
I tried to figure out what is wrong with my code with no success.
Any help will be appreciated.
private void clbAllRooms_ItemCheck(object sender, ItemCheckEventArgs e)
{
//If the checkstate changed, update price
//It updates price only when the state turns from Checked to Uncheck
if (e.NewValue != e.CurrentValue)
Update_rtbPrice();
}
The trouble is no doubt located in your Update_rtbPrice() method. It would have to call the list box' GetItemChecked() method to do something meaningful and that's a problem when you make the method call from the event handler. The item check state doesn't change until after the event runs.
A workaround is to delay the call so it runs after the control's state is updated. Like this:
private void clbAllRooms_ItemCheck(object sender, ItemCheckEventArgs e) {
this.BeginInvoke(new MethodInvoker(() => Update_rtbPrice()));
}

Auto activate an item on select in a ListView

Is it possible that in a ListView whenever an Item has been selected (e.g mouse 1 left click or Key down or key up, left right) that item gets activated (Like when you hit enter or double click!). What event or properties of ListView will do this, if any?
UPDATE
I found out it will work if ListView.Activation is set to OneClick but thi is only for the mouse, I want the same with keyboard arrows too.
You can do that by listening the the ItemSelectionChanged event and simply calling the code that runs whenever an item is double-clicked.
Alternatively you can call ItemActivate event that fires when an item is double-clicked using this bit of code, although I'd recommend the first method:
private void ListView1_ItemSelectionChanged(Object sender, ListViewItemSelectionChangedEventArgs e)
{
ListView1_ItemActivate(sender, e);
}
You, have mouseclick and mousedoubleclick events in the listview control for this purpose.
Implement the following events with whatever you want your listview to do.
KeyDown , KeyUp, KeyPress, MouseDown, MouseUp, MousePress, MouseHover, MouseEnter, MouseClick, MouseDoubleClick
those are only a few select examples.
Lets say your listview is named listView1
to Subscribe to one of the events, do this
private void Form1_Load(object sender, EventArgs e)
{
listView1.KeyDown += new KeyEventHandler(listView1_KeyDown);
}
void listView1_KeyDown(object sender, KeyEventArgs e)
{
throw new NotImplementedException();
}
Enter your content at the throw statement

Categories

Resources