Working in VS 2012, WinForms, C#...
I have a ListBox I would like to populate depending upon the value selected in a ComboBox. I've tested my SQL Query and it works, but I'm getting a weird problem where, when I run my routines, my ComboBox comes up empty, as well as my ListBox. When I comment out the code in my cb_Session_SelectedValueChanged routine, my CB and LB load just fine, but when it's not commented out is when my LB and CB end up blank.
This is what I have:
private void cb_Session_SelectedValueChanged(object sender, EventArgs e)
{
listbox_Sessions.Visible = true;
LoadSessionListbox();
}
private void LoadSessionListbox()
{
int tempID = Convert.ToInt32(cb_Session.SelectedValue);
// Code here to load listbox, which works without above routine.
}
Am I missing something? Why are my CB and LB blank with that first routine added?
[EDIT]:
I put the routines which were in SelectedValueChanged in a MouseClick event and it works, but not when I want it to... You have to click a couple times to get it to re-load with the correct ID. I feel like I'm getting closer, but still not the right event.
Try this:
private void cb_Session_SelectedValueChanged(object sender, EventArgs e)
{
if(cb_Session.SelectedValue>-1)
{
listbox_Sessions.Visible = true;
LoadSessionListbox();
}
}
Figured it out!!
I ended up adding a simple if statement to my SelectedValueChanged routine, and it fixed everything!
private void cb_Sessions_SelectedValueChanged(object sender, EventArgs e)
{
listBox_Sessions.Visible = true;
if (cb_Sessions.SelectedValue != null)
LoadSessionListbox();
}
Works perfectly now.
Try in SelectedIndexChanged Event and follow
private void cb_Session_SelectedIndexChanged(object sender, EventArgs e)
{
if (cb_Session.SelectedValue == null) return;
if (cb_Session.SelectedIndex == -1) return;
listbox_Sessions.Visible = true;
LoadSessionListbox((int)cb_Session.SelectedValue);
}
private void LoadSessionListbox(int selectedValue)
{
//TODO: Do stuff
}
Related
I created a windows forms like this
As you can see in selected changed event I disable button move to, it works correctly, problem starts when I try to
return an item it to main list, move to button keeps disable instead enable it again when I select item of first list. Someone knows
what occurs there?
Events:
private void lstTechUnnotified_SelectedIndexChanged(object sender, EventArgs e)
{
btnReturnTo.Enabled = false;
btnMoveTo.Enabled = true;
}
private void lstTechToNotified_SelectedIndexChanged(object sender, EventArgs e)
{
btnReturnTo.Enabled = true;
btnMoveTo.Enabled = false;
}
You need to make sure that there actually is an item being selected since ListBox.SelectedIndexChanged event gets fired even when there're no items selected - making the new SelectedIndex equal to -1. Also, from the way you asking, I expect you want to enable btnMoveTo when there's a selected item in lstTechUnnotified and otherwise, disable it - and the same for btnReturnTo and lstTechToNotified; if that's it, then the easy way is:
private void lstTechUnnotified_SelectedIndexChanged(object sender, EventArgs e)
{
btnMoveTo.Enabled = (lstTechUnnotified.SelectedIndex > -1);
}
private void lstTechToNotified_SelectedIndexChanged(object sender, EventArgs e)
{
btnReturnTo.Enabled = (lstTechToNotified.SelectedIndex > -1);
}
Though I'm not sure about your button names..
So, I have one GridView and 2 DataSources created using "wizard" ( GridViewTasks-->NewDataSource)
I have one CheckBox on my page...when checked=true I want to use DataSource1 for my GridView, and when checked=false DataSource2.
I have tried add CheckBox_CheckedChanged event in my code-behind something like this:
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox.Checked == true)
{
gvPredbiljezbe.DataSource = dsGridView1;
gvPredbiljezbe.DataBind();
}
else
{
gvPredbiljezbe.DataSource = dsGridView2;
gvPredbiljezbe.DataBind();
}
}
But this doesn't work.
Any suggestions?
I know I can go in my code-behind and do it all "manually" (SqlDataConnection->DataAdapter->DataTable->GridViewDataSource) but is there a way when you create your DataSources with GridView wizard and on CheckBox or ButtonClick event change your GridView's DataSource's?
Thanks
Best
K
Make sure to set AutoPostBack="true" for your checkbox.
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkbox = (CheckBox)sender;
if (checkbox != null)
gvPredbiljezbe.DataSourceID = checkbox.Checked ? dsGridView1.ID : dsGridView2.ID;
}
I used to use this technique; don't assign the DatasourceID, then set it in page_init for the initial data load, then do:
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox.Checked == true)
{
gvPredbiljezbe.DataSourceID = dsGridView1.ID;
gvPredbiljezbe.DataBind();
}
else
{
gvPredbiljezbe.DataSourceID = dsGridView2.ID;
gvPredbiljezbe.DataBind();
}
}
Something like that; swapping the ID of the datasource worked without issue (been a little while so I think you use ID, not ClientID property).
Most of my dropdown boxes use the SuggestAppend property, meaning when you start typing in the box, it will make a shortlist of the items that match your case. However, if I do this after opening the drawer, this happens:
I have tried using this method, but it closes both instead of just one:
private void cmbLoc_TextChanged(object sender, EventArgs e)
{
if (cmbLoc.Text != "")
{
cmbLoc.DroppedDown = false;
}
}
I am trying to have it so that when I type something into the text box, the original dropdown will disappear, and the SuggestAppend draw will appear. How can I manage this?
It worked if I used KeyDown. Try and tell if that helps
private void cmbLoc_KeyDown(object sender, KeyEventArgs e)
{
var comboBox = (ComboBox)sender;
comboBox.DroppedDown = false;
}
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);
I have a dropdown list and radio button. If something is selected from the dropdown by the user, I want the radio button cleared. If the radio button is selected I want the selection of the dropdown cleared. Unfortunately, this creates events that cancel each other out. I tried using the sender as shown below to determine if the value was being changed by code or by the user, but that doesn't work. How do I make these events only work if the user is the source of the action?
private void rbBlank_Checked(object sender, RoutedEventArgs e)
{
// Verify source of event
if (sender is RadioButton)
{
// Display
comboBoxTitles.SelectedIndex = -1;
}
}
private void comboBoxTitles_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
// Verify source of event
if (sender is ComboBox)
{
// Display
rbBlank.IsChecked = false;
}
}
You won't be able to tell the difference between the two since the source will be the same instance for both occasions.
This doesn't answer the question directly but if you compare the SelectedIndex of comboBoxTitles in the SelectionChanged event handler, your problem should be solved
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (comboBoxTitles.SelectedIndex != -1)
{
rbBlank.IsChecked = false;
}
}
Try to compare if sender == instance of a control instead of is type of.
private void rbBlank_Checked(object sender, RoutedEventArgs e)
{
// Verify source of event
if (sender == rbBlank)
{
// Display
comboBoxTitles.SelectedIndex = -1;
}
}
private void comboBoxTitles_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
// Verify source of event
if (sender == comboBoxTitles)
{
// Display
rbBlank.IsChecked = false;
}
}
If you know the IDs of those controls, you can try something like this:
System.Web.UI.WebControls.WebControl webControl = (System.Web.UI.WebControls.WebControl) sender;
if( webControl.ID == <comboboxId>)
{
//Do something
}
I havent tried this, but I guess it might work.