473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

custom control with database backend problem

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
1 1450
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Moogy | last post by:
I'm pulling my hair out here. First, I'm new to XML, so that doesn't help, but none of this makes any sense to me. All I'm trying to do is take a simple source XML file and translate it with an...
6
by: Ben | last post by:
Hi I've got a problem with a custon control that inherits form the UserControl class and that used the overlib javascript library. The control works well in a standard form but when I used it in...
7
by: Julian Jelfs | last post by:
Hi, I had an aspx pag in .Net 1.1 with a label on it. As such I had a code behind page with a declaration for that label. When I convert to Asp.Net 2.0 the code behind is converted to a...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
17
by: shineofleo | last post by:
Here is the situation: I wrote a VB programm, which stores all the information in a single Access database file using jet engine. It worked well, however one of my customs reported that there was...
15
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt...
0
by: Atul Thombre | last post by:
Hello, I am developing a custom membership provider. For that I built a prototype that uses a SQL Server 2005 database as a backend store. I implemented the class...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.