Friday, March 16, 2012

ListBox control and ArrayList objects

Hi there

I am having trouble getting the selected value from a ListBox control

The Listbox is populated with an ArrayList of Objects by calling a ToString() method and these objects are not simple Strings like all the examples I'm finding on the web.

Is there anyway of getting the value of one of the objects based on the user's selection?

My object contains an int and a string but I need to be able to get the value of the int.

Any of the built in methods like selectedItem or selectedvalue are returning strings.

Many thanks for any help or guidance!

Get the selectedValue & convert it to int


Hai ,

' Let lb be the list box

Dim str as string

str= lb.selectedvalue

magbox(str)

You will get the string value


Sorry

' Let lb be the list box

Dim intValue as integer

intValue = cint(lb.selectedvalue)

magbox(intValue)

You will get the integer value


Hi Everyone

Thanks for the replies.

Sorry, I wasn't clearer - I have an arrayList of objects that have an int called encID and a string called userID - I needed to display both in the ListBox, hence this is why I had to use a ToString() to display both items (returned from a database) as a String

It is therefore displaying in the listbox as "123 - ABC"

The second item in the list would be: "456 - DEF" and so on

The SelectedValue or SelectedItem returns as a String but what I need is to get is the int component and pass to the session object for the next page.

Is there any way of referencing the object that the selected item represents - I can then call the getEncID() to get the specific value?

I'm writing this in C# so I might have to use a converter for any VB code samples!!Smile

thanks

]glo[


Just Check the value of

String s = MyListBox.SelectedValue;

If that works, use the following

int i = Convert.ToInt32(MyListBox.SelectedValue);


Thanks for this reply too.

I did think of this and even thought of Parsing the string

However, although the UserID is a string - it is actually composed of letters and numbers so I would be afraid of pulling out some of these numbers too.

I thought that there would be some way of using the ListItem as the MyListBox.selectedItem is of type ListItem.

Maybe there is some way of iterating though the collection to find the int ???

Thanks again for your help.


In ur original post u said u need to get the integer value.

Again in another post also u were telling:

It is therefore displaying in the listbox as "123 - ABC"

The second item in the list would be: "456 - DEF" and so on

If u want to get the 123, 456, get it as I suggested.

If u want to get ABC,DEF or if u want to use the ListItem or something else, let us know.


Sorry this has been so confusing - I know it has been quite difficult to get across what I'm trying to do.

I appreciate that I could parse the value selected in the ListBox as you suggest. Thanks for this.

I wonder if there is a better object oriented way to do this. For example, what if the data I wanted to select and pass on in my session was not represented by the string of text displayed in the listbox. Remember I'm supplying a collection of objects to the ListBox and based on my selection I would like to find the object associated from my collection, and pass this object along in my session. Working with only Strings and parsing through them seems a bit awkward.

Here's a snippet of the code I'm referring to:

ArrayList list = dao.getList();

MyListBox.DataSource = list;

MyListBox.DataBind();

This ArrayList is not a simple set of Strings but contains a series of objects. For each object, I have overridden the ToString() method. This means that the ListBox displays the results of the ToString() method for each of the objects in my ArrayList. What I wish to know is when the user makes a selection in the ListBox, how can I determine which *object* in my ArrayList that is being selected.


As far as I'm aware you're going to have to get the selected value and then use that to retrieve the object that was selected,

Something like

Object yourSelectedObject = someDataLayer.GetByID(Convert.ToInt32(YourListBox.SelectedValue.ToString());
 There isnt a way (again as far as I know) where you can associate your objects with the ListItem.  

Thanks so much for this reply - this is getting closer to what I need to do.

Can I ask what might seem like a really silly question?

The someDataLayer that you are taking about - is this the data access object that I used to pull back the objects and the GetByID you mention - would I need to write this method or are you talking about some other built in method that I'm not aware of?

Thanks


yeah sorry I should have made that more clear.

The someDataLayer would be the dataaccessobject which you use to interface with your data store, and the GetByID method would just be a method within that class which returned a single object based on some unique id you were using.

For example, lets say you were dealing with Customers, you might do something like this

Customer selectedCustomer = CustomerDataAccessObject.GetCustomerByCustomerID(Convert.ToInt32(CustomersListBox.SelectedValue.ToString()))


Hi Glo,

Based on my understanding, you bound the ListBox with ArrayList that contains the custom object. The custom object contains two properties. You want to know how to get the custom object according to the select item from the ListBox. If I have misunderstood you, please feel free to let me know.

I agree with the member above. We can create the method to get the custom object by the ListBox's SelectValue. We also can bound the ListBox with the custom object and set the DataTextField and DataValueField to the properties of the custom object. For example, we create the class named CustomObj which contains two properties: UserID and UserName:

public partialclass BindObject_Default : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e) {if (!IsPostBack) {//Bound the ListBox. ListBox1.DataSource = GetCustomerList();//Set the UserName as Text Field.ListBox1.DataTextField ="UserName";//Display ID and Name.//ListBox1.DataTextField = "UserIdName"; //Set the UserID as Value Field. ListBox1.DataValueField ="UserID"; ListBox1.DataBind(); } }/// <summary> /// Get the list that contains the Object. /// </summary> /// <returns></returns>public ArrayList GetCustomerList() { ArrayList list =new ArrayList(); CustomObj objUserA =new CustomObj(123,"abc"); CustomObj objUserB =new CustomObj(456,"edf"); list.Add(objUserA); list.Add(objUserB);return list; }protected void Button1_Click(object sender, EventArgs e) {foreach (ListItem itemin ListBox1.Items) {if (item.Selected) {//Store the object in the session.Session["SelectObj"] =new CustomObj(Convert.ToInt32( ListBox1.SelectedItem.Value), ListBox1.SelectedItem.Text); } } }}/// <summary>/// The class has two properties: UserID, UserName./// </summary>public class CustomObj{public CustomObj(int id,string name) {this.userID = id;this.userName = name; }private int userID;public intUserID {get {return userID; }set { userID =value; } }private string userName;public stringUserName {get {return userName; }set { userName =value; } }//Display ID and Name.public stringUserIdName {get {return userID+"-"+userName; } } }


I hope this helps.


Thanks so much for going to all this effort - I managed to get something working last night and it's along the same lines as what you describe.

I've included the code here below for any one who is interested or come across this problem again.

Hope this helps someone else too!

public void Page_Load(object sender, EventArgs e) {if (!IsPostBack) { MyDataAccessObject dao =new MyDataAccessObject(); ArrayList availableEncs = dao.getEncs(); IEnumerator listEnum = availableEncs.GetEnumerator();while (listEnum.MoveNext()) { MyDataAccessObject currentResult = (MyDataAccessObject)listEnum.Current; ListItem li =new ListItem(); li.Text = currentResult.ToString(); li.Value = currentResult.getEncounterID().ToString(); MyListBox.Items.Add(li); } } }protected void NextButton_Click(object sender, EventArgs e) {int encID = -1; ListItem li = MyListBox.SelectedItem;if (li !=null) encID = Convert.ToInt32(li.Value); }

0 comments:

Post a Comment