Link to home
Start Free TrialLog in
Avatar of RobertNZana
RobertNZanaFlag for United States of America

asked on

How set minimum width of listbox?

How can I set the MINIMUM width of a listbox control, so that when there are NO ITEMS it is 150px, but when there are items in it the listbox gets wider to FIT the longest item, so there's no scrollbar?
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

I don't think that would be a proper design. Imagine if the width of the text is 500px then it will ruin the other controls on the page.  So what I suggest is this.

Use the title tag to show a tooltip to the user.

Use the databound event of the listbox
protected void ListBox1_DataBound(object sender, EventArgs e)
        {
            foreach (ListItem li in ListBox1.Items)
            {
                li.Attributes.Add("title", li.Text);
            }
        }

Open in new window

Hi,

on the Page_Load event you could check if there are any items in the listbox and if not set the width to 150px. Also i you  might be able to loop through the items in the list box and get the length of the logest and work off that. Something like:

Page_Load()
{
   if(ListBox.Items.Count = 0)
   {
       ListBox.Width = 150px;
   }
}

HTH
-M3mph15
Avatar of RobertNZana

ASKER

I had that part already.  But, let's say it's set to 150px then the user adds an item to the listbox.  I cannot set it to 0, or it's too narrow.  How can I "NULLIFY" the value once it's set?
Hi,

Why do you want to Nullify the value?
If a value for the width is set I cannot "remove" the defined width value.  Setting it to 0 doesn't work either.  I need to put it in a state as if the width was never set, so it will expand dynamically based on the largest listitem width.
ASKER CERTIFIED SOLUTION
Avatar of M3mph15
M3mph15
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.  I ended up using a checkbox list control, which suited my needs.  But I gave u the points...  :)