469,290 Members | 1,806 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

how to get count of items in listbox?

Hi, How can I set all the items in a listbox to be selected? I can't find a
property or mehtod to do it so I thought I'll try using setselected method
but I need to find out how many items are in the listbox.

Thanks,
Alpha
Nov 17 '05 #1
9 44206
Something similar to the following code should do it.

for (int x = 0; x < this.listBox1.Items.Count; x++)
{
this.listBox1.SetSelected(x, true);
}

--
Tim Wilson
..Net Compact Framework MVP

"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com...
Hi, How can I set all the items in a listbox to be selected? I can't find a property or mehtod to do it so I thought I'll try using setselected method
but I need to find out how many items are in the listbox.

Thanks,
Alpha

Nov 17 '05 #2
Very cool! Thank you so much again.
Have a great weekend!
Alpha

"Tim Wilson" wrote:
Something similar to the following code should do it.

for (int x = 0; x < this.listBox1.Items.Count; x++)
{
this.listBox1.SetSelected(x, true);
}

--
Tim Wilson
..Net Compact Framework MVP

"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com...
Hi, How can I set all the items in a listbox to be selected? I can't find

a
property or mehtod to do it so I thought I'll try using setselected method
but I need to find out how many items are in the listbox.

Thanks,
Alpha


Nov 17 '05 #3
The method above is good for a small number of items in the listbox, but if
you have many. (100,000) it is either too slow or doesn't work. Is there
any other way to accomplish the same thing, but may be faster.

TIA,

Fritz
"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
Very cool! Thank you so much again.
Have a great weekend!
Alpha

"Tim Wilson" wrote:
Something similar to the following code should do it.

for (int x = 0; x < this.listBox1.Items.Count; x++)
{
this.listBox1.SetSelected(x, true);
}

--
Tim Wilson
..Net Compact Framework MVP

"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com...
> Hi, How can I set all the items in a listbox to be selected? I can't
> find

a
> property or mehtod to do it so I thought I'll try using setselected
> method
> but I need to find out how many items are in the listbox.
>
> Thanks,
> Alpha


Nov 17 '05 #4
Try the following code.

this.listBox1.SelectionMode = SelectionMode.MultiExtended;

....

if (this.listBox1.Items.Count > 0)
{
this.listBox1.Focus();
this.listBox1.SetSelected(0, true);
SendKeys.Send("+{END}");
}

--
Tim Wilson
..Net Compact Framework MVP

"Fritz Switzer" <fr***********@abletfactory.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
The method above is good for a small number of items in the listbox, but if you have many. (100,000) it is either too slow or doesn't work. Is there
any other way to accomplish the same thing, but may be faster.

TIA,

Fritz
"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
Very cool! Thank you so much again.
Have a great weekend!
Alpha

"Tim Wilson" wrote:
Something similar to the following code should do it.

for (int x = 0; x < this.listBox1.Items.Count; x++)
{
this.listBox1.SetSelected(x, true);
}

--
Tim Wilson
..Net Compact Framework MVP

"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com...
> Hi, How can I set all the items in a listbox to be selected? I can't
> find
a
> property or mehtod to do it so I thought I'll try using setselected
> method
> but I need to find out how many items are in the listbox.
>
> Thanks,
> Alpha


Nov 17 '05 #5
Tim,

Thanks, that was about "a gazillion" times faster. :)
Fritz

"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message
news:OH**************@TK2MSFTNGP14.phx.gbl...
Try the following code.

this.listBox1.SelectionMode = SelectionMode.MultiExtended;

...

if (this.listBox1.Items.Count > 0)
{
this.listBox1.Focus();
this.listBox1.SetSelected(0, true);
SendKeys.Send("+{END}");
}

--
Tim Wilson
.Net Compact Framework MVP

"Fritz Switzer" <fr***********@abletfactory.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
The method above is good for a small number of items in the listbox, but

if
you have many. (100,000) it is either too slow or doesn't work. Is there
any other way to accomplish the same thing, but may be faster.

TIA,

Fritz
"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
> Very cool! Thank you so much again.
> Have a great weekend!
> Alpha
>
> "Tim Wilson" wrote:
>
>> Something similar to the following code should do it.
>>
>> for (int x = 0; x < this.listBox1.Items.Count; x++)
>> {
>> this.listBox1.SetSelected(x, true);
>> }
>>
>> --
>> Tim Wilson
>> ..Net Compact Framework MVP
>>
>> "Alpha" <Al***@discussions.microsoft.com> wrote in message
>> news:82**********************************@microsof t.com...
>> > Hi, How can I set all the items in a listbox to be selected? I
>> > can't
>> > find
>> a
>> > property or mehtod to do it so I thought I'll try using setselected
>> > method
>> > but I need to find out how many items are in the listbox.
>> >
>> > Thanks,
>> > Alpha
>>
>>
>>



Nov 17 '05 #6
PP
Hi Tim

I tried this code, but doesn't seem to work. It does not SELECT all item(s),
it selects only the first item.

private void Form1_Load(object sender, System.EventArgs e)
{
listBox1.Items.Add ("Item-1") ;
listBox1.Items.Add ("Item-2") ;
listBox1.Items.Add ("Item-3") ;
listBox1.Items.Add ("Item-4") ;
listBox1.Items.Add ("Item-5") ;

this.listBox1.SelectionMode = SelectionMode.MultiExtended;
if (this.listBox1.Items.Count > 0)
{
this.listBox1.Focus();
this.listBox1.SetSelected(0, true);
SendKeys.Send("+{END}");
}
}


"Tim Wilson" wrote:
Try the following code.

this.listBox1.SelectionMode = SelectionMode.MultiExtended;

....

if (this.listBox1.Items.Count > 0)
{
this.listBox1.Focus();
this.listBox1.SetSelected(0, true);
SendKeys.Send("+{END}");
}

--
Tim Wilson
..Net Compact Framework MVP

"Fritz Switzer" <fr***********@abletfactory.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
The method above is good for a small number of items in the listbox, but

if
you have many. (100,000) it is either too slow or doesn't work. Is there
any other way to accomplish the same thing, but may be faster.

TIA,

Fritz
"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
Very cool! Thank you so much again.
Have a great weekend!
Alpha

"Tim Wilson" wrote:

> Something similar to the following code should do it.
>
> for (int x = 0; x < this.listBox1.Items.Count; x++)
> {
> this.listBox1.SetSelected(x, true);
> }
>
> --
> Tim Wilson
> ..Net Compact Framework MVP
>
> "Alpha" <Al***@discussions.microsoft.com> wrote in message
> news:82**********************************@microsof t.com...
> > Hi, How can I set all the items in a listbox to be selected? I can't
> > find
> a
> > property or mehtod to do it so I thought I'll try using setselected
> > method
> > but I need to find out how many items are in the listbox.
> >
> > Thanks,
> > Alpha
>
>
>



Nov 17 '05 #7
PP
Here is an solution, that we had implemented (sorry, it might sound stupid :-))
To the list box we added an item as follows
[ALL] <-- Represents user wants to select all item(s)
item1
item2....

Based on the selection, we did the apprpriate thing wthin the code

Thanks
PP

"Fritz Switzer" wrote:
Tim,

Thanks, that was about "a gazillion" times faster. :)
Fritz

"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message
news:OH**************@TK2MSFTNGP14.phx.gbl...
Try the following code.

this.listBox1.SelectionMode = SelectionMode.MultiExtended;

...

if (this.listBox1.Items.Count > 0)
{
this.listBox1.Focus();
this.listBox1.SetSelected(0, true);
SendKeys.Send("+{END}");
}

--
Tim Wilson
.Net Compact Framework MVP

"Fritz Switzer" <fr***********@abletfactory.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
The method above is good for a small number of items in the listbox, but

if
you have many. (100,000) it is either too slow or doesn't work. Is there
any other way to accomplish the same thing, but may be faster.

TIA,

Fritz
"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
> Very cool! Thank you so much again.
> Have a great weekend!
> Alpha
>
> "Tim Wilson" wrote:
>
>> Something similar to the following code should do it.
>>
>> for (int x = 0; x < this.listBox1.Items.Count; x++)
>> {
>> this.listBox1.SetSelected(x, true);
>> }
>>
>> --
>> Tim Wilson
>> ..Net Compact Framework MVP
>>
>> "Alpha" <Al***@discussions.microsoft.com> wrote in message
>> news:82**********************************@microsof t.com...
>> > Hi, How can I set all the items in a listbox to be selected? I
>> > can't
>> > find
>> a
>> > property or mehtod to do it so I thought I'll try using setselected
>> > method
>> > but I need to find out how many items are in the listbox.
>> >
>> > Thanks,
>> > Alpha
>>
>>
>>



Nov 17 '05 #8
It has to do with setting focus and selections in the Load event, before the
Form is visible. To get around this, explicitly show the Form.

private void Form1_Load(object sender, System.EventArgs e)
{
// Do preparation work here.

for (int x = 0; x < 100; x++)
{
this.listBox1.Items.Add("Item" + x.ToString());
}

this.listBox1.SelectionMode = SelectionMode.MultiExtended;

this.Show();

// Do focused work here.

if (this.listBox1.Items.Count > 0)
{
this.listBox1.Focus();
this.listBox1.SetSelected(0, true);
SendKeys.Send("+{END}");
}
}

Alternatively, you could use the Activated event instead of Load. Just note
that the Activated event may fire multile times during the life span of the
Form, depending on what the user does. For example, if the Form loses focus
to another Form, the Activated event will fire again when the Form is
brought back to the foreground. So you would need to handle this by using a
flag that indicates the work you did in the Activated event at startup has
already been done and does not need to be done again.

--
Tim Wilson
..Net Compact Framework MVP

"PP" <PP@discussions.microsoft.com> wrote in message
news:5A**********************************@microsof t.com...
Hi Tim

I tried this code, but doesn't seem to work. It does not SELECT all item(s), it selects only the first item.

private void Form1_Load(object sender, System.EventArgs e)
{
listBox1.Items.Add ("Item-1") ;
listBox1.Items.Add ("Item-2") ;
listBox1.Items.Add ("Item-3") ;
listBox1.Items.Add ("Item-4") ;
listBox1.Items.Add ("Item-5") ;

this.listBox1.SelectionMode = SelectionMode.MultiExtended;
if (this.listBox1.Items.Count > 0)
{
this.listBox1.Focus();
this.listBox1.SetSelected(0, true);
SendKeys.Send("+{END}");
}
}


"Tim Wilson" wrote:
Try the following code.

this.listBox1.SelectionMode = SelectionMode.MultiExtended;

....

if (this.listBox1.Items.Count > 0)
{
this.listBox1.Focus();
this.listBox1.SetSelected(0, true);
SendKeys.Send("+{END}");
}

--
Tim Wilson
..Net Compact Framework MVP

"Fritz Switzer" <fr***********@abletfactory.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
The method above is good for a small number of items in the listbox, but
if
you have many. (100,000) it is either too slow or doesn't work. Is

there any other way to accomplish the same thing, but may be faster.

TIA,

Fritz
"Alpha" <Al***@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
> Very cool! Thank you so much again.
> Have a great weekend!
> Alpha
>
> "Tim Wilson" wrote:
>
>> Something similar to the following code should do it.
>>
>> for (int x = 0; x < this.listBox1.Items.Count; x++)
>> {
>> this.listBox1.SetSelected(x, true);
>> }
>>
>> --
>> Tim Wilson
>> ..Net Compact Framework MVP
>>
>> "Alpha" <Al***@discussions.microsoft.com> wrote in message
>> news:82**********************************@microsof t.com...
>> > Hi, How can I set all the items in a listbox to be selected? I can't >> > find
>> a
>> > property or mehtod to do it so I thought I'll try using setselected >> > method
>> > but I need to find out how many items are in the listbox.
>> >
>> > Thanks,
>> > Alpha
>>
>>
>>


Nov 17 '05 #9
PP
Sweet. That did the trick.

"Tim Wilson" wrote:
It has to do with setting focus and selections in the Load event, before the
Form is visible. To get around this, explicitly show the Form.

private void Form1_Load(object sender, System.EventArgs e)
{
// Do preparation work here.

for (int x = 0; x < 100; x++)
{
this.listBox1.Items.Add("Item" + x.ToString());
}

this.listBox1.SelectionMode = SelectionMode.MultiExtended;

this.Show();

// Do focused work here.

if (this.listBox1.Items.Count > 0)
{
this.listBox1.Focus();
this.listBox1.SetSelected(0, true);
SendKeys.Send("+{END}");
}
}

Alternatively, you could use the Activated event instead of Load. Just note
that the Activated event may fire multile times during the life span of the
Form, depending on what the user does. For example, if the Form loses focus
to another Form, the Activated event will fire again when the Form is
brought back to the foreground. So you would need to handle this by using a
flag that indicates the work you did in the Activated event at startup has
already been done and does not need to be done again.

--
Tim Wilson
..Net Compact Framework MVP

"PP" <PP@discussions.microsoft.com> wrote in message
news:5A**********************************@microsof t.com...
Hi Tim

I tried this code, but doesn't seem to work. It does not SELECT all

item(s),
it selects only the first item.

private void Form1_Load(object sender, System.EventArgs e)
{
listBox1.Items.Add ("Item-1") ;
listBox1.Items.Add ("Item-2") ;
listBox1.Items.Add ("Item-3") ;
listBox1.Items.Add ("Item-4") ;
listBox1.Items.Add ("Item-5") ;

this.listBox1.SelectionMode = SelectionMode.MultiExtended;
if (this.listBox1.Items.Count > 0)
{
this.listBox1.Focus();
this.listBox1.SetSelected(0, true);
SendKeys.Send("+{END}");
}
}


"Tim Wilson" wrote:
Try the following code.

this.listBox1.SelectionMode = SelectionMode.MultiExtended;

....

if (this.listBox1.Items.Count > 0)
{
this.listBox1.Focus();
this.listBox1.SetSelected(0, true);
SendKeys.Send("+{END}");
}

--
Tim Wilson
..Net Compact Framework MVP

"Fritz Switzer" <fr***********@abletfactory.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
> The method above is good for a small number of items in the listbox, but if
> you have many. (100,000) it is either too slow or doesn't work. Is there > any other way to accomplish the same thing, but may be faster.
>
> TIA,
>
> Fritz
>
>
> "Alpha" <Al***@discussions.microsoft.com> wrote in message
> news:6F**********************************@microsof t.com...
> > Very cool! Thank you so much again.
> > Have a great weekend!
> > Alpha
> >
> > "Tim Wilson" wrote:
> >
> >> Something similar to the following code should do it.
> >>
> >> for (int x = 0; x < this.listBox1.Items.Count; x++)
> >> {
> >> this.listBox1.SetSelected(x, true);
> >> }
> >>
> >> --
> >> Tim Wilson
> >> ..Net Compact Framework MVP
> >>
> >> "Alpha" <Al***@discussions.microsoft.com> wrote in message
> >> news:82**********************************@microsof t.com...
> >> > Hi, How can I set all the items in a listbox to be selected? I can't > >> > find
> >> a
> >> > property or mehtod to do it so I thought I'll try using setselected > >> > method
> >> > but I need to find out how many items are in the listbox.
> >> >
> >> > Thanks,
> >> > Alpha
> >>
> >>
> >>
>
>


Nov 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Stephen | last post: by
1 post views Thread by Jbatty | last post: by
3 posts views Thread by Miguel | last post: by
14 posts views Thread by BartlebyScrivener | last post: by
2 posts views Thread by Seema Multani | last post: by
2 posts views Thread by ProgrammerChicago | last post: by
4 posts views Thread by shifflav | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.