Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello Guys! i am facing a issue in my C# project and that issue is How to change listbox selected item forecolor/backcolor on button click in c#
Please help me sort out this issue
Thanks

What I have tried:

i have also tried drawitem event,fontdialog ,color dialog
Posted
Updated 7-Jul-16 20:58pm

try this

C#
private void Form1_Load(object sender, EventArgs e)
       {
           listBox1.DrawMode = DrawMode.OwnerDrawFixed;
           listBox1.SelectionMode = SelectionMode.MultiExtended;
           listBox1.DrawItem += new DrawItemEventHandler(listBox_DrawItem);
           listBox1.Items.AddRange(new object[] { "A", "B", "C", "D" });
       }
       List<int> lstSelectedIndex = new List<int>();
       void listBox_DrawItem(object sender, DrawItemEventArgs e)
       {
           if (e.Index > -1)
           {
               e.DrawBackground();
               if (lstSelectedIndex.Any(k => k == e.Index))
                   e.Graphics.FillRectangle(Brushes.Green, e.Bounds);

               using (Brush textBrush = new SolidBrush(e.ForeColor))
               {
                   e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, textBrush, e.Bounds.Location);
               }
           }
       }

       private void button1_Click(object sender, EventArgs e)
       {
           lstSelectedIndex.Clear();
           lstSelectedIndex.AddRange(listBox1.SelectedIndices.OfType<int>());
           listBox1.Refresh();
       }
 
Share this answer
 
Here is mine implemented the same using jQuery with RegisterClientScriptBlock

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.10.2.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:listbox id="lstBox" runat="server" clientidmode="Static" xmlns:asp="#unknown">
            <asp:listitem text="test1" value="1" selected="True"></asp:listitem>
            <asp:listitem text="test2" value="2"></asp:listitem>
            <asp:listitem text="test3" value="3"></asp:listitem>
            <asp:listitem text="test4" value="4"></asp:listitem>
        </asp:listbox>
    </div>
        <asp:button id="btnText" runat="server" text="Button" onclick="btnText_Click" xmlns:asp="#unknown" />
    </form>
</body>
</html>


And on the server side click event handler for the button is as below:-

C#
protected void btnText_Click(object sender, EventArgs e)
        {
            String scriptName = "ButtonClickJSScript";
            Type pageType = this.GetType();

            ClientScriptManager cs = Page.ClientScript;

            if (!cs.IsClientScriptBlockRegistered(pageType, scriptName))
            {
                StringBuilder scriptText = new StringBuilder();
                scriptText.Append("<script type="\"text/javascript\""> $(function () {");
                scriptText.Append("$('#lstBox option:selected').css('background-color', 'red');");
                scriptText.Append("$('#lstBox option:selected').css('color', 'green');");
                scriptText.Append("});</script>");
                cs.RegisterClientScriptBlock(pageType, scriptName, scriptText.ToString());
            }
        }



Hope this will be of help to you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900