如何更改列表框选中的项目forecolor / backcolor按钮单击C#[复制] [英] How to change listbox selected item forecolor/backcolor on button click in C# [duplicate]

查看:100
本文介绍了如何更改列表框选中的项目forecolor / backcolor按钮单击C#[复制]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我在我的C#项目中遇到了一个问题,问题是如何更改列表框选中的项目forecolor / backcolor按钮点击c#

请帮我解决这个问题

谢谢



我尝试了什么:



i也尝试过drawitem活动,fontdialog,颜色对话框

解决方案

参考 - c# - 如何以编程方式更改列表框中所选项目的背景颜色? - 堆栈溢出 [ ^ ]


试试这个



 私有  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 [] { 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);

使用(画笔textBrush = 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();
}


这是我使用jQuery与RegisterClientScriptBlock实现相同的



 <   html     xmlns   =  http:/ /www.w3.org/1999/xhtml\">  
< head runat = server >
< title > < / title >
< script src = Scripts / jquery-1.10.2。 js > < / script >
< / head >
< body >
< 表格 id = form1 runat = server >
< div >
< asp:listbox id = lstBox runat = server clientidmode = 静态 xmlns:asp = #unknown >
< asp:listitem text = test1 value = 1 已选择 = True > < / asp:listitem >
< asp:listitem text = test2 value = 2 > < span class =code-keyword>< / asp:listitem >
< asp:listitem text = test3 value = 3 > < / asp:listitem >
< asp:listitem text = test4 = 4 > ; < / asp:listitem > ;
< / asp:listbox >
< / div >
< asp:button id = btnText runat < span class =code-keyword> = server text = 按钮 onclick = btnText_Click xmlns:asp = #unknown / >
< / form >
< / body >
< / html >





按钮的服务器端单击事件处理程序如下: -



  protected   void  btnText_Click( object  sender,EventArgs e)
{
< span class =code-sdkkeyword> String scriptName = ButtonClickJSScript;
类型pageType = .GetType();

ClientScriptManager cs = Page.ClientScript;

if (!cs.IsClientScriptBlockRegistered(pageType,scriptName))
{
StringBuilder scriptText = new StringBuilder();
scriptText.Append( < script type = \ text / javascript \ >

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

解决方案

Refer - c# - how to change background color of selected item in a listbox programmatically? - Stack Overflow[^]


try this

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();
       }


Here is mine implemented the same using jQuery with RegisterClientScriptBlock

<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:-

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\"">


这篇关于如何更改列表框选中的项目forecolor / backcolor按钮单击C#[复制]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆