Results 1 to 10 of 10

Thread: ListView1 partial searched text highlighted

  1. #1
    Junior Member
    Join Date
    May 12
    Posts
    22

    ListView1 partial searched text highlighted

    I'm trying to do this..



    I Took the a screenshot of my program and fixed it up in photoshop what i'm trying to do. See the yellow highlight and and the orange text? well i'm trying to do either one of those but i'm not sure if it's possible to do.

    What i'm trying to do is when text is entered in textbox1, it will highlight or bold and color every word that has the word that is in textbox1 in listview1

    Here is the code i'm trying to do this with, but it changes the whole words for color instead of just that word in textbox.

    vb.net Code:
    1. For Each sKey As ListViewItem In Me.ListView2.Items
    2.             If sKey.SubItems(0).Text.StartsWith(Me.SeedKW.Text) Then
    3.                 sKey.SubItems(0).ForeColor = Color.Aqua
    4.             End If
    5.         Next

  2. #2
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,292

    Re: ListView1 partial searched text highlighted

    Moved From The CodeBank (which is for sharing code rather than posting questions )
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,968

    Re: ListView1 partial searched text highlighted

    It can be done but you'll have to make the listview owner draw and then do all the graphics and string drawing yourself which sounds a lot easier than it actually is on account of the metrics involved in lining up partial strings. I've done this with the simpler listbox but with only moderate success having never found a viable formula for working with changes of font.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,968

    Re: ListView1 partial searched text highlighted

    Well, inspired by the question, I did some more research and I think I've cracked it. It's pumpkin time here though so you'll have to wait for an example.

  5. #5
    Junior Member
    Join Date
    May 12
    Posts
    22

    Re: ListView1 partial searched text highlighted

    Quote Originally Posted by dunfiddlin View Post
    Well, inspired by the question, I did some more research and I think I've cracked it. It's pumpkin time here though so you'll have to wait for an example.
    Well, have a happy pumpkin time (btw what does that mean? lol)

  6. #6
    Frenzied Member i00's Avatar
    Join Date
    Mar 02
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    1,719

    Re: ListView1 partial searched text highlighted

    Hi .. i did something like this with the DGV.. check post here, you can see the result from some of the screen shots... if you want i could help you implement this into the list view (PM me)

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,968

    Re: ListView1 partial searched text highlighted

    Quote Originally Posted by Cjones636 View Post
    Well, have a happy pumpkin time (btw what does that mean? lol)
    Midnight! When Cinderella's carriage reverts to its original vegetable form and all good coders should be going to bed!

  8. #8
    Junior Member
    Join Date
    May 12
    Posts
    22

    Re: ListView1 partial searched text highlighted

    I've been away from coding for a long time, never used listview so i can't figure out why this is so hard. It seems so simple in my head but in reality i'm beating my head up against the desk. Usually if i skip it and go do something else, then when i come back to it i kind of figure it out pretty quick.. To me this is almost like working on a car. Have you ever tried to get a nut started on a bolt that was in an awkward position, but for the life of you, you just can't get it started and when you hit your boiling point and ready to take a hammer and beat your car to death with it, you stop and go do something else then when you come back to it an an hour or 2, the nut goes right on the bolt with ease? Well to me, working on a car and coding is pretty similar is some cases.

    I'm not a "master" coder by any stretch of the imagination. I Started coding in vb3 and pretty much taught myself from tutorials... Never read a book on coding or went to school for programming. I Pretty much taught myself to understand it. It's just when i start venturing outside of my knowledgeable zone, that's when it all starts looking like gibberish again.

    But please no one think i'm just another code beggar, because i'm not and i do really, really try to do it on my own. I Spend plenty of days and late nights trying to do it on my own, but when i'm finally whipped and down for the 10 count i ask for help getting back up (if that made any sense to you).

    I Do this as a hobby and that's it... Never sold anything i made and i always credit everyone who helped me if i share my program with the public. I'm not one of those people that takes all the credit when i had help on it.

  9. #9
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,968

    Re: ListView1 partial searched text highlighted

    Er ... right ... well.... <cough> ...

    Real life being what it is I may have to give this slightly less priority than, you know, earning a living, eating and so on so I'm not setting myself any deadlines but if you want to have a try at this on your own while I dilly and dally, this is how far I got last night on the listbox project I already talked about. There are added complications with the listview but it's basically the same principle ...

    vb.net Code:
    1. Private Sub ListBox1_DrawItem(sender As System.Object, e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
    2. ' Aim: to show the last letter of a selected 3 char string in red and bold font
    3.  
    4.         Dim normal As String 'the normal bit
    5.         Dim bold As String 'the bold bit
    6.         Dim NewX = e.Bounds 'the drawing rectangle
    7.         Dim f = New Font(e.Font, FontStyle.Bold)
    8.         Dim Metric As SizeF 'this determines the display size of a string
    9.         e.Graphics.PageUnit = GraphicsUnit.Pixel 'probably it is already but just to make sure
    10.  
    11.         Try
    12.             If e.Index = ListBox1.SelectedIndex Then
    13.  
    14.                 normal = ListBox1.Items(e.Index).ToString
    15.                 bold = normal.Substring(2)
    16.                 normal = normal.Substring(0, 2)
    17.                 Metric = e.Graphics.MeasureString(normal, e.Font, e.Bounds.Width, StringFormat.GenericTypographic) ' size the rectangle for first section
    18.                 NewX.X = e.Bounds.X + Metric.Width 'move draw position to end of first section rectangle
    19.  
    20.                 e.Graphics.DrawString(normal, e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault)
    21.                 e.Graphics.DrawString(bold, f, Brushes.Red, NewX, StringFormat.GenericDefault)
    22.  
    23.  
    24.             Else
    25.                 e.Graphics.DrawString(ListBox1.Items(e.Index).ToString, e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault) 'everything just as it usually is
    26.  
    27.             End If
    28.         Catch
    29.  
    30.         End Try
    31.  
    32.     End Sub

  10. #10
    Junior Member
    Join Date
    May 12
    Posts
    22

    Re: ListView1 partial searched text highlighted

    Quote Originally Posted by dunfiddlin View Post
    Er ... right ... well.... <cough> ...

    Real life being what it is I may have to give this slightly less priority than, you know, earning a living, eating and so on so I'm not setting myself any deadlines but if you want to have a try at this on your own while I dilly and dally, this is how far I got last night on the listbox project I already talked about. There are added complications with the listview but it's basically the same principle ...

    vb.net Code:
    1. Private Sub ListBox1_DrawItem(sender As System.Object, e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
    2. ' Aim: to show the last letter of a selected 3 char string in red and bold font
    3.  
    4.         Dim normal As String 'the normal bit
    5.         Dim bold As String 'the bold bit
    6.         Dim NewX = e.Bounds 'the drawing rectangle
    7.         Dim f = New Font(e.Font, FontStyle.Bold)
    8.         Dim Metric As SizeF 'this determines the display size of a string
    9.         e.Graphics.PageUnit = GraphicsUnit.Pixel 'probably it is already but just to make sure
    10.  
    11.         Try
    12.             If e.Index = ListBox1.SelectedIndex Then
    13.  
    14.                 normal = ListBox1.Items(e.Index).ToString
    15.                 bold = normal.Substring(2)
    16.                 normal = normal.Substring(0, 2)
    17.                 Metric = e.Graphics.MeasureString(normal, e.Font, e.Bounds.Width, StringFormat.GenericTypographic) ' size the rectangle for first section
    18.                 NewX.X = e.Bounds.X + Metric.Width 'move draw position to end of first section rectangle
    19.  
    20.                 e.Graphics.DrawString(normal, e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault)
    21.                 e.Graphics.DrawString(bold, f, Brushes.Red, NewX, StringFormat.GenericDefault)
    22.  
    23.  
    24.             Else
    25.                 e.Graphics.DrawString(ListBox1.Items(e.Index).ToString, e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault) 'everything just as it usually is
    26.  
    27.             End If
    28.         Catch
    29.  
    30.         End Try
    31.  
    32.     End Sub

    Yeah, i'll have a go at it. Well just as soon as i get done play iracing lol

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •