• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C# Listbox programming

I have a listbox in a form. When the application is run, the user can press a button that adds items to the listbox like this:

private void UpdateRuleList()
{
this.listRules.Items.Clear();
ScriptAction scr;
int i = scripts.scripts.Count;
for (int j = 0; j < i; j++)
{
scr = (ScriptAction)scripts.scripts[j];
this.listRules.Items.Add(scr.RuleName);
}
}

Then I attached the following code to the listbox:
private void listRules_SelectedIndexChanged(object sender, EventArgs e)
{
if (listRules.SelectedIndex != -1)
LoadScript(listRules.Items[listRules.SelectedIndex].ToString());
}

When the program is run, if I click on an item in the listbox, then Loadscript is called, but the highlighting on the item I clicked on in the listbox disapears. If I comment out the call to Loadscript, then the highlighting stays, but I need to call it so that it can update the values of other controls in the window.

I tried calling listRules.Focus(); after the LoadScript, because I thought the control lost focus for some reason, but that didn't change anything. How can I get the highlighting to stay?
 
Save the selected item (by whatever is a unique ID for it in your app) in the actual Loadscript function, and restore it at the end of the function.
 
Thanks dandragonrage.

I changed the function to:
private void listRules_SelectedIndexChanged(object sender, EventArgs e)
{
int i = listRules.SelectedIndex;
if (listRules.SelectedIndex != -1)
LoadScript(listRules.Items[listRules.SelectedIndex].ToString());
listRules.SelectedIndex = i;
}

and now it works. But I'm confused as to why I have to do this since LoadScript does not affect the listRules listbox in any way. It only modifies other controls in the form.
 
You don't show us the LoadScript function, so we can't see what it does. You do show an UpdateRuleList function, and that clears the items collection. Post the contents of LoadScript and perhaps we can see what's going on. Does it call UpdateRuleList?
 
I didn't post it because it's way too long (over 1000 lines of code). It doesn't directly call UpdateRuleList, it only deals with other controls in the form.

Don't worry about it though, I figured it out. It turns out that when LoadScript is called and it changes one of the other controls, that control had an event that was called which called UpdateRuleList.

What I ended up doing was create a global variable which keeps track of if LoadScript is running and then in every control's events I check the variable to see if the LoadScript was running. If it was, then I didn't execute the event.

Works great. :thumbsup:
 
Back
Top