S
Sean Wolfe
I have a winform that is used for setting configuration parameters for
an application, and stores the settings to a file so it can be read when
the application starts. You starndard properties window.
One of the settings is for Logging and the level of logging that wishes
to be captured. In our application, the logging levels is provided by
BitFlag Enum. So I wished to display the selected bitflags inside a
Multi-select listbox, which people could click on the levels that they
want available. I got this working fine for saving and loading. But
there is a particularly peculiar problem i ran into with the ListBox
control.
Our enum has 6 levels of severity, and has 3 helper levels (All,
Default, None). Here is the code example of the Enum.
[Flags]
public enum LogMessageSeverity
{
None = 0,
Debug = 1,
Notification = 2,
Warning = 4,
Error = 8,
Critical = 16,
Fatal = 32,
Default = (Warning | Error | Critical | Fatal ),
All = (Debug | Notification | Default)
}
In the listbox, to avoid confusion with users, I only have the 6
severity levels. Each one can be selected individually. But what happens
is when the user selects Fatal + Critical + Error + Warning, the enum ==
Default. And if the user selects all 6 then the enum == All. This is
fine so I account for that in the code.
The problem occurs that when I load up the settings form, it will try to
select the appropriate values in the select box that have been saved in
the settings file. When it loads items that are selected individually,
it works. But for an item that has an enumeration == All, I tried to use
a for loop that uses the SetSelected() method, and when I do, the items
don't show up selected on the form. The thing that is bizzare about
this, is that if I run it in the debugger and step through it, it will
display it successfully, but when it runs without stepping through it
won't. I've tied calling the Update() method and DoEvents, and these
don't seem to help. I ended up having to select each item explicitly.
Here is an excerpt from the code showing this behavior.
-------------------------------------------------------------
// early in the code
ListBox logfileSeverity;
this.logfileSeverity = new System.Windows.Forms.ListBox();
this.logfileSeverity.Items.AddRange(new object[] {
"Fatal",
"Critical",
"Error",
"Warning",
"Notification",
"Debug"});
this.logfileSeverity.Location = new System.Drawing.Point(160, 48);
this.logfileSeverity.Name = "logfileSeverity";
this.logfileSeverity.SelectionMode =
System.Windows.Forms.SelectionMode.MultiSimple;
this.logfileSeverity.Size = new System.Drawing.Size(104, 82);
this.logfileSeverity.TabIndex = 32;
// where the code has the problems.
// TrayParameters.LogfileSeverity is a
// LogMessageSeverity enum.
string logSeverityFlags = TrayParameters.LogfileSeverity.ToString("g");
// if the enum is individual flags it works.
// if the enum is "All" or "Default" the selections do not work.
foreach(string sev in logSeverityFlags.Split(new char[] {','}))
{
if(sev == "All")
{
for(int itemIndex = 0; itemIndex < 6; itemIndex++)
{
// this doesn't seem to actually set the list items.
this.logfileSeverity.SetSelected(itemIndex, true);
}
break;
}
else if(sev == "Default")
{
for(int itemIndex = 0; itemIndex < 4; itemIndex++)
{
// this doesn't seem to actually set the list items.
this.logfileSeverity.SetSelected(itemIndex, true);
}
}
else
this.logfileSeverity.SelectedItem = sev.Trim();
}
// I had to change the code block above to as follows.
foreach(string sev in logSeverityFlags.Split(new char[] {','}))
{
if(sev == "All")
{
this.messageSeverity.SelectedItem = "Fatal";
this.messageSeverity.SelectedItem = "Critical";
this.messageSeverity.SelectedItem = "Error";
this.messageSeverity.SelectedItem = "Warning";
this.messageSeverity.SelectedItem = "Notification";
this.messageSeverity.SelectedItem = "Debug";
break;
}
else if(sev == "Default")
{
this.messageSeverity.SelectedItem = "Fatal";
this.messageSeverity.SelectedItem = "Critical";
this.messageSeverity.SelectedItem = "Error";
this.messageSeverity.SelectedItem = "Warning";
}
else
this.logfileSeverity.SelectedItem = sev.Trim();
}
---------------------------------------------------------
Does anyone have any ideas as to why this happens?
sean
an application, and stores the settings to a file so it can be read when
the application starts. You starndard properties window.
One of the settings is for Logging and the level of logging that wishes
to be captured. In our application, the logging levels is provided by
BitFlag Enum. So I wished to display the selected bitflags inside a
Multi-select listbox, which people could click on the levels that they
want available. I got this working fine for saving and loading. But
there is a particularly peculiar problem i ran into with the ListBox
control.
Our enum has 6 levels of severity, and has 3 helper levels (All,
Default, None). Here is the code example of the Enum.
[Flags]
public enum LogMessageSeverity
{
None = 0,
Debug = 1,
Notification = 2,
Warning = 4,
Error = 8,
Critical = 16,
Fatal = 32,
Default = (Warning | Error | Critical | Fatal ),
All = (Debug | Notification | Default)
}
In the listbox, to avoid confusion with users, I only have the 6
severity levels. Each one can be selected individually. But what happens
is when the user selects Fatal + Critical + Error + Warning, the enum ==
Default. And if the user selects all 6 then the enum == All. This is
fine so I account for that in the code.
The problem occurs that when I load up the settings form, it will try to
select the appropriate values in the select box that have been saved in
the settings file. When it loads items that are selected individually,
it works. But for an item that has an enumeration == All, I tried to use
a for loop that uses the SetSelected() method, and when I do, the items
don't show up selected on the form. The thing that is bizzare about
this, is that if I run it in the debugger and step through it, it will
display it successfully, but when it runs without stepping through it
won't. I've tied calling the Update() method and DoEvents, and these
don't seem to help. I ended up having to select each item explicitly.
Here is an excerpt from the code showing this behavior.
-------------------------------------------------------------
// early in the code
ListBox logfileSeverity;
this.logfileSeverity = new System.Windows.Forms.ListBox();
this.logfileSeverity.Items.AddRange(new object[] {
"Fatal",
"Critical",
"Error",
"Warning",
"Notification",
"Debug"});
this.logfileSeverity.Location = new System.Drawing.Point(160, 48);
this.logfileSeverity.Name = "logfileSeverity";
this.logfileSeverity.SelectionMode =
System.Windows.Forms.SelectionMode.MultiSimple;
this.logfileSeverity.Size = new System.Drawing.Size(104, 82);
this.logfileSeverity.TabIndex = 32;
// where the code has the problems.
// TrayParameters.LogfileSeverity is a
// LogMessageSeverity enum.
string logSeverityFlags = TrayParameters.LogfileSeverity.ToString("g");
// if the enum is individual flags it works.
// if the enum is "All" or "Default" the selections do not work.
foreach(string sev in logSeverityFlags.Split(new char[] {','}))
{
if(sev == "All")
{
for(int itemIndex = 0; itemIndex < 6; itemIndex++)
{
// this doesn't seem to actually set the list items.
this.logfileSeverity.SetSelected(itemIndex, true);
}
break;
}
else if(sev == "Default")
{
for(int itemIndex = 0; itemIndex < 4; itemIndex++)
{
// this doesn't seem to actually set the list items.
this.logfileSeverity.SetSelected(itemIndex, true);
}
}
else
this.logfileSeverity.SelectedItem = sev.Trim();
}
// I had to change the code block above to as follows.
foreach(string sev in logSeverityFlags.Split(new char[] {','}))
{
if(sev == "All")
{
this.messageSeverity.SelectedItem = "Fatal";
this.messageSeverity.SelectedItem = "Critical";
this.messageSeverity.SelectedItem = "Error";
this.messageSeverity.SelectedItem = "Warning";
this.messageSeverity.SelectedItem = "Notification";
this.messageSeverity.SelectedItem = "Debug";
break;
}
else if(sev == "Default")
{
this.messageSeverity.SelectedItem = "Fatal";
this.messageSeverity.SelectedItem = "Critical";
this.messageSeverity.SelectedItem = "Error";
this.messageSeverity.SelectedItem = "Warning";
}
else
this.logfileSeverity.SelectedItem = sev.Trim();
}
---------------------------------------------------------
Does anyone have any ideas as to why this happens?
sean