473,386 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 45411
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: darren | last post by:
How, using asp.net (vb.net), can I remove multiple selected items from a listbox. Everything I try does not seem to work. Thanks in advance.
3
by: Stephen | last post by:
I'm trying to do an item count of a datagrid but my problem is that the datagrid is paged and I only get a count for the number of rows in one page as oppose to the whole dataset which has been...
1
by: Jbatty | last post by:
To count all the item in currently in my listview, i use this.TotalMessages = myListView.Items.Count; What can I use to count the number of item in the list box that has the ImageIndex of 0? ...
4
by: Ron | last post by:
I've got a listbox that holds a list of groups. Users can select a group, hit the remove button and the group should be removed from the listbox. The only problem is that no matter which group you...
3
by: Miguel | last post by:
I have a parameter query to filter by date from which I run a report. I need to be able to count the number of times a particular product or error type appears after the report is run from the...
14
by: BartlebyScrivener | last post by:
Still new. I am trying to make a simple word count script. I found this in the great Python Cookbook, which allows me to process every word in a file. But how do I use it to count the items...
2
by: Seema Multani | last post by:
How can I find out weather my listbox is empty or not
2
by: ProgrammerChicago | last post by:
I'm not sure if this is the result of the postback behavior or my own code, but for some reason my onclick function is not detecting listbox selections (It's meant to delete files uploaded to the...
2
by: gobblegob | last post by:
hi guys, All i want to do is count items in a listbox (Lines not Characters) i scoured google with no luck. Thanks in advanced, GobbleGob.
4
by: shifflav | last post by:
The code below puts each item in a float:left DIV. After each 3rd DIV it needs to clear the previous 3 DIVs and start a new row. The code below works, but it requires that I edit a 'column' element...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.