By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,420 Members | 1,085 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,420 IT Pros & Developers. It's quick & easy.

custom control with database backend problem

P: n/a
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.GetManifestResourc eStream("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

================================================== ==========================
====


Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
my solution so far is not to inherite a listbox like i was but to make a
user custom control and do stuff from inside that... but suggestions would
still be nice thanks!
"Brian Henry" <brianiup[nospam]@adelphia.net> wrote in message
news:O3**************@TK2MSFTNGP12.phx.gbl...
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.GetManifestResourc eStream("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

================================================== ========================== ====

Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.