Windows Develop Bookmark and Share   
 index > Windows Forms General > How to Set attribute(invisible) to ListBox Item
 

How to Set attribute(invisible) to ListBox Item

Hi friends

i have a list box withitems.

i have to addsome additional information to eachitem which should not be visible in the viewer.

and after i have to transfer the listbox selected items from one window form to another

window form including the additional information.

i am able totransfer the listbox selected items but facing problems in adding attributes.

any suggetions

regards

Ranadheer

ranadheer mac  Friday, January 12, 2007 12:34 PM

Ranadheer,

Listbox takes all kinds of items. Try creating our own object with the information you want. The ToString() method will be used to display the object in the listbox. Like this

class A
{
public A()
{
}

// here you can put our extra values in fields or properties

//here is theToString method used for displaying
public override string ToString()
{
return "something"; //text displayed in the textbox
}
}

Now you can add an instance of the class to the listbox:

listBox1.Items.Add(new A());

micvos  Friday, January 12, 2007 1:27 PM

You can create a class something like this:

class ListBoxInformativeItem
{
private string value;
private string extraInfo;

public ListBoxInformativeItem(string value, string extraInfo)
{
this.value = value;
this.extraInfo = extraInfo;
}

public string Value
{
get { return this.value; }
set { this.value = value; }
}

public string ExtraInfo
{
get { return this.extraInfo; }
set { this.extraInfo = value; }
}

public override string ToString()
{
return this.value;
}
}

Now creta an object of this class and add to ListBox using Add method of it.

When added, you'll see value part of the object in the ListBox while extraInfo will be Hidden.

When you want to access the Hidden Value to can do something like this:

ListBoxInformativeItemlistBoxItem = ListBox.Items[0] as ListBoxInformativeItem;

if(listBoxItem != null)

{

MessageBox.Show(listBoxItem.ExtraInfo);

}

You can also add all items to and array using the above thing in a loop and then send to other forms.

I hope this will help.

Best Regards,

Rizwan aka RizwanSharp

RizwanSharp  Friday, January 12, 2007 5:11 PM

Dear rizwan,

thanks its very informative.

and i have one more complexity.

that is i have to transfer the selected listbox items(including Extrainfo)from one WindowForm to another WindoForm

then ihave to send the class object From one Form to Other

howcan iDo that

regards

ranadheer.

ranadheer mac  Saturday, January 13, 2007 7:05 AM

You can createa public property or function in the form to which this object has to be passed.

Property way:

put this to hold the object gotten:

private ListBoxInformativeIteminformativeItem

public ListBoxInformativeItem InformativeItem

{

get {return this.informativeItem; }

set { this.informativeItem = value; }

}

Now get the selected item from the ListBox using:

ListBoxInformativeItem listBoxItem =ListBox.Items[listBox.SelectedIndex]as ListBoxInformativeItem;

Now pass to other form using that property:

form2.InformativeItem = listBoxItem ;

now in that form you can access itsExtraInfolike this.Item.ExtraInfo;

I hope this will help.

Best Regards,

Rizwan aka RizwanSharp

RizwanSharp  Saturday, January 13, 2007 2:54 PM

You can use google to search for other answers

Custom Search

More Threads

• Custom cursor as resource?
• Showing forms
• TextBox Line Number/Column Number
• How to configure Avaya Phone Manger with C# windows or console application?
• Textbox Currency formating
• Winforms Exceptions - Exception Handling Application Block
• Controls - dropdowns & datepickers change border color & dropdown arow icon to grey
• /res in Visual Studio???
• all tool bar in report view are missing. Why?
• WinForms MenuStrip Bug