按颜色分类列表框项目 [英] Categorize Listbox items by Color

查看:79
本文介绍了按颜色分类列表框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图用2个按钮制作一个列表框. 列表框应该显示特定文件夹中的文件(我回到此列表) 并且这两个按钮应该称为"Set .."(与set目录中的一样) 和更新(将刷新列表(每次Windows窗体运行时,我都会执行此操作.

So I'm trying to make a listbox with 2 buttons. Listbox is supposed to display files from a specific folder (I get back to this) And the two buttons are supposed to be called "Set.." (As in set directory) and Update (As the list will be refreshed (Something I do every time the Windows Form Runs.

因此,到目前为止,当我启动我的应用程序并转到带有列表框的表单时,该列表框为空.当按下更新"时,列表"框将从位于我的硬盘驱动器上的地址(因此这是位于我的代码中的静态地址)中拾取文件.

So as of now, when I start my application, and go to the form with the listbox, the listbox is empty. When pressing "Update", the List box picks up files from an address located on my Harddrive (So this is a static address located in my code).

它还会找到7个不同的扩展名(文件类型),并正确列出所有扩展名.

It also finds 7 different extensions (Filetypes), and lists all of them correctly.

我的问题如下,我希望设置按钮在首次运行时为用户打开文件对话框,以便用户自己可以选择程序索引或搜索"所在的文件夹.然后,当他再次运行该应用程序并找到列表框时,他只能按Update,该列表框将显示上次选择的文件夹的内容.

My problem is as follows, I want the set Button to open a File Dialog for the user on First-Time Runtime, so the user himself can choose what folder the program "Indexes or Searches" if you will. And then when he runs the application again, and finds the listbox, he can only press Update, and the listbox shows the content of the folder he choose last time.

设置-"按钮目前在我的代码中没有执行任何操作.

The Set - button doesn't do anything in my code right now.

第二,我希望每个文件类型都用特定颜色标记或着色.

Second up, I want each filetype to be labeled or colored with a specific color.

像; .txt应该是蓝色,.jpg应该是红色,等等.

Like; .txt should be blue, .jpg is red, ect..

如果有帮助,请运行Visual Studio 2013.

Running Visual Studio 2013 if that helps.

另外,在检查我的代码时,如果您也有任何建议,我将如何改进代码,使其变得更容易,更简短,以及为了避免重复的代码而进行更改,请让我知道.

Also, when checking my code, if you have any suggestions too, how I can improve the code, make it easier, shorter, and just to change things to avoid duplicate codes, please let me know.

这里来自VS2013中的设计

Here is from the Design in VS2013

代码:

Private Sub Form_Load(sender As Object, e As EventArgs) Handles Me.Load

FolderBrowserDialog1.SelectedPath = "xxx\xxx\xxx\xxx"
        System.IO.Directory.GetCurrentDirectory()

 Private Sub updateButtonGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updateButtonGame.Click
        If FolderBrowserDialog1.SelectedPath = "xxx\xxx\xxx\xxx" Then

            ListFiles(FolderBrowserDialog1.SelectedPath)

        End If
    End Sub

    Private Sub ListFiles(ByVal folderPath As String)
        filesListBox.Items.Clear()

        Dim fi = From f In New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath).GetFiles().Cast(Of IO.FileInfo)() _
                  Where f.Extension = ".z64" OrElse f.Extension = ".nds" OrElse f.Extension = ".BIN" OrElse f.Extension = ".smc" OrElse f.Extension = ".ISO" OrElse f.Extension = ".nes" OrElse f.Extension = ".gb"
                  Order By f.Extension
                  Select f

        For Each fileInfo As System.IO.FileInfo In fi
            filesListBox.Items.Add(fileInfo.Name)
        Next

    End Sub

另一件事,这是更可选的.

Another thing, this is more optional..

我的列表完全是黑色的,所以我选择让列表"框中的项目"变成浅灰色".

My list is completely black, so I choose to have the "Items" in the Listbox turn Light gray.

我玩了一种叫做e.Graphics的东西,希望实现为特定文件类型着色,然后将所有项目变成黑色,红色或其他任何我喜欢的颜色. 但是删除代码后,所有项目都变成与列表框的背景色相同的颜色.因此,除了侧面弹出的滚动条(因为它在我选择的文件夹中有很多项目)之外,我再也看不到实际在其中的元素

I played around with something called e.Graphics in hope to achieve Coloring a specific filetype, and it turned ALL items either Black, Red, or whatever I put it to. But after removing the code, all Items turns into the same color as the Background color of the Listbox. So I can no longer see the elements actually being there, other than the Scroll-bar popping up on the side (Since its many Items in the folder I picked)

此外,我在编码/视觉工作室方面还不够好,因为我大约是在1周前开始的. 从VB 2010开始,然后转到VS2013,看看我是否能够解决一些与列​​表框有关的问题.

Also, I am not that good with coding/visual studio yet, as I started around 1 week ago to date. Started with VB 2010 and then went to VS2013 to see if I managed to fix some issues, also related to List Box.

如果我讲得不好,请告诉我,我会提供更好的信息. 该项目也是首先在VB 2010中创建的,然后迁移"或在VS 2013中打开.

If I explained rather poorly, let me know and I'll update with better info. Project was also first created in VB 2010, and then "Migrated" or opened in VS 2013.

推荐答案

一种更好的方法是使用ListViewImageList并在其中包含一些用于文本,图像,PDF等的标准图像.列表,然后在将每个项目添加到列表时为每个项目设置图像键.

A much, much better way to do this is with a ListView and an ImageList with some standard images for Text, Image, PDF etc in the list, then just set the image key for each item when you add them to the list.

或者,您可以在列表框中模拟相同的内容(使用OwnerDrawFixed)以绘制指定的图像以指示文件类型.实现此目标的一个非常好的方法是使用ExtenderProvider作为起点,使用类似于以下代码的代码.作为EP,您可以将任何cbo或列表框链接到图像列表,以提供图像提示,就像ListView的工作原理一样:

Alternatively, you could emulate the same thing in a listbox (using OwnerDrawFixed) to draw a specified image to indicate the file type. A really good way to implement this is as an ExtenderProvider using code similar to that below as a starting point. As an EP, you can link any cbo or listbox to an image list to provide image cues very much like the ListView works:

您经常看不到有色商品成语的原因是,无论您选择哪种颜色,在所有系统上看起来都不正确.颜色越多,用户的配色方案就越不可能具有足够的对比度,可读性等,就越有可能.您也不需要传奇"来解释颜色的含义-图像是可以自我解释的.也就是说,DrawItem代码将如下所示:

The reason you do not see your colored item idiom very often is that whatever colors you pick will not look right on all systems. The more colors, the more likely and more often they wont have enough contrast, be readable etc with the user's color scheme. You also dont need a "Legend" to explain what the colors mean - an image is self explanatory. That said, the DrawItem code would be something like this:

NB:列表框控件设置为OwnerDrawFixedItemHeight = 16

NB: Listbox control is set to OwnerDrawFixed, ItemHeight = 16

Private Sub lb_DrawItem(sender As Object, 
           e As DrawItemEventArgs) Handles lb.DrawItem

    Dim TXT As Color = Color.Black
    Dim JPG As Color = Color.Green
    Dim PDF As Color = Color.Blue
    Dim EXE As Color = Color.Gray
    Dim SEL As Color = SystemColors.HighlightText
    Dim thisColor As Color = Color.Orange

    Dim ndx As Integer = e.Index

    ' isolate ext ans text to draw
    Dim text As String = lb.Items(ndx).ToString()
    Dim ext As String = System.IO.Path.GetExtension(text).ToLowerInvariant

    ' dont do anything if no item being drawn
    If ndx = -1 Then Exit Sub

    ' default
    e.DrawBackground()

    ' color selector
    Select Case ext
        Case ".jpg"
            thisColor = JPG
        Case ".txt"
            thisColor = TXT
        Case ".exe"
            thisColor = EXE
        Case ".pdf"
            thisColor = PDF
    End Select

    ' override color to use default when selected
    If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
        thisColor = SEL
    End If

    ' render the text
    TextRenderer.DrawText(e.Graphics, text, lb.Font, e.Bounds, 
               thisColor, TextFormatFlags.Left)

    ' default
    e.DrawFocusRectangle()

End Sub

结果:

在我的系统"上工作 TM

这篇关于按颜色分类列表框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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