473,322 Members | 1,403 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,322 software developers and data experts.

listbox item font

Hey Guys.
I'm making an asp.net app and I have a question regarding listboxes. I
have a list box and I would like some of the items to be bold based on
certian criteria. Is this possible?
matt
Nov 19 '05 #1
3 2836
I do not believe that is possible in HTML. You might want to look into using
an ASP.NET DataList.

Jason Lind

"mkiger" wrote:
Hey Guys.
I'm making an asp.net app and I have a question regarding listboxes. I
have a list box and I would like some of the items to be bold based on
certian criteria. Is this possible?
matt

Nov 19 '05 #2
No way! Standard ListBox is really "simple". Try something like this:

/// <summary>
/// ListBoxWithColor
///
/// Simple control that renders the color attribute of each ListBox option.
///
/// Usage:
/// ListBoxWithColor1.Items.Clear();
/// ListBoxWithColor1.Items.Add(new ListItem("Hubba","0"));
/// ListBoxWithColor1.Items[0].Attributes["style"] =
"background-color:red";
///
/// ListBoxWithColor1.Items.Add(new ListItem("Hubba1","1"));
/// ListBoxWithColor1.Items[1].Attributes["style"] =
"background-color:white";
///
/// ListBoxWithColor1.Items[1].Selected = true;
///
/// Test with:
/// M$ Internet Explorer 6.
///
/// </summary>
[ToolboxData("<{0}:ListBoxWithColor runat=server></{0}:ListBoxWithColor>")]
public class ListBoxWithColor : System.Web.UI.WebControls.ListBox
{
public ListBoxWithColor()
{
// constructor
}
/// <summary>
/// Override to save color attrib to viewstate
/// </summary>
protected override object SaveViewState()
{
// creat object array for Item count + 1
object[] allStates = new object[this.Items.Count + 1];

// the +1 is to hold the base info like dis
object baseState = base.SaveViewState();
allStates[0] = baseState;

Int32 i = 1;
// now loop thru and save each Style attrib for the List
foreach(ListItem li in this.Items)
{
allStates[i++] = li.Attributes["style"].ToString();
}
return allStates;
}
/// <summary>
/// Override to restore color attrib
/// </summary>
protected override void LoadViewState(object savedState)
{
if (savedState != null)
{
object[] myState = (object[])savedState;

// restore base first
if (myState[0] != null)
base.LoadViewState(myState[0]);

Int32 i = 1;
foreach(ListItem li in this.Items)
{
// loop thru and restore each style attrib
li.Attributes["style"] = (string)myState[i++];
}
}
}
/// <summary>
/// Override to render contents of ListBox
/// </summary>
protected override void RenderContents(HtmlTextWriter writer)
{
foreach(ListItem li in this.Items)
{
writer.WriteBeginTag("option");

if(li.Selected)
writer.WriteAttribute("selected","selected",false) ;

if(li.Attributes.Count > 0)
li.Attributes.Render(writer);

writer.WriteAttribute("value",li.Value.ToString()) ;
writer.Write(HtmlTextWriter.TagRightChar);
writer.Write(li.Text);

writer.WriteEndTag("option");
writer.WriteLine();
}
}
}
--
C# Dev
"Jason L Lind" wrote:
I do not believe that is possible in HTML. You might want to look into using
an ASP.NET DataList.

Jason Lind

"mkiger" wrote:
Hey Guys.
I'm making an asp.net app and I have a question regarding listboxes. I
have a list box and I would like some of the items to be bold based on
certian criteria. Is this possible?
matt

Nov 19 '05 #3
works great. (nearly) exactly what I wanted... :)

changed the save/load viewstate to deal with all the attributes not
just style.

protected override object SaveViewState()
{
// creat object array for Item count + 1
object[] allStates = new object[this.Items.Count + 1];

// the +1 is to hold the base info like dis
object baseState = base.SaveViewState();
allStates[0] = baseState;

Int32 i = 1;
// now loop thru and save each Style attrib for the List
foreach(ListItem li in this.Items)
{
Int32 j = 0;
string[][] attributes = new string[li.Attributes.Count][];
foreach (string attribute in li.Attributes.Keys)
{
attributes[j++] = new string[] {attribute,
li.Attributes[attribute]};
}
allStates[i++] = attributes;
}
return allStates;
}

protected override void LoadViewState(object savedState)
{
if (savedState != null)
{
object[] myState = (object[])savedState;

// restore base first
if (myState[0] != null)
base.LoadViewState(myState[0]);

Int32 i = 1;
foreach(ListItem li in this.Items)
{
// loop thru and restore each style attrib
//li.Attributes["style"] =
foreach (string[] attribute in (string[][])myState[i++])
{
li.Attributes[attribute[0]] = attribute[1];
}
}
}
}

Nov 19 '05 #4

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

Similar topics

6
by: R.Wieser | last post by:
Hello All, I'm trying to get a "Virtual Listbox" to work. I've currently got a form, and used CreateWindowExA to create a ListBox with the LBS_OWNERDRAWFIXED and LBS_NODATA flags on it. I've...
0
by: pothik05 | last post by:
I can not make the DrawItem event to work on the CheckedListBox control. I suggestion will be highly appreciated. I have the following code. The code works for ListBox and the second item (Two)...
1
by: Patty O'Dors | last post by:
Hi I have some code to create an ownerdrawn listbox (derived), and when I add an item to it, the bold text of the first item (the title, 'Collections and Maturities') mysteriously seems to get...
1
by: Edward | last post by:
I am having a terrible time getting anything useful out of a listbox on my web form. I am populating it with the results from Postcode lookup software, and it is showing the results fine. What...
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...
6
by: Max | last post by:
I need some control that basically acts as a listbox, but allows each item to take up more then one line. I've tried adding a new item and putting in a vbCrLf in there, but that just comes out as a...
0
by: Mark Smith | last post by:
hi i use an ownerdraw method for coloring some items in the list different then the others. code: private void ListBoxDrawItem(object sender, DrawItemEventArgs e) { ListBox lst =...
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: ...
6
by: Dom | last post by:
In some programs, I see that certain items in a listbox have been grayed, to indicate that they are "there" but can not be chosen. This is very helpful. Can you do this in CSharp? Is there a...
3
by: CSharper | last post by:
Is it possible to selectivly change the color of an item in text. I saw ForeColor option, but it changes the color of all the items. If it is not possible, is there any other control list listbox...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.