Selecting all in listbox

  • Thread starter Thread starter Gav
  • Start date Start date
G

Gav

Hi all,

I got some sample code from the following website:

http://msdn.microsoft.com/library/d...systemwindowsformslistboxclasssortedtopic.asp

This example is actually showing how to invert the selection on a listbox so
this is my code:

myda = new SqlDataAdapter(mycom);

dsRet = new DataSet();

try

{

conSQL.Open();

myda.Fill(dsRet, "BRANCHES");

}

catch(Exception err)

{

MessageBox.Show(err.Message);

}

finally

{

conSQL.Close();

}

lbBranch.ValueMember = "BRANCHID";

lbBranch.DisplayMember = "BRANCHNAME";

if (dsRet.Tables["BRANCHES"]!= null &&
dsRet.Tables["BRANCHES"].Rows.Count>0)

{

lbBranch.DataSource = dsRet.Tables["BRANCHES"].DefaultView;

for (int nCounter = 0;nCounter < lbBranch.Items.Count;nCounter++)

{

if (lbBranch.GetSelected(nCounter)== false)

lbBranch.SetSelected(nCounter, true);

}

}



For some reason this is not working I have stepped through the code and it
seems to be executing correctly (ie it skips the first item because it is
already selected and executes the statement for the rest of the items),
however it does not seem to be selecting the items in the listbox. Does
anybody have any ideas where I could be going wrong?



Thanks

Gav
 
Did you set your listbox's SelectionMode property to MultiExtended?

lbBranch.SelectionMode = SelectionMode.MultiExtended;

HTH,

John Scragg
 
Thanks for the reply John...

I had already set the SelectionMode to MultiExtended.

I have narrowed it down a little bit,

The problem seems to be that it is inside a tabpage(the page is not the
initial page), on load it selects all items in the listbox (which is
working!! I can see that by looking in the code before doing anything else)
however if I select the tabpage is loses my selection!!! Even if I click on
a particular item in the listbox if I change to a different tabpage and go
back again it loses my selection... is this a fault by Microsoft? It looks
like I will have to completely manage which items are selected by saving
them in an array and reselecting them when the tabpage is viewed.

Thanks
Gav

John Scragg said:
Did you set your listbox's SelectionMode property to MultiExtended?

lbBranch.SelectionMode = SelectionMode.MultiExtended;

HTH,

John Scragg


Gav said:
Hi all,

I got some sample code from the following website:

http://msdn.microsoft.com/library/d...systemwindowsformslistboxclasssortedtopic.asp

This example is actually showing how to invert the selection on a listbox
so
this is my code:

myda = new SqlDataAdapter(mycom);

dsRet = new DataSet();

try

{

conSQL.Open();

myda.Fill(dsRet, "BRANCHES");

}

catch(Exception err)

{

MessageBox.Show(err.Message);

}

finally

{

conSQL.Close();

}

lbBranch.ValueMember = "BRANCHID";

lbBranch.DisplayMember = "BRANCHNAME";

if (dsRet.Tables["BRANCHES"]!= null &&
dsRet.Tables["BRANCHES"].Rows.Count>0)

{

lbBranch.DataSource = dsRet.Tables["BRANCHES"].DefaultView;

for (int nCounter = 0;nCounter < lbBranch.Items.Count;nCounter++)

{

if (lbBranch.GetSelected(nCounter)== false)

lbBranch.SetSelected(nCounter, true);

}

}



For some reason this is not working I have stepped through the code and
it
seems to be executing correctly (ie it skips the first item because it is
already selected and executes the statement for the rest of the items),
however it does not seem to be selecting the items in the listbox. Does
anybody have any ideas where I could be going wrong?



Thanks

Gav
 
My mistake if you select one item in the listbox it keeps the selection when
switching between tabpages but if you select more than one it only keeps the
first one you selected.

Gav

Gav said:
Thanks for the reply John...

I had already set the SelectionMode to MultiExtended.

I have narrowed it down a little bit,

The problem seems to be that it is inside a tabpage(the page is not the
initial page), on load it selects all items in the listbox (which is
working!! I can see that by looking in the code before doing anything
else) however if I select the tabpage is loses my selection!!! Even if I
click on a particular item in the listbox if I change to a different
tabpage and go back again it loses my selection... is this a fault by
Microsoft? It looks like I will have to completely manage which items are
selected by saving them in an array and reselecting them when the tabpage
is viewed.

Thanks
Gav

John Scragg said:
Did you set your listbox's SelectionMode property to MultiExtended?

lbBranch.SelectionMode = SelectionMode.MultiExtended;

HTH,

John Scragg


Gav said:
Hi all,

I got some sample code from the following website:

http://msdn.microsoft.com/library/d...systemwindowsformslistboxclasssortedtopic.asp

This example is actually showing how to invert the selection on a
listbox so
this is my code:

myda = new SqlDataAdapter(mycom);

dsRet = new DataSet();

try

{

conSQL.Open();

myda.Fill(dsRet, "BRANCHES");

}

catch(Exception err)

{

MessageBox.Show(err.Message);

}

finally

{

conSQL.Close();

}

lbBranch.ValueMember = "BRANCHID";

lbBranch.DisplayMember = "BRANCHNAME";

if (dsRet.Tables["BRANCHES"]!= null &&
dsRet.Tables["BRANCHES"].Rows.Count>0)

{

lbBranch.DataSource = dsRet.Tables["BRANCHES"].DefaultView;

for (int nCounter = 0;nCounter < lbBranch.Items.Count;nCounter++)

{

if (lbBranch.GetSelected(nCounter)== false)

lbBranch.SetSelected(nCounter, true);

}

}



For some reason this is not working I have stepped through the code and
it
seems to be executing correctly (ie it skips the first item because it
is
already selected and executes the statement for the rest of the items),
however it does not seem to be selecting the items in the listbox. Does
anybody have any ideas where I could be going wrong?



Thanks

Gav
 
Back
Top