mardi 10 mars 2015

Prevent Listbox selecting item at the very beginning

I have a problem that has a very bad effect on the runtime of my graphical user interface. I'm Initializing my Form1 (when the GUI opens) with:



InitializeComponent();

var fill_listboxes = new FillListBoxes(this);
fill_listboxes.fillFromDatabase();


So he opens the function to fill the Listboxes with values from a database (one listbox per column). The function itself fills the boxes this way:



datTable = new DataTable();
sqlCmd = new SqlCommand(@"SELECT DISTINCT [" + form1.getCol1() + "] FROM [" + form1.getTableName() + "]", connection);
sqlDatAdapter = new SqlDataAdapter(sqlCmd.CommandText, connection);
sqlDatAdapter.Fill(datTable);

form1.listbox1.DisplayMember = form1.getCol1();
form1.listbox1.ValueMember = "Column1";
form1.listbox1.DataSource = datTable;


I'm setting the Listbox selectedIndex to "-1" after this function and I tried to put this right after the last line and right before it but every time he reaches the last line and sets the data source to the data table he automatically jumps into the "selectedIndexChanged" method of the listbox and selects the first value of it. That causes bad runtime problems. Is it possible to prevent him selecting a value from the Listbox (or setting it to -1 before selecting something) ?


Aucun commentaire:

Enregistrer un commentaire