Wednesday, September 3, 2008

Solving The Double Click Problem

Solving The Double Click Problem

I alluded to this problem yesterday that I wanted if a user double clicks in a ListBox for it treat this the same as selecting a choice, then hitting the OK. I couldn't figure out how to make it happen. Here's the solution. This line adds the double click event handler:
box.DoubleClick += new EventHandler(this.listBoxDoubleClick);
The event handler:

private void listBoxDoubleClick(Object s, EventArgs e)
{
this.configDlg.DialogResult = DialogResult.OK;
this.configDlg.Close();
}
The configDlg is a Form created from within this class, setting DialogResult controls what gets returned to the ShowDialog() caller. The annoying gotcha of this is that to make configDlg visible in the event handler, I had to store in as a class level variable, rather than simply declare it in the function where I create the dialog window. My guess is that if the event handler was ever invoked while the dialog window didn't exist, this would cause an exception--but heck, I'm writing for fun right now. If someone wants to pay me to write C#, I would put in exception handling for this.

The this.configDlg.Close() method closes down the dialog window. When ShowDialog returns, the DialogResult.OK gets treated as though the user hit the OK button, and the selected values are set up correctly.

if (configDlg.ShowDialog(this) == DialogResult.OK)
{
System.Console.WriteLine("OK");
// Grab the selection list from the list box.
ListBox.SelectedIndexCollection selectedItems = box.SelectedIndices;
// Now go through and convert this into a list of ints.
listToShow.Clear();
for (int i = 0; i < numbersarraymax =" RecalculateMaxValue();">


I was especially pleased to find out that if I selected one item in the ListBox first, then double clicked on another item, both items came back in the SelectedIndices field.

UPDATE: Oh yes, the plot area also adjusts to the size of the overall window. If you resize the window, the plot area changes and redraws automatically.

No comments:

Post a Comment