Connecting Tech Pros Worldwide Forums | Help | Site Map

Counting clicks in a listbox....

Newbie
 
Join Date: Jul 2007
Posts: 3
#1: Jul 22 '07
Have a counter in place, however, it isn't correctly counting clicks in a listbox...help....

Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#2: Jul 22 '07

re: Counting clicks in a listbox....


Quote:

Originally Posted by vipbus7

Have a counter in place, however, it isn't correctly counting clicks in a listbox...help....

I think you need to provide us more details about the situation. For instance, what version of VB are you using? And what have you done so far, which isn't working?

One possible problem is variable "scope" (check your VB doco). If you create a variable in the Click event procedure, for example, then it only exists until you exit from that procedure. Next time you execute the procedure, it will be created from scratch again, and won't have the value you left there before.

That's just one possibility, of course. Without knowing more about what you're doing, I can't say whether it actually is the problem.
Newbie
 
Join Date: Jul 2007
Posts: 3
#3: Jul 22 '07

re: Counting clicks in a listbox....


I am using VB 5....here is the code I have in place so far...counter isn't showing correct number of times someone has selected something in the ListBox via a msgbox...the number does change, however, it is never correct.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit On
  2. Option Strict On
  3. Imports System.Data
  4. Public Class Form1
  5.     Dim dt As New DataTable()   'First step in filling data table with data from database.
  6.     Dim dt1 As New DataTable()
  7.     Dim counter As Integer = 0
  8.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e _
  9.         As System.EventArgs) Handles Button2.Click
  10.         MsgBox(counter & " cities were looked at.", 0, "City Count")
  11.         End
  12.     End Sub
  13.     Private Sub Form1_Load(ByVal sender As Object, ByVal e _
  14.     As System.EventArgs) Handles Me.Load
  15.         'Open a connection to the database
  16.         Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  17.         "Data Source=LOSSPROPERTIES.MDB"
  18.         Dim sqlStr As String = "SELECT * FROM Properties"
  19.         Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connStr)
  20.         dataAdapter.Fill(dt)
  21.         dataAdapter.Dispose()
  22.         ListBox2.DataSource = dt
  23.         ListBox2.DisplayMember = "property"
  24.         TextBox1.Clear()
  25.         TextBox2.Clear()
  26.         TextBox3.Clear()
  27.     End Sub
  28.     Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, _
  29.     ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
  30.        'Load data from the Countries table into second data table
  31.         Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  32.                 "Data Source=LOSSPROPERTIES.MDB"
  33.         Dim sqlStr As String = "SELECT * FROM Counties"
  34.         Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connStr)
  35.         dataAdapter.Fill(dt1)
  36.         dataAdapter.Dispose()
  37.         TextBox1.Text = CStr(dt.Rows(ListBox2.SelectedIndex)("sales"))
  38.         TextBox1.Text = TextBox1.Text
  39.         TextBox1.Text = FormatNumber(TextBox1.Text, 2)
  40.         For i As Integer = 0 To dt.Rows.Count - 1
  41.             Do While CStr(dt1.Rows(counter)("County")) <> _
  42.             CStr(dt.Rows(ListBox2.SelectedIndex)("county"))
  43.                 counter = counter + 1
  44.             Loop
  45.             TextBox2.Text = CStr(dt1.Rows(counter)("sales"))
  46.             TextBox2.Text = TextBox2.Text
  47.             TextBox2.Text = FormatNumber(TextBox2.Text, 2)
  48.             Dim popPercent As Double
  49.             Dim devPop, ctyPop As Double
  50.             devPop = CDbl(TextBox1.Text)
  51.             ctyPop = CDbl(TextBox2.Text)
  52.             popPercent = CDbl(devPop / ctyPop)
  53.             TextBox3.Text = ("The percentage of the population of " & _
  54.                                 CStr(dt.Rows(ListBox2.SelectedIndex)("properties")) & " to the population of " & _
  55.                                 CStr(dt.Rows(ListBox2.SelectedIndex)("county")) & " is: " & FormatPercent(popPercent))
  56.         Next
  57.     End Sub
  58. End Class
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#4: Jul 22 '07

re: Counting clicks in a listbox....


I can guarantee you, that is not VB5 code. Presumably you must mean VB 2005.

Anyway, I'll have a look through it as soon as I have a chance.
Reply


Similar Visual Basic 4 / 5 / 6 bytes