Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am trying to get a little fancy with a list box and use the owner drawn feature to change the look. I have written some code to change the look of the selected item but i cannot figure out how I can reverse it when someone clicks another item.

 

Here is the code I wrote to do this:

Private Sub listBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
       e.DrawBackground()
       Dim l As ListBox = CType(sender, ListBox)

       Dim stringSize As SizeF = e.Graphics.MeasureString(l.SelectedItem, New Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold))

       If ((DrawItemState.Selected) > 0) Then
           Dim selRect As New Rectangle(e.Bounds.X, e.Bounds.Y, l.Width - 1, stringSize.Height / 2)
           Dim br As New SolidBrush(Color.FromArgb(206, 223, 255))
           e.Graphics.FillRectangle(br, selRect)
           e.Graphics.DrawRectangle(New Pen(Color.FromArgb(30, 108, 253), 1), selRect)
           e.Graphics.DrawString(l.Items(e.Index), e.Font, Brushes.Black, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
           br.Dispose()
       End If
   End Sub

  • Leaders
Posted

You have to draw over 'all' of the elements in an owner-drawn listbox.

 

I'm quite surprised if

If ((DrawItemState.Selected) > 0) Then

that line works (I thought DrawItemState.Selected is always 1)

 

But, I'll just assume that it works for now.

 

You would put an Else onto this If block and then, in this Else block, you draw the object as it would be if it were not selected. :)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted
You have to draw over 'all' of the elements in an owner-drawn listbox.

 

I'm quite surprised if

If ((DrawItemState.Selected) > 0) Then

that line works (I thought DrawItemState.Selected is always 1)

 

But, I'll just assume that it works for now.

 

You would put an Else onto this If block and then, in this Else block, you draw the object as it would be if it were not selected. :)

 

I have tried that before and it does not work.....actually if I remove the if then statement and run it , the program does not draw the rectangles around the text but its still draws the text .

  • Leaders
Posted

OK, so how about doing the check like this. That If statement must not be working as I thought it wouldn't.

 

If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted
OK, so how about doing the check like this. That If statement must not be working as I thought it wouldn't.

 

If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then

 

awsome, that fixed it! thank you!

now for my next step, which I havnt looked into yet but do you know how to either redraw the vertical scroll or tell the listbox to scroll up or down?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...