<legend id='lBxT3'><style id='lBxT3'><dir id='lBxT3'><q id='lBxT3'></q></dir></style></legend>

        <tfoot id='lBxT3'></tfoot>

      1. <small id='lBxT3'></small><noframes id='lBxT3'>

          <bdo id='lBxT3'></bdo><ul id='lBxT3'></ul>
        <i id='lBxT3'><tr id='lBxT3'><dt id='lBxT3'><q id='lBxT3'><span id='lBxT3'><b id='lBxT3'><form id='lBxT3'><ins id='lBxT3'></ins><ul id='lBxT3'></ul><sub id='lBxT3'></sub></form><legend id='lBxT3'></legend><bdo id='lBxT3'><pre id='lBxT3'><center id='lBxT3'></center></pre></bdo></b><th id='lBxT3'></th></span></q></dt></tr></i><div id='lBxT3'><tfoot id='lBxT3'></tfoot><dl id='lBxT3'><fieldset id='lBxT3'></fieldset></dl></div>

        C# Listbox.DrawItem 为每一行着色

        时间:2023-10-07
          • <bdo id='t5BDH'></bdo><ul id='t5BDH'></ul>
              <i id='t5BDH'><tr id='t5BDH'><dt id='t5BDH'><q id='t5BDH'><span id='t5BDH'><b id='t5BDH'><form id='t5BDH'><ins id='t5BDH'></ins><ul id='t5BDH'></ul><sub id='t5BDH'></sub></form><legend id='t5BDH'></legend><bdo id='t5BDH'><pre id='t5BDH'><center id='t5BDH'></center></pre></bdo></b><th id='t5BDH'></th></span></q></dt></tr></i><div id='t5BDH'><tfoot id='t5BDH'></tfoot><dl id='t5BDH'><fieldset id='t5BDH'></fieldset></dl></div>

            • <tfoot id='t5BDH'></tfoot>

                <tbody id='t5BDH'></tbody>
              <legend id='t5BDH'><style id='t5BDH'><dir id='t5BDH'><q id='t5BDH'></q></dir></style></legend>

                  <small id='t5BDH'></small><noframes id='t5BDH'>

                  本文介绍了C# Listbox.DrawItem 为每一行着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一些代码可以改变列表框的行为方式.我可以更改文本的颜色,但我无法更改每行的背景颜色.

                  I have some code to change how listbox act. I can change the color of text, but I am unable to change the color of the background of each line.

                  我的每一行都在 for 循环中

                  This is in a for loop for every of my lines

                  LBLines 是一个存储在全局变量中的字符串数组

                  LBLines is an array of string store in a global variable

                  if (LBLines[e.Index] != "None")
                  {
                         e.Graphics.FillRectangle(new SolidBrush(Color.FromName(LBLines[e.Index])),
                  e.Bounds.X,e.Bounds.Y,e.Bounds.Width,e.Bounds.Height);
                  }
                  

                  这将为每条线着色相同颜色,即使是那些列为无"的线,我需要的是它们保持与默认背景颜色相同的颜色.

                  This will color EVERY lines of the same color, even those listed as "None", thou what I need is that they stay same color as default background color.

                  比较不是问题,问题来自 e.Graphics.FillRectangle.不管我画的是什么,它似乎都给所有的线条空间着色.

                  Comparaison is not the problem, problem come from the e.Graphics.FillRectangle. It seems to color ALL the lines spaces, regardless of the one I am drawing.

                  修改代码以显示 h 等于 e.Index

                  Modified code to show that h was equal to e.Index

                  推荐答案

                  如果没有更多关于代码(循环、方法等)的上下文,很难说,但是这段代码可以满足我的需求:

                  Hard to say without more context around your code (the loop, the method,...), but this code does what I think it is you want:

                  public partial class Form1 : Form
                  {
                      string[] Colors { get; set; }
                  
                      public Form1()
                      {
                          InitializeComponent();
                          Colors = new string[] { "red", "blue", "white", "none", "orange" };
                          listBox1.Items.AddRange(Colors);
                      }
                  
                      private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
                      {
                          e.DrawBackground();
                          if (Colors[e.Index] != "none")
                          {
                              using (var brush = new SolidBrush(Color.FromName(Colors[e.Index])))
                              {
                                  e.Graphics.FillRectangle(brush, e.Bounds);
                              }
                          }
                          e.Graphics.DrawString(Colors[e.Index], Font, SystemBrushes.ControlText, e.Bounds);
                      }
                  }
                  

                  注意 ListBoxDrawStyle 属性设置为 OwnerDrawFixed.

                  Note that the DrawStyle property of the ListBox is set to OwnerDrawFixed.

                  这篇关于C# Listbox.DrawItem 为每一行着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:验证 ListBoxItem 而不是 ListBox 下一篇:如何在多种模式下滚动asp列表框中的选定项目?

                  相关文章

                      <bdo id='cIjX4'></bdo><ul id='cIjX4'></ul>
                  1. <small id='cIjX4'></small><noframes id='cIjX4'>

                  2. <tfoot id='cIjX4'></tfoot>

                    <legend id='cIjX4'><style id='cIjX4'><dir id='cIjX4'><q id='cIjX4'></q></dir></style></legend>
                  3. <i id='cIjX4'><tr id='cIjX4'><dt id='cIjX4'><q id='cIjX4'><span id='cIjX4'><b id='cIjX4'><form id='cIjX4'><ins id='cIjX4'></ins><ul id='cIjX4'></ul><sub id='cIjX4'></sub></form><legend id='cIjX4'></legend><bdo id='cIjX4'><pre id='cIjX4'><center id='cIjX4'></center></pre></bdo></b><th id='cIjX4'></th></span></q></dt></tr></i><div id='cIjX4'><tfoot id='cIjX4'></tfoot><dl id='cIjX4'><fieldset id='cIjX4'></fieldset></dl></div>