473,466 Members | 4,879 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Listbox selection

Hi
What is the most compact way of selecting an item in a listbox based on the first part of its string, does it have an inbuilt method?

For instance if I have the items
Aardvark
Antelope
Beaver
Camel

and I 'pass' the string "Ant", then I want 'Antelope' selected. But if I pass 'Adf' or 'Gup', then nothing should be selected.

Any neat way of accomplishing this?
Thanks
Nov 16 '05 #1
2 1887
Hi Beeeeeeeeeeeeves,

I fear you have to step through all items until you find a match. One way to find a match and select it is

string s = "Ant";

for(int i = 0; i < listBox1.Items.Count; i++)
{
if(((string)listBox1.Items[i]).IndexOf(s) == 0)
{
listBox1.SetSelected(i, true);
break; // no need to check the rest
}
}

This sample assumes the ListBox is filled with strings and that only one match will be found.

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2

Beeves

This will reduce number of lines

Method 1:

int pos = listBox1.FindString(s,0); //if you have partial string
//select item
if(pos >=0)
listBox1.SelectedIndex = pos;
Method 2:

listBox1.FindStringExact(s,0);// if you have whole string.
if(pos >=0)
listBox1.SelectedIndex = pos;

Shak.

"Beeeeeeeeeeeeves" <Be**************@discussions.microsoft.com> wrote in
message news:DC**********************************@microsof t.com...
Hi
What is the most compact way of selecting an item in a listbox based on the first part of its string, does it have an inbuilt method?
For instance if I have the items
Aardvark
Antelope
Beaver
Camel

and I 'pass' the string "Ant", then I want 'Antelope' selected. But if I pass 'Adf' or 'Gup', then nothing should be selected.
Any neat way of accomplishing this?
Thanks

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: (Pete Cresswell) | last post by:
Seems like I've been here before, but can't find anyting in Google. I've got two list boxes on a form. Seems to me like the inactive ListBox's selection rectangle should be something like...
6
by: Dan Bass | last post by:
If you look at explorer, right clicking on a file, first selects the file, then throws up the context menu relating to that selection. With a Windows ListBox control and a simple context menu,...
6
by: Alpha | last post by:
I have a listbox with datasource from a dataview. When a user selects a different item in a combobox then I need to refresh the listbox to the appropriate listing based on that combobox's selected...
6
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset...
5
by: Lie | last post by:
Hi all, I have problem in getting selectedindex of multiple listbox selection in a datagrid. I have a listbox with multiple selection mode inside datagrid. In Edit mode, I need to get back all...
5
by: Dave H | last post by:
I have an asp:listbox, allowing multiple selections, is there a quick check to see if there's more than one selected, or do I need to go through the whole list? Thanks, Dave
0
by: Sanjin | last post by:
I have populated listbox with data from the datatable. I binded SelectedValue to the another datatable - i.e. selection result (with VS 20005 wizard). Selection result datatable has...
3
by: Kevin Walzer | last post by:
I'm trying to set the active item in a Tkinter listbox to my application's currently-defined default font. Here's how I get the fonts loaded into the listbox: ...
9
by: zdrakec | last post by:
Hello all: Clearly, I'm not getting it! Here is the scenario: On a web page, I have two list boxen and a text box. The first listbox is populated at page load time (if it is not a postback)....
4
by: kimiraikkonen | last post by:
Hi, I have a odd but a known question about listbox. I know listbox control can provide multi-select, multi-extendend selections. But i wonder if this selection type belongs to them or it has...
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.