[RESOLVED] [2005] Listview noob Q
Hey,
I am trying to get the data to appear in my listview as it would in a listbox. I am changing the View property to List but I am missing something. It is creating columns of data and a horizontal scroll, but I just want 1 column with a vertical scroll.
What am I doing wrong?
Re: [2005] Listview noob Q
Change the view to "Details".
Then make a single Column, name it.
Finally, add your items, one for each row in that column.
If you want multiple columns, you'll need to add a "SubItem" onto each "ListViewItem".
Re: [2005] Listview noob Q
unless you need the ability to do something like sort the list via a column header click, there is little benefit to using a listview over a listbox when you are only dealing with a single column of data.
Re: [2005] Listview noob Q
just trying to format the font accordinginly which I didn't thing the listbox would allow.
thanks for the replies!
Re: [RESOLVED] [2005] Listview noob Q
Any font formatting you can do in the listview would be the same for the listbox. All you need to do is set the font property to whatever font style you want. That can be done at design time or runtime, in the designer, or in code.
Re: [RESOLVED] [2005] Listview noob Q
hmm I was trying to programmatically change the font color depending on the string inside of an item... what property would I use to do that in a listbox in code?
Re: [RESOLVED] [2005] Listview noob Q
you can use
Code:
ListView1.Font = New System.Drawing.Font("Arial", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point)
Re: [RESOLVED] [2005] Listview noob Q
wouldn't that change the font for all of the items?
Re: [RESOLVED] [2005] Listview noob Q
Just use a listbox, set its DrawMode property to OwnerDrawFixed.
Then you can custom draw the items, however you want.
Lets say I have a listbox (listbox1) that has a bunch of strings in it that either say "GOOD" or "BAD" and I want GOOD to be blue, and BAD to be red.
Code like this would work:
Code:
Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
Dim stringToDraw As String = ListBox1.Items(e.Index).ToString
Dim C As Color
If stringToDraw = "GOOD" Then
C = Color.Blue
Else 'BAD
C = Color.Red
End If
e.DrawBackground()
e.DrawFocusRectangle()
e.Graphics.DrawString(stringToDraw, e.Font, New SolidBrush(C), e.Bounds)
End Sub
Re: [RESOLVED] [2005] Listview noob Q
opps i didnt notice you wanted to change font for item inside........may be going for listview will be good option
Re: [RESOLVED] [2005] Listview noob Q
Quote:
Originally Posted by riteshjain1982
opps i didnt notice you wanted to change for item inside........may be going for listview will be good option
please see my previous post.
Re: [RESOLVED] [2005] Listview noob Q
Quote:
Originally Posted by kleinma
please see my previous post.
got it ;)
Re: [RESOLVED] [2005] Listview noob Q
Thanks again... will be using kleinma's method.
Re: [RESOLVED] [2005] Listview noob Q
actually that method didn't do anythin. :P
this is what I used:
vb Code:
Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
Dim stringToDraw As String = ListBox1.Items(e.Index).ToString
Dim C As Color
If stringToDraw.Contains("Error") Then
C = Color.Red
End If
e.DrawBackground()
e.DrawFocusRectangle()
e.Graphics.DrawString(stringToDraw, e.Font, New SolidBrush(C), e.Bounds)
End Sub
Re: [RESOLVED] [2005] Listview noob Q
did you set the DrawMode property of the listbox to OwnerDrawFixed? If you do not, that code will never run.
Re: [RESOLVED] [2005] Listview noob Q
Also, still set color to SOMETHING when the string does not contain "Error", otherwise it will be blank..
Code:
If stringToDraw.Contains("Error") Then
C = Color.Red
else
C = ListBox1.Forecolor 'default color
End If
Re: [RESOLVED] [2005] Listview noob Q
oops I'm an idiot... thanks again.
Re: [RESOLVED] [2005] Listview noob Q
glad you got it all working ;)
owner drawing is really cool, as it lets you add a lot of custom visual functionality while still retaining all the other functionality the control already had.
You can also owner draw listview's as well, but I think for your specific task here, the listbox is your best bet.
Re: [RESOLVED] [2005] Listview noob Q
I'm attempting to use the above code examples to color code my results based on string.contains criteria.
I've set my LstBox.DrawMode = DrawMode.OwnerDrawFixed:
vb.net Code:
Private Sub main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LstResults.DrawMode = DrawMode.OwnerDrawFixed
End Sub
Then I've added the routine that is to evaluate the strings of the individual items of the LstBox control:
vb.net Code:
Private Sub LstResults_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles LstResults.DrawItem
Dim stringToDraw As String = LstResults.Items(e.Index).ToString
Dim C As Color
If stringToDraw.Contains("Make This Red") Then
C = Color.Red
End If
e.DrawBackground()
e.DrawFocusRectangle()
e.Graphics.DrawString(stringToDraw, e.Font, New SolidBrush(C), e.Bounds)
End Sub
But, currently I am unable to use my SqlDataReader because the I cannot load the form, resulting in a -1 index of the LstResults control. I know this is because it is trying to draw and evaluate the list items before there are any present.
vb.net Code:
Dim reader As SqlDataReader = comm.ExecuteReader()
While reader.Read()
LstResults.Items.Add(reader("that_result").ToString() & vbTab & reader("this_result"))
End While
'NowDrawItems()
I'm just very uncertain as to how to only use the Draw routine after the query to populate the items has been resulted. I'd like the blank ListBox to be visible before I select the query to run from DropDown, but only set the items to color coded after there truly are items present to evaluate.
I know that the current issue is due to having added the ListBox control in using the toolbox instead of instantiating it through code directly, is there a way to still use the DrawItem event with a control that has already been created so that I do not have to determine the exact placement, width, and height of the control, or should I just instantiate a new Listbox control that sits above my current Listbox control and set the values equal to that of the dummy control?
I apologize in advanced if this seems like a very juvenile question, but I can't quite wrap my head around it, and any assistance that anyone can provide would be greatly appreciated.
Thank you!