如何更改列表视图选择行背景色,即使专注于其他控件? [英] How to change listview selected row backcolor even when focus on another control?

查看:249
本文介绍了如何更改列表视图选择行背景色,即使专注于其他控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用一个酒吧code扫描仪作为输入设备,这样就意味着我需要保持专注于一个文本框的程序。

I have a program which uses a barcode scanner as input device so that means I need to keep the focus on a text box.

该方案有ListView控件,我选择编程当某个酒吧code扫描的项目之一。我设置行的背景颜色是:

The program has a listview control and I select one of the items programatically when a certain barcode is scanned. I set the background color of the row by:

listviewitem.BackColor = Color.LightSteelBlue;

东西我曾尝试:


  • listview.HideSelection 设置为false

  • 通话 listview.Focus()设置颜色后

  • listviewitem.Focused 设置为true

  • 通话 listview.Invalidate

  • 通话 listview.Update()

  • 通话 listview.Refresh()

  • 上述
  • 的不同组合
  • listview.HideSelection set to false
  • call listview.Focus() after setting the color
  • listviewitem.Focused set to true
  • call listview.Invalidate
  • call listview.Update()
  • call listview.Refresh()
  • different combinations of the above

我也做了上面的东西组合在一个定时器,使得他们被称为在不同的线程,但仍然没有成功。

I've also did combinations above stuff in a timer so that they are called on a different thread but still no success.

任何想法?

更多信息:


  • 这里的关键是控制的重点。在 ListView控件不具有焦点当我选择其中一个项目。

  • 我选择一个项目做:

  • The key here is the control focus. The listview control does not have the focus when I select one of the items.
  • I select one item by doing:

listView1.Items[index].Selected = true;


  • 焦点总是在文本框。

  • the Focus is always in the textbox.

    我有这样的code继续把重点放在文本框:

    I have this code to keep the focus on the textbox:

    private void txtBarcode_Leave(object sender, EventArgs e)
    {
       this.txtBarcode.Focus();
    }
    

    您需要有一个文本框添加code来模拟我的问题。

    You need to have a textbox add that code to simulate my problem.

    推荐答案

    你描述的作品的究竟的预期,假设你已经设置了 HideSelection 的ListView 控制为False的财产。下面是演示截图。我创建了一个空白的项目,新增了的ListView 控制和文本框控件到窗体,增加了一些样本项目到的ListView ,将其以详细信息(虽然这可以在任何视图),并设置 HideSelection 为false。我处理的 TextBox.Leave 事件就像你在问题中显示,并增加了一些简单的逻辑来选择对应的 ListViewItem的每当它的名字输入到文本框请注意,测试项目六中选择了的ListView

    What you describe works exactly as expected, assuming that you've set the HideSelection property of the ListView control to False. Here's a screenshot for demonstration purposes. I created a blank project, added a ListView control and a TextBox control to a form, added some sample items to the ListView, set its view to "Details" (although this works in any view), and set HideSelection to false. I handled the TextBox.Leave event just as you showed in the question, and added some simple logic to select the corresponding ListViewItem whenever its name was entered into the TextBox. Notice that "Test Item Six" is selected in the ListView:

         

         

    现在,我开始怀疑,您打算把事情搞得一团糟,如果你去设置背景色自己财产胡闹左右。我不知道你为什么会曾经想要做到这一点,作为控制已经使用了默认选择的颜色来表示默认选择项。如果你想使用的不同的的颜色,你应该改变你的Windows主题,而不是试图去写code做到这一点。

    Now, as I suspected initially, you're going to mess things up if you go monkeying around with setting the BackColor property yourself. I'm not sure why you would ever want to do this, as the control already uses the default selection colors to indicate selected items by default. If you want to use different colors, you should change your Windows theme, rather than trying to write code to do it.

    在事实上,如果我还添加行 item.BackColor = Color.LightSteelBlue 来我现有的code选择的ListViewItem 对应输入到文本框的名字,我得到的究竟的同样的事情,如上图所示。该项目的背景颜色不会改变,直到你将焦点设置到该控件。那是的有望的行为,因为选择的项目看起来不同,当他们有当他们的父母控制聚焦比他们做的重点。在集中控制所选项目都画着系统突出显示颜色;在聚焦控制所选项目都画着系统3D彩色。否则,就不可能告诉的ListView 控制是否没有焦点。此外,任何自定义的背景色属性的完全忽略的操作系统时,的ListView 控制具有焦点。背景被绘在默认系统高亮颜色。

    In fact, if I add the line item.BackColor = Color.LightSteelBlue in addition to my existing code to select the ListViewItem corresponding to the name typed into the TextBox, I get exactly the same thing as shown above. The background color of the item doesn't change until you set focus to the control. That's the expected behavior, as selected items look different when they have the focus than they do when their parent control is unfocused. Selected items on focused controls are painted with the system highlight color; selected items on unfocused controls are painted with the system 3D color. Otherwise, it would be impossible to tell whether or not the ListView control had the focus. Moreover, any custom BackColor property is completely ignored by the operating system when the ListView control has the focus. The background gets painted in the default system highlight color.

    明确设置焦点的ListView 控制,当然,使自定义背景色被应用到 ListViewItem的,事情与非常符合,我已经在我的电脑上选择的配色方案对比色渲染(记住,不是每个人都使用的默认值)。这个问题,不过,成为显而易见的:您不能设置,因为$ C $的焦点的ListView 控制c。你写的 TextBox.Leave 事件处理方法!

    Explicitly setting the focus to the ListView control, of course, causes the custom background color to be applied to the ListViewItem, and things render with a color that very much contrasts with the color scheme that I've selected on my computer (remember, not everyone uses the defaults). The problem, though, becomes immediately obvious: you can't set the focus to the ListView control because of the code you've written in the TextBox.Leave event handler method!

    我现在能在一个焦点改变事件设置焦点是错误的事情告诉你。这就是你不允许做这样的事情在Windows中一个硬性的规则,而<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.control.leave.aspx\">documentation即使是警告你的明确的不要做。 presumably,你的回答将是沿着我要线的东西,但是这是没有任何借口。如果按预期进行的一切,你就不会问放在第一位这个问题。

    I can tell you right now that setting the focus in a focus-changing event is the wrong thing to do. It's a hard rule in Windows you're not allowed to do things like that, and the documentation even warns you explicitly not to do it. Presumably, your answer will be something along the lines of "I have to", but that's no excuse. If everything were working as expected, you wouldn't be asking this question in the first place.

    那么,现在该怎么办? 您的应用程序的设计是坏了。我建议修复它。不要试图与自己设置背景色属性表明一个项目被选中的猴子。它与冲突的默认方式的Windows凸显选定的项目。此外,不要试图将焦点设置在一个焦点改变事件。窗户明确禁止这一点,和文档很清楚,你不应该这样做。如果目标计算机没有鼠标或键盘,但目前还不清楚用户将如何将焦点设置到摆在首位别的,除非你写code做到这一点,你不应该做。

    So, what now? Your application's design is broken. I suggest fixing it. Don't try and monkey with setting the BackColor property yourself to indicate that an item is selected. It conflicts with the default way that Windows highlights selected items. Also, don't try and set the focus in a focus-changing event. Windows explicitly forbids this, and the documentation is clear that you're not supposed to do this. If the target computer doesn't have a mouse or keyboard, it's unclear how the user is going to set focus to anything else in the first place, unless you write code to do it, which you shouldn't be doing.

    但我有令人惊讶的一点信心,你要修复您的应用程序。谁忽视文档中警告人们往往是谁不听善意的Q&放意见相同的人; A位点。所以我会扔你一个骨告诉你如何得到你想要反正效果。 关键在于没有设置 ListViewItem的属性,它避免了您的自定义之间的矛盾背景色键,系统默认高亮颜色。这也使我们不必明确设置焦点的ListView 控件,然后再返回(正如我们上面建立的,不是实际发生的,因为你的离开事件处理方法)。这样做,将产生以下结果:

    But I have surprisingly little faith that you'll want to fix your application. People who ignore warnings in the documentation tend to be the same people who don't listen to well-meaning advice on Q&A sites. So I'll throw you a bone and tell you how to get the effect you desire anyway. The key lies in not setting the ListViewItem's Selected property, which avoids the conflict between your custom BackColor and the system default highlight color. It also frees you from having to explicitly set the focus to the ListView control and back again (which, as we established above, isn't actually happening, given your Leave event handler method). Doing that produces the following result:

    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;

         

    和这里的code-它不是很pretty,但是这仅仅是一个概念的证据,而不是最好的做法的一个示例:

    And here's the code—it's not very pretty, but this is just a proof of concept, not a sample of best practice:

    public partial class Form1 : Form
    {
       public Form1()
       {
          InitializeComponent();
          listView1.View = View.Details;
          listView1.HideSelection = false;
       }
    
       private void textBox1_TextChanged(object sender, EventArgs e)
       {
          foreach (ListViewItem item in listView1.Items)
          {
             if (item.Text == textBox1.Text)
             {
                item.BackColor = Color.LightSteelBlue;
                return;
             }
          }
       }
    
       private void textBox1_Leave(object sender, EventArgs e)
       {
          this.textBox1.Focus();
       }
    }
    

    这篇关于如何更改列表视图选择行背景色,即使专注于其他控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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