Listbox.SelectedIndex Question

ALWAYS returns -1, for no reason. Super frustrating

Page 1 of 1

3 Replies - 12388 Views - Last Post: 07 July 2007 - 01:45 PM Rate Topic: -----

#1 killnine  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 161
  • Joined: 12-February 07

Listbox.SelectedIndex Question

Posted 18 June 2007 - 07:27 AM

So I have the following source code. Now when I run this code, the value of listbox_param.SelectedIndex is ALWAYS -1. Wtf? Sure, I erase all of the values in the listbox in the initial part of the function, but I write them back right afterward. What cooky programming joo joo is this?

private void listbox_param_change(object sender, EventArgs e)
		{
			/*
			 * +++++++++++++++++++++
			 * ++++GET PARAMETER++++
			 * +++++++++++++++++++++ 
			 */

			listbox_param.Items.Clear();
			
			
			for (int i = 0; i < tg.GetNumParams(); i++)
			{

				string[] param_name  = new string[2];
				param_name			  = (string[])(Array)tg.GetParamName(i);
				listbox_param.Items.Add(param_name[1]);
				index++;
			}

			int index = listbox_param.SelectedIndex;

			int index_cells			 = 0;
			param_dim				 = (int[])(Array)tg.GetParamDims(1);
			int rows					  = param_dim[0];
			int cols					   = param_dim[1];

			double[] param_value = new double[rows * cols];
			param_value			  = (double[])(Array)tg.GetParam(index);
			
			
			
...
			  
			 

			

			
		}



Is This A Good Question/Topic? 0
  • +

Replies To: Listbox.SelectedIndex Question

#2 killnine  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 161
  • Joined: 12-February 07

Re: Listbox.SelectedIndex Question

Posted 18 June 2007 - 08:20 AM

To give a little more info,

when I put a breakpoint on the line

int index = listbox_param.SelectedIndex;

it actually runs through the whole condition TWICE the first time I open the program. The first time it runs through as -1 (screwing up all my arrays), and the second time it runs through with the CORRECT value. I modified my code to use if-statements to check if the index is -1, and return if the condition is true. This allows my other functions to work the second time around. Still, how stupid is this?

MODIFIED CODE:


index = listbox_param.SelectedIndex;	  
			
			if (index != -1)
			{
				param_dim = (int[])(Array)tg.GetParamDims(index); //use listbox_param.SelectedIndex when it starts working
			}

			if (index == -1)
			{
				return;
			}

			int index_cells	  = 0;
			int rows			 = param_dim[0];
			int cols			 = param_dim[1];

			double[] param_value = new double[rows * cols];
			param_value		  = (double[])(Array)tg.GetParam(index);			
			
			
			//PREPARE PARAMETER GRID FOR PARAMETER(i)
			grid1.Redim(0, 0);
			grid1.ColumnsCount = cols;
			grid1.RowsCount = rows;
			grid1.Rows.Insert(0);

			if (index != -1)
			{

				for (int j = 0; j < cols; j++)
				{
					for (int k = 0; k < rows; k++)
					{
						index_cells = ((rows * j) + k);
						grid1.Rows.Insert(k);
						grid1[k, j] = new SourceGrid.Cells.Cell(param_value[index_cells], typeof(double));
					}
				}
			}		
	 
		}


Was This Post Helpful? 0
  • +
  • -

#3 thyris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-June 07

Re: Listbox.SelectedIndex Question

Posted 19 June 2007 - 10:30 PM

I would imagine the code is firing twice because the function is sitting in SelectedIndexChanged event?
Was This Post Helpful? 0
  • +
  • -

#4 serializer  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 110
  • Joined: 25-June 07

Re: Listbox.SelectedIndex Question

Posted 07 July 2007 - 01:45 PM

Two problems spring to mind looking at your code:

- Your function is clearing all the listbox items out, before rpopulating it; then checking the value of SelectedIndex. It's possible that clearing the items could reset the SelectedIndex since you've just deleted whichever one was selected

- It's possible (I can't remember offhand & don't have the IDE installed on this machine) that listbox_param.SelectedIndex will only get set AFTER the onchange function has run. Instead the newly-selected index will be stored in a property of EventArgs e.

-- serializer
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1