Selected index changed fail into ListBox - c#

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..

Related

How can I manage overlapping DropDownBox drawers?

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;
}

C# editable listview

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);

Do I need to trigger an event for the bind with Combox works properly?

I have an issue with a ComboBox
private void Form_AddGoal_Load(object sender, EventArgs e)
{
LoadGoal();
IList<Perspective> perspectives = PerspectiveRepository.All(); // I get all perspectives
CBPerspective.DataSource = perspectives;
CBPerspective.DisplayMember = "Name";
// Here I initialize other components
}
private void LoadGoal()
{
if (Goal== null)
Goal = new Goal();
// Here I bind other components
CBPerspective.DataBindings.Add("SelectedItem", Goal, "Perspective");
}
public void SaveBtn_Click(object sender, EventArgs e)
{
// I save the Goal
}
When the form is open, all is ok. If I don't choose any option in the combobox (That is, the first option keep it) and I save the data on the form the Perspective property is null but If I choose other option in the combobox and proceed in the same way, perspective is equal to the selected item so works perfectly.
What is going on? Do I need trigger an event?

How to get ListBox to load after selecting ComboBox value?

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
}

In the WinForms DevExpress XtraWizard control, how can one hide the back button?

I know that if I set the WizardStyle of an XtraWizard control to WizardAero, it will remove or hide the back button from the first page instead of simply disabling it, as can be seen here. I want the same behaviour, but I want to keep the WizardStyle as Wizard97.
Is this possible, and if so, how?
One way to do this would be to use the CustomizeCommandButtons event on the WizardControl.
private void wizardControl1_CustomizeCommandButtons(object sender, CustomizeCommandButtonsEventArgs e)
{
e.PrevButton.Visible = false;
}
If you only want to hide it on the first page
private void wizardControl1_CustomizeCommandButtons(object sender, CustomizeCommandButtonsEventArgs e)
{
if(wizardControl1.SelectedPageIndex == 0)
e.PrevButton.Visible = false;
}
It seems like it will reset the visibility each time (so you don't need to toggle it back on). Anyway, I think this is what you're looking for.
http://documentation.devexpress.com/#WindowsForms/DevExpressXtraWizardWizardControl_CustomizeCommandButtonstopic
You can also set your own fields to the wizard buttons and then use these anywhere in your code. This will, for example, allow you to hide/disable the "Next" button until all fields page have been completed.
private void NodeConfigurationWizardCustomizeCommandButtons(object sender, CustomizeCommandButtonsEventArgs e)
{
_nextButton = e.NextButton;}
private void GetRowsButtonClick(object sender, EventArgs e)
{
var rowList = ServiceClient.GetAvailableRows();
var rowsReturned = rowList.Count > 0;
_nextButton.Button.Enabled = rowsReturned ;}

Categories