Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / October 2005

Tip: Looking for answers? Try searching our database.

Selecting all in listbox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gav - 27 Oct 2005 11:16 GMT
Hi all,

I got some sample code from the following website:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fsystemwindowsformslistboxclasssortedtopic.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
John Scragg - 27 Oct 2005 15:40 GMT
Did you set your listbox's SelectionMode property to MultiExtended?

lbBranch.SelectionMode = SelectionMode.MultiExtended;

HTH,

John Scragg

> Hi all,
>
[quoted text clipped - 67 lines]
>
> Gav
Gav - 27 Oct 2005 16:14 GMT
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

> Did you set your listbox's SelectionMode property to MultiExtended?
>
[quoted text clipped - 77 lines]
>>
>> Gav
Gav - 27 Oct 2005 16:18 GMT
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

> Thanks for the reply John...
>
[quoted text clipped - 97 lines]
>>>
>>> Gav

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.