B
Brian Henry
I am trying to make a custom user control that gets a list of users from our
database and populates the list, its an owner drawn control also, the
problem is, I placed the item onto a form and every time i make a change to
the control it reads the database and adds new items to the listing but
never gets rid of the old items in the list, the form seems to be makeing a
stored copy of the items locally then rebuilding the list ontop of it...
here is my code for the control... what am i doing wrong? or any suggestions
on where you should populate a list like this... thanks!
I also tried putting the code that builds the list into the overrided
onpaint method, which fixed the problem in a way, but im scared its useing
up a ton of bandwidth querying the database at every paint.. any suggestions
would be great, thanks!
==================================================================
Imports System.Drawing
Imports System
Public Class userList
Inherits System.Windows.Forms.ListBox
Private Shared icoMyIcon As Icon = New
Icon(GetType(userList).Assembly.GetManifestResourceStream("bdb.person256xp16
..ico"))
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
database.OpenDBConnections() ' open if not already
MyBase.Items.Clear()
MyBase.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
Dim cmdGetUsers As New SqlClient.SqlCommand("SELECT loginname from users",
database.dbConnectionUsers)
database.dbReader = cmdGetUsers.ExecuteReader
While database.dbReader.Read
MyBase.Items.Add(database.dbReader("loginname"))
End While
database.dbReader.Close()
End Sub
'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container
End Sub
#End Region
Protected Overrides Sub RefreshItem(ByVal index As Integer)
End Sub
Protected Overrides Sub SetItemsCore(ByVal items As
System.Collections.IList)
End Sub
Protected Overrides Sub OnDrawItem(ByVal e As
System.Windows.Forms.DrawItemEventArgs)
e.DrawBackground()
e.DrawFocusRectangle()
e.Graphics.DrawIcon(icoMyIcon, 1, e.Bounds.Top + 2)
If e.State = Windows.Forms.DrawItemState.None Then
e.Graphics.DrawString(MyBase.Items.Item(e.Index), New
Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular), New
SolidBrush(Color.Black), 18, e.Bounds.Top)
Else
e.Graphics.DrawString(MyBase.Items.Item(e.Index), New
Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular), New
SolidBrush(Color.White), 18, e.Bounds.Top)
End If
End Sub
Protected Overrides Sub OnMeasureItem(ByVal e As
System.Windows.Forms.MeasureItemEventArgs)
e.ItemHeight = 18
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
'Add any initialization after the InitializeComponent() call
MyBase.OnPaint(e)
End Sub
End Class
============================================================================
====
database and populates the list, its an owner drawn control also, the
problem is, I placed the item onto a form and every time i make a change to
the control it reads the database and adds new items to the listing but
never gets rid of the old items in the list, the form seems to be makeing a
stored copy of the items locally then rebuilding the list ontop of it...
here is my code for the control... what am i doing wrong? or any suggestions
on where you should populate a list like this... thanks!
I also tried putting the code that builds the list into the overrided
onpaint method, which fixed the problem in a way, but im scared its useing
up a ton of bandwidth querying the database at every paint.. any suggestions
would be great, thanks!
==================================================================
Imports System.Drawing
Imports System
Public Class userList
Inherits System.Windows.Forms.ListBox
Private Shared icoMyIcon As Icon = New
Icon(GetType(userList).Assembly.GetManifestResourceStream("bdb.person256xp16
..ico"))
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
database.OpenDBConnections() ' open if not already
MyBase.Items.Clear()
MyBase.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
Dim cmdGetUsers As New SqlClient.SqlCommand("SELECT loginname from users",
database.dbConnectionUsers)
database.dbReader = cmdGetUsers.ExecuteReader
While database.dbReader.Read
MyBase.Items.Add(database.dbReader("loginname"))
End While
database.dbReader.Close()
End Sub
'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container
End Sub
#End Region
Protected Overrides Sub RefreshItem(ByVal index As Integer)
End Sub
Protected Overrides Sub SetItemsCore(ByVal items As
System.Collections.IList)
End Sub
Protected Overrides Sub OnDrawItem(ByVal e As
System.Windows.Forms.DrawItemEventArgs)
e.DrawBackground()
e.DrawFocusRectangle()
e.Graphics.DrawIcon(icoMyIcon, 1, e.Bounds.Top + 2)
If e.State = Windows.Forms.DrawItemState.None Then
e.Graphics.DrawString(MyBase.Items.Item(e.Index), New
Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular), New
SolidBrush(Color.Black), 18, e.Bounds.Top)
Else
e.Graphics.DrawString(MyBase.Items.Item(e.Index), New
Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular), New
SolidBrush(Color.White), 18, e.Bounds.Top)
End If
End Sub
Protected Overrides Sub OnMeasureItem(ByVal e As
System.Windows.Forms.MeasureItemEventArgs)
e.ItemHeight = 18
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
'Add any initialization after the InitializeComponent() call
MyBase.OnPaint(e)
End Sub
End Class
============================================================================
====